Programming Articles - Page 1206 of 3363

How do I change the range of the X-axis with datetimes in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:35:17

10K+ Views

To change the range of the X-axis with datetimes in matplotlib, we can take the following steps −Create a list of x and y, where x stores the datetime and y stores the number.Using subplots method, create a figure and add a set of subplots.Plot x and y data points using plots() method, wehere markerface color is green, marker edge color is red, and marker size is 7.Since date ticklabels often overlap, so it is useful to rorate them and right-align them using autofmt_xdate() method.To change the range of X-axis with datetimes, use set_xlim() with range of datetimes.To change the range of Y-axis, use set_ylim() method.To ... Read More

Determine Matplotlib axis size in pixels

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:34:35

3K+ Views

To determine the axis size in pixels, we can take the following steps −Create a figure and a set of subplots, using subplots() method, fig and ax.To get the DPI, use fig.dpi. Print the details.Find bounding box in the display box.Find the width and height, using bbox.width and bbox.height.Print the width and height.Examplefrom matplotlib import pyplot as plt fig, ax = plt.subplots() print("Dot per inch(DPI) for the figure is: ", fig.dpi) bbox = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted()) width, height = bbox.width, bbox.height print("Axis sizes are(in pixels):", width, height)OutputDot per inch(DPI) for the figure is: 100.0 Axis sizes are(in pixels): 4.96 3.696Read More

How to make a multicolored point in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:34:14

317 Views

To make a multicolored point in matplotlib, we can take the following steps−Initialize two varuables, x and y.Use scatter method with x and y data points with green color having marker size 2000.Use scatter method with x and y data points with red color having marker size 1000.Use scatter method with x and y data points with blue color having marker size 500.Use scatter method with x and y data points with white color having marker size 10.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, y = 0, ... Read More

How to add percentages on top of bars in Seaborn using Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:33:49

7K+ Views

To add percentages on top of bars in Seaborn, we can take the following steps −Create the lists, x, y and percentages to plot using Seaborn.Using barplot, show point estimates and confidence intervals with bars. Store the returned axis.Find patches from the returned axis (In step 2).Iterate the patches (returned in step 3).Find x and y from the patches to place the percentage value at the top of the bars.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = ['A', 'B', 'C', 'D', 'E'] y = [1, 3, 2, 0, ... Read More

How to decouple hatch and edge color in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:33:30

936 Views

To decouple hatch and edge color in matplotlib, we can use hatch color “o” and edge color “red”.−StepsCreate a new figure or activate existing figure.Add a subplot arrangement to the current axes.Create two lists of data points.Use bar() method with hatch and edgecolor.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax1 = fig.add_subplot(111) x = [3, 6, 1] y = [4, 6, 1] ax1.bar(x, y, color='black', edgecolor='red', hatch="o", lw=1., zorder=0) plt.show()Output

What does a 4-element tuple argument for 'bbox_to_anchor' mean in Matplotlib?

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

491 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

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

Advertisements