Found 33676 Articles for Programming

How to make the angles in a Matplotlib polar plot go clockwise with 0° at the top?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:26:43

5K+ Views

To make the angles in a matplotlib polar plot go clockwise with 00 at the top, we can take the followingsteps−StepsAdd a subplot to the current figure ax.To set polar plot clockwise with top 0o, set the theta direction as −1 using set_theta_direction()method. And, use set_theta_offset() method to set the offset for the location of 0 in radians.Create theta, using numpy.Plot theta and sin(theta) on the current axis.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 ax = plt.subplot(1, 1, 1, projection='polar') ax.set_theta_direction(-1) ax.set_theta_offset(np.pi / 2.0) ... Read More

Matplotlib – Plot over an image background in Python

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:26:18

20K+ Views

To plot over an image background, we can take the following steps−Read an image from a file into an array.Create a figure (fig) and add a set of subplots (ax) with extent [0, 300, 0, 300].Create an array x of range (300).Plot x using plot() method with linestyle=dotted, linewidth=2, and color=red.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 im = plt.imread("bird.jpg") fig, ax = plt.subplots() im = ax.imshow(im, extent=[0, 300, 0, 300]) x = np.array(range(300)) ax.plot(x, x, ls='dotted', linewidth=2, color='red') plt.show()OutputRead More

Equivalent to matlab's imagesc in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:25:54

4K+ Views

To make equivalent imagesc, we can use extent [left, right, bottom, top].StepsCreate random data using numpy.Display the data as an image, i.e., on a 2D regular raster, with data and extent [−1, 1, −1, 1] arguments.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 data = np.random.rand(4, 4) plt.imshow(data, extent=[-1, 1, -1, 1]) plt.show()Output

How to plot multiple graphs in Matplotlib?

Rishikesh Kumar Rishi
Updated on 22-Oct-2023 13:20:10

22K+ Views

To plot multiple graphs in matplotlib, we will use the following steps −StepsCreate x, y1 and y2 data points using numpy.Add a subplot to the current figure at index 1.Plot curve 1 using x and y1.Add a subplot to the current figure at index 2.Plot curve 2 using x and y2.To display the figure, use 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(-2, 2, 10) y1 = np.sin(x) y2 = np.cos(x) plt.subplot(211) plt.plot(y1) plt.subplot(212) plt.plot(y2) plt.show()OutputRead More

How do I extend the margin at the bottom of a figure in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:28:47

774 Views

To fix the extension of margin at the bottom of a figure, we can take the following steps −Using Pandas dataframe, create a df with the keys, time and speed.Plot df.time and df.speed using plot() method.Tick_params() is a convenience method for changing the appearance of ticks and tick labels. rotation=90 extends the tick labels at the bottom.To fix the bottom extension, use tight_layout() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) plt.plot(df.time, df.speed) plt.tick_params(rotation=90) plt.show()OutputRead More

Shading an area between two points in a Matplotlib plot

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:25:29

7K+ Views

To shade an area between two points in matplotlib, we can take the following steps−Create x and y data points using numpy.Plot x and y data points, with color=red and linewidth=2.To shade an area parallel to X-axis, initialize two variables, y1 and y2.To add horizontal span across the axes, use axhspan() method with y1, y2, green as shade color, and alpha for transprency of the shade.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 20, 500) y = np.cos(3*x) + np.sin(2*x) plt.plot(x, y, c='red', lw=2) ... Read More

How can I set the background color on specific areas of a Pyplot figure using Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:28:27

2K+ Views

To set the background color on specific areas of a pyplot, 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.To color a specific portion of the plot, 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"] = ... Read More

How to set my xlabel at the end of X-axis in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:28:01

10K+ Views

To set the xlabel at the end of X-axis in matplotlib, we can take the following steps −Create data points for x using numpy.Using subplot() method, add a subplot to the current figure.Plot x and log(x) using plot() method.Set the label on X-axis using set_label() method, with fontsize=16, loc=left, and color=red.To set the xlabel at the end of X-axis, use the coordinates, x and y.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 x = np.linspace(1, 2, 5) ax = plt.subplot() ax.plot(x, np.log(x)) ax.set_xticks(x) label = ax.set_xlabel('X ->', fontsize=16, loc="left", c="red") ax.xaxis.set_label_coords(1.0, -0.025) plt.show()OutputRead More

How to draw axis in the middle of a figure in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:27:41

4K+ Views

To draw axis in the middle of a figure, we can take the following steps −Create x and sqr data points using numpy.Create a new figure, or activate an existing figure, using figure() method.Add an axis to the figure as a part of a subplot arrangement.Set the postion of left and bottom spines.Set the color of the right and top spines.Plot x and sqr, using plot() method, with label y=x2 and color=red.Place the legend using legend() method. Set the location at upper right corner.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ... Read More

Creating a graph with date and time in axis labels with Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:25:04

4K+ Views

To create a graph with date and time in axis labels, we can take the following steps−Create a figure and add a set of subplots.Create x and y data points using numpy.Set date formatter for X-axis.Plot x and y using plot() method.Set the ticks of X-axis.Set the date-time tick labels for X-axis, with some rotation.Make the plot tight layout using plt.tight_layout() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, dates import datetime import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i ... Read More

Advertisements