Set 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

Set X-axis Label at the End 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

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

4-Element Tuple Argument for bbox to Anchor in Matplotlib

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

478 Views

If a 4-tuple or B box Base is given, then it specifies the b box (x, y, width, height) that the legend is placed in.StepsCreate x and y data points using numpy.Plot x and y using plot() method, with label y=sin(x) and color=green.To place the legend at a specific location, use location 'upper left' and use legend box dimension with four tuples that was defined in the above description.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(-2, 2, 10) y = np.sin(x) plt.plot(x, ... Read More

Make Angles in Matplotlib Polar Plot Go Clockwise

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

Plot Over an Image Background in Python using Matplotlib

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

Shading 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

Creating a Graph with Date and Time in Axis Labels using 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

Specify Values on Y-Axis in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:24:32

12K+ Views

To specify values on Y-axis in Python, we can take the following steps−Create x and y data points using numpy.To specify the value of axes, create a list of characters.Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively.Plot the line using x and y, color=red, using plot() method.Make x and y margin 0.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.array([0, 2, 4, 6]) y = np.array([1, 3, 5, 7]) ticks = ... Read More

Advertisements