Found 1034 Articles for Matplotlib

How to draw a rectangle over a specific region in a Matplotlib graph?

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:52:30

531 Views

To draw a rectangle over a specific region in a matplotlib graph, we can take the following steps −Using subplots() method, create a figure and a set of subplots, where nrows=1.Using rectangle, we can create a rectangle, defined via an anchor point and its width and height. Where, edgecolor=orange, linewidth=7, and facecolor=green.To plot a diagram over the axis, we can create a line using plot() method, where line color is red.Add a rectangle patch on the diagram, using add_patch() 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 figure, ax = plt.subplots(1) ... Read More

How to create a draggable legend in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:52:08

1K+ Views

To create a draggable legend in matplotlib, we can take the following steps −Create two lines, line1 and line2, using plot() method.Place the legend for plot line1 and line2 with ordered lables at location 1, using legend() method.To create a draggable legend, use set_draggable() method, where state=True. If state=False, then we can't drag the legend.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, 2, 3]) line2, = plt.plot([3, 2, 1]) leg = plt.legend([line2, line1], ["line 2", "line 1"], loc=1) leg.set_draggable(state=True) plt.show()OutputOn the output window, you can drag the legend around with ... Read More

Automatically Rescale ylim and xlim in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:51:49

2K+ Views

To rescale ylim and xlim automatically, we can take the following steps −To plot a line, use plot() method and data range from 0 to 10.To scale the xlim and ylim automatically, we can make the variable scale_factore=6.Use scale_factor (from Step 2) to rescale the xlim and ylim, using xlim() and ylim() methods, respectively.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 plt.plot(range(0, 10)) scale_factor = 6 xmin, xmax = plt.xlim() ymin, ymax = plt.ylim() plt.xlim(xmin * scale_factor, xmax * scale_factor) plt.ylim(ymin * scale_factor, ymax * scale_factor) plt.show()OutputRead More

How do I plot Shapely polygons and objects using Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:51:12

5K+ Views

To plot shapely polygons and objects using matplotlib, the steps are as follows −Create a polygon object using (x, y) data points.Get x and y, the exterior data, and the array using polygon.exterior.xy.Plot x and y data points using plot() method with red color.Examplefrom shapely.geometry import Polygon import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True polygon1 = Polygon([(0, 5),    (1, 1),    (3, 0),    (4, 6), ]) x, y = polygon1.exterior.xy plt.plot(x, y, c="red") plt.show()Output

Set variable point size in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:53:39

323 Views

To set the variable point size in matplotlib, we can take the following steps−Initialize the coordinates of the point.Make a variable to store the point size.Plot the point using scatter method, with marker=o, color=red, s=point_size.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 xy = (3, 4) point_size = 100 plt.scatter(x=xy[0], y=xy[1], marker='o', c='red', s=point_size) plt.show()Output

How can I create a stacked line graph with matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 08:07:21

1K+ Views

To create a stacked lines graph with Python, we can take the following Steps −Create x, y, y1 and y2 points using numpy.Plot the lines using numpy with the above data (Step 1) and labels mentioned.Fill the color between curve y=e^x and y=0, using the fill_between() method.Fill the color between curve y=2x and y=0, using the fill_between() method.Fill the color between curve y=log(x) and y=0, using fill_between() method.Place the curve text using the legend() method.To display the figure, use the show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 5, 100) y = x * 2 y1 = np.log(x) ... Read More

How do I change matplotlib's subplot projection of an existing axis?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 08:04:40

1K+ Views

It seems difficult to change the projection of an existing axis, but we can take the following steps to create different type projections −Using subplot() method, add a subplot to the current figure, with nrows=1, ncols=3 and current index=1.Add a title to the current axis.Using subplot() method, add a subplot to the current figure, with nrows=1, ncols=3 and current index=2, projection=hammer.Add a title to current axis, hammer.Using subplot() method, add a subplot to the current figure, with nrows=1, ncols=3 and current index=3, projection=polar.Add a title to current axis, polar.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = ... Read More

Matplotlib Plots Lose Transparency When Saving as .ps/.eps

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 08:01:27

1K+ Views

Whenever plots are saved in .eps/.ps, then the transparency of the plots get lost.To compare them, we can take the following Steps −Create x_data and y_data using numpy.Plot x_data and y_data (Step 1), using the plot() method, with less aplha value, to make it more transparent.Use the grid() method to prove the transparency of the line.Save the created plot in .eps format.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x_data = np.linspace(1, 10, 100) y_data = np.sin(x_data) plt.plot(x_data, y_data, c='green', marker='o', alpha=.35, ms=10, lw=1) plt.grid() plt.savefig("lost_transparency_img.eps") plt.show()OutputThe PostScript backend ... Read More

How to plot a gradient color line in matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:58:32

7K+ Views

To plot a gradient color line in matplotlib, we can take the following steps −Create x, y and c data points, using numpy.Create scatter points over the axes (closely so as to get a line), using the scatter() method with c and marker='_'.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1, 1, 1000) y = np.exp(x) c = np.tan(x) plt.scatter(x, y, c=c, marker='_') plt.show()Output

Superscript in Python plots

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:56:42

9K+ Views

To put some superscript in Python, we can take the following steps −Create points for a and f using numpy.Plot f = ma curve using the plot() method, with label f=ma.Add title for the plot with superscript, i.e., kgms-2.Add xlabel for the plot with superscript, i.e., ms-2.Add ylabel for the plot with superscript, i.e., kg.To place the legend, use legend() method.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True a = np.linspace(1, 10, 100) m = 20 f = m*a plt.plot(a, f, c="red", lw=5, label="f=ma") plt.title("Force $\mathregular{kgms^{-2}}$") plt.xlabel("Acceleration $\mathregular{ms^{-2}}$") plt.ylabel("Acceleration $\mathregular{kg}$") ... Read More

Advertisements