HomeLinuxMatplotlib Pie Chart

Matplotlib Pie Chart


Python has a number of packages for information visualization, and “matplotlib” is one among them that’s most most well-liked. It may also be utilized for displaying each easy and complex graphs with a minimal piece of code. These graphs may be modified with the assistance of quite a few strategies.

The pie chart is a quantitative structure that might solely show one dataset at a time. The entire proportion of the supplied information is represented by the realm of the graph. For that function, the “pie()” operate may be utilized from Python’s “matplotlib” library.

The outcomes from this publish are:

Methods to Generate a Pie Chart in Python?

To generate a pie chart in Python, first, import the “matplotlib.pyplot” library:

import matplotlib.pyplot as plt

 

Then, create information for axes and put it aside into the variables. For example, we’ve created labels and values. After that, handed them to the “days” and “duty_hours” variables:

days = [“Mon”, “Tue”, “Wed”, “Thur”, “Fri”, “Sat”]
duty_hours = [12, 22, 16, 38, 12, 17]

 

Now, use the “plt.subplot()” operate for making a determine and grid it with a single name, whereas specifying affordable management over how the person plots are generated. Then, invoke the “pie()” technique together with the required parameters, akin to “duty_hours” as a worth and “labels =days”:

fig, ay = plt.subplots()
ay.pie(duty_hours, labels = days)

 

Now, apply the “set_title()” technique to set the title of the chart. Then, name the “plt.present()” technique for displaying the resultant pie chart:

ay.set_title(“Pie Chart”)
plt.present()

 

Within the below-provided output, it may be seen that the pie chart has been created efficiently with labels:

Methods to Set Pie Chart Begin Angle and Auto Share in Python?

To set the beginning angle of the pie chart in line with the will, use the “startangle” parameter contained in the “pie()” technique. Then, add the “autopct” argument with worth, akin to “%.0f%%”. As follows:

ay.pie(duty_hours, labels = days, autopct=‘%.0f%%’, startangle = 40)

 

It may be seen that the beginning angle of the primary slice is “40” levels and the odds for every slice have been displayed efficiently:

Methods to Explode Slices of Pie Chart in Python?

If you wish to explode one or a number of slices of the pie chart, go an array of the size of the supplied information, and use the “explode” parameter. Right here, we’ve created an array with one worth representing a worth for each wedge of the pie chart having the identical size because the given information and handed it to the “my_ex” variable. Then, add the “explode” argument and go the “my_ex” variable as its worth:

my_ex = [0.4, 0, 0, 0, 0, 0.1]
ay.pie(duty_hours, labels = days, autopct=‘%.0f%%’, explode = my_ex)

 

Because of this, the desired wedge of the pie chart has exploded:

That’s it! We have now defined the matplotlib pie chart in Python.

Conclusion

Python has a number of packages for information visualization, and “matplotlib” is one among them that’s most most well-liked. By utilizing this library, we are able to create a pie chart which is a quantitative structure that solely shows a single dataset at a time. The “pie()” operate may be utilized from Python’s “matplotlib” library. This publish demonstrated the pie chart utilizing Python’s matplotlib library.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments