Found 1034 Articles for Matplotlib

In Matplotlib, show the percentage or proportional data where each slice of pie represents a category

Rishikesh Kumar Rishi
Updated on 26-Oct-2021 13:02:12

2K+ Views

In this article, we can create a pie chart to show our daily activities, i.e., sleeping, eating, working, and playing. Using plt.pie() method, we can create a pie chart with the given different data sets for different activities.StepsCreate a list of days, i.e., [1, 2, 3, 4, 5]. Similarly, make lists for sleeping, eating, playing, and working. There is an activities list that keeps “sleeping”, “eating”, “working” and “playing”.Make a list of colors.Use plt.pie() method to draw the pie chart, where slices, activities, colors as cols, etc. are passed.Set a title for the axes, i.e., “Pie Chart”.To show the figure ... Read More

Show only certain items in legend Python Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:51:23

1K+ Views

Using plt.legend(), we can add or show certain items just by putting the values in the list.StepsSet the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Plot the lines using the lists that are passed in the plot() method argument.Location and legend_drawn flags can help to find a location and make the flag True for border.Set the legend with “blue” and “orange” elements.To show the figure use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.ylabel("Y-axis ") plt.xlabel("X-axis ") plt.plot([9, 5], [2, 5], [4, 7, 8]) location = 0 # For the best location legend_drawn_flag = True plt.legend(["blue", ... Read More

How to turn on minor ticks only on the y-axis Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:02:26

1K+ Views

First, we can create fig, ax using subplots() and then, we can plot the lines. After that, using ax.yaxis.set_minor_locator(tck.AutoMinorLocator()), we can turn on the minor ticks.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Plot the line using two lists.Set the locator of the minor ticker.Dynamically find minor tick positions based on the positions of major ticks. The scale must be linear with major ticks evenly spaced.Using plt.show() method, we can show the figure.Exampleimport matplotlib.pyplot as plt import matplotlib.ticker as tck fig, ax = plt.subplots() plt.plot([0, 2, 4], [3, 6, 1]) ax.yaxis.set_minor_locator(tck.AutoMinorLocator()) plt.show()OutputRead More

How to add a second X-axis in Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:48:18

9K+ Views

We can use the twiny() method to create a second X-axis. Similarly, using twinx, we can create a shared Y-axis.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Plot line with lists passed in the argument of plot() method with color="red".Create a twin of Axes with a shared Y-axis but independent X-axis.Plot the line on ax2 that is created in step 3.Adjust the padding between and around subplots.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt fig, ax1 = plt.subplots() ax1.plot([1, 2, 3, 4, 5], [3, 5, 7, 1, 9], color='red') ax2 = ax1.twiny() ... Read More

What does .shape[] do in “for i in range(Y.shape[0])” using Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:45:39

843 Views

The shape property is usually used to get the current shape of an array, but it may also be used to reshape the array in-place by assigning a tuple of array dimensions to it.StepsGet an array Y using np.array method.Y.shape would return a tuple (4, ).Y.shape[0] method would return 4, i.e., the first element of the tuple.Exampleimport numpy as np Y = np.array([1, 2, 3, 4]) print("Output of .show method would be: ", Y.shape, " for ", Y) print("Output of .show[0] method would be: ", Y.shape[0], " for ", Y) print("Output for i in range(Y.shape[0]): ", end=" ") for ... Read More

Multiple axes in Matplotlib with different scales

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:55:01

4K+ Views

In the following code, we will see how to create a shared Y-axis.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Plot line with lists passed in the argument of plot() method with color="red".Create a twin of Axes with a shared X-axis but independent Y-axis.Plot the line on ax2 that is created in step 3.Adjust the padding between and around subplots.To show the figure use plt.show() method.Exampleimport matplotlib.pyplot as plt fig, ax1 = plt.subplots() ax1.plot([1, 2, 3, 4, 5], [3, 5, 7, 1, 9], color='red') ax2 = ax1.twinx() ax2.plot([11, 12, 31, 41, 15], [13, 51, ... Read More

Prevent scientific notation in matplotlib.pyplot

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:53:27

14K+ Views

To prevent scientific notation, we must pass style='plain' in the ticklabel_format method.StepsPass two lists to draw a line using plot() method.Using ticklabel_format() method with style='plain'. If a parameter is not set, the corresponding property of the formatter is left unchanged. Style='plain' turns off scientific notation.To show the figure, use plt.show() method.Examplefrom matplotlib import pyplot as plt plt.plot([1, 2, 3, 4, 5], [11, 12, 13, 14, 15]) plt.ticklabel_format(style='plain')    # to prevent scientific notation. plt.show()Output

Manually add legend Items Python Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:52:12

21K+ Views

Using plt.legend() method, we can create a legend, and passing frameon would help to keep the border over there.StepsSet the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Draw lines using plot() method.Location and legend drawn flags can help to find a location and make the flag True for the border.Set the legend with “blue” and “orange” elements.To show the figure use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.ylabel("Y-axis ") plt.xlabel("X-axis ") plt.plot([9, 5], [2, 5], [4, 7, 8]) location = 0 # For the best location legend_drawn_flag = True plt.legend(["blue", "orange"], loc=0, frameon=legend_drawn_flag) plt.show()OutputRead More

Make 3D plot interactive in Jupyter Notebook (Python & Matplotlib)

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:58:03

3K+ Views

In this article, we can take a program code to show how we can make a 3D plot interactive using Jupyter Notebook.StepsCreate a new figure, or activate an existing figure.Create fig and ax variables using subplots method, where default nrows and ncols are 1, projection=’3d”.Get x, y and z using np.cos and np.sin function.Plot the 3D wireframe, using x, y, z and color="red".Set a title to the current axis.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j] x = np.cos(u) * ... Read More

How to zoom subplots together in Matplotlib/Pyplot?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:01:02

2K+ Views

We can use the attribute sharex = "ax1", and then, use the subplot method to zoom the subplots together.StepsAdd a subplot to the current figure with (nrow = 1, ncols = 2, index = 1).Add line on the current subplot with (nrow = 1, ncols = 2, index = 1).Add a subplot to the current figure with (nrow = 1, ncols = 2, index = 2).Add line on the current subplot with (nrow = 1, ncols = 2, index = 2), where sharex can help to share the x or y `~matplotlib.axis` with sharex and/or sharey. The axis will have ... Read More

Advertisements