Found 10476 Articles for Python

How to get the center of a set of points using Python?

Rishikesh Kumar Rishi
Updated on 12-May-2021 12:00:08

5K+ Views

To get the center of a set of points, we can add all the elements of the list and divide that sum with the length of the list so that result could be the center of the corresponding axes.StepsMake two lists of data points.Plot x and y data points using plot() method.Get the center tuple of the x and y data points.Place the center point on the plot.Annotate the center as label for center of the x and y data points.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [5, ... Read More

How do I set color to Rectangle in Matplotlib?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:58:39

2K+ Views

To set color to a rectangle in matplotlib, we can take the following steps −Create a figure or activate an existing figure using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.A rectangle is defined via an anchor point with width and heights.Add a rectangle patch to the plot.Set the x and y limit using xlim() and ylim() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) rectangle = patches.Rectangle((0, 0), 3, 3, edgecolor='orange', facecolor="green", linewidth=7) ax.add_patch(rectangle) plt.xlim([-5, 5]) plt.ylim([-5, 5]) ... Read More

How to scale axes in Mplot3d?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:57:04

4K+ Views

To scale axes in mplot3d, we can take the following steps −Create a figure or activate an existing figure using figure() method.Instantaite 3D axes instance using Axes3D() class.To scale X-axis, use set_xlim3d() method.To scale Y-axis, use set_ylim3d() method.To scale Z-axis, use set_zlim3d() method.To display the plot, use show() method.Examplefrom mpl_toolkits.mplot3d import Axes3D from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = Axes3D(fig) ax.set_xlim3d(-100, 100) ax.set_ylim3d(-100, 100) ax.set_zlim3d(-100, 100) plt.show()OutputRead More

How to increase the font size of the legend in my Seaborn plot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:55:28

4K+ Views

To increase the font size of the legend in a Seaborn plot, we can use the fontsize variable and can use it in legend() method argument.StepsCreate a data frame using Pandas. The keys are number, count, and select.Plot a bar in Seaborn using barplot() method.Initialize a variable fontsize to increase the fontsize of the legend.Use legend() method to place legend on the figure with fontsize in the argument.To display the figure, use show() method.Exampleimport pandas import matplotlib.pylab as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pandas.DataFrame(dict(    number=[2, 5, 1, 6, 3],    count=[56, 21, 34, 36, ... Read More

How to adjust the size of a Matplotlib legend box?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:52:48

7K+ Views

To adjust the size of a matplotlib legend box, we can use borderpad arguments in the legend method.StepsCreate line1 and line2 using two lists with different line widths.To place a legend on the figure and to adjust the size of legend box, use borderpad=2 in legend() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True line1, = plt.plot([1, 5, 1, 7], linewidth=0.7) line2, = plt.plot([5, 1, 7, 1], linewidth=2.0) plt.legend([line1, line2], ["line1", "line2"], bbox_to_anchor=(0.35, 1.0), borderpad=2) plt.show()Output

How to decrease the density of tick labels in subplots in Matplotlib?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:51:13

1K+ Views

To decrease the density of tick labels in subplots in matplotlib, we can assign the minimum value to density.StepsInitialize a variable, density.Create x and y data points using numpy.Plot x and y data points using plot() method.Get or set the current tick locations and labels of the X-axis using xticks() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True density = 10 x = np.linspace(-2, 2, density) y = np.sin(x) plt.plot(x, y) plt.xticks(x) plt.show()OutputRead More

How to switch axes in Matplotlib?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:49:46

8K+ Views

To switch axes in matplotlib, we can create a figure and add two subplots using subplots() method. Plot curves, extract x and y data, and set these data in a second plotted curve.StepsCreate x and y data points using numpy.Create a figure and add a set of two subplots.Set the title of the plot on both the axes.Plot x and y data points using plot() method.Extract the x and y data points using get_xdata and get_ydata.To switch the axes of the plot, set x_data and y_data of the axis 1 curve to axis 2 curve.Adjust the padding between and around the subplots.To display the ... Read More

Place text inside a circle in Matplotlib

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:47:57

2K+ Views

To place text inside a circle in matplotlib, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add a subplot method to the current axis.Create a Circle instance using Circle() class.Add a circle path on the plot.To place the text in the circle, we can use text() method.Scale x and y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) circle = matplotlib.patches.Circle((0, 0), radius=1, color='yellow') ax.add_patch(circle) plt.text(-.25, 0, "This is a Circle") plt.xlim([-4, 4]) plt.ylim([-4, 4]) plt.axis('equal') ... Read More

How to reshape a networkx graph in Python?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:45:40

413 Views

To reshape a networkx graph in Python, we can take the following steps −Create a data frame using Panda's data frame.Return a graph from Pandas DataFrame containing an edge list using from_pandas_edgelist() method.Draw the graph G with matplotlib. We can reshape the network by increasing and decreasing the list of keys "from" and "to".To display the figure, use show() method.Exampleimport pandas as pd import networkx as nx from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'from': ['A', 'B', 'C', 'A'], 'to': ['D', 'A', 'E', 'C']}) G = nx.from_pandas_edgelist(df, 'from', 'to') nx.draw(G, with_labels=True, node_size=150, alpha=0.5, linewidths=40) plt.show()OutputRead More

Plot a circle inside a rectangle in Matplotlib

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:44:02

1K+ Views

To place a circle inside a rectangle, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add a subplot to the current axis.Create a rectangle and a circle instance.Add a rectangle patch to the current axis.Add a circle patch to the current axis.Scale x and y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) rect = patches.Rectangle((2, 2), 8, 5, color='yellow') circle = patches.Circle((6, 4.5), radius=2, color='red') ax.add_patch(rect) ax.add_patch(circle) plt.xlim([-10, 10]) ... Read More

Advertisements