Python Matplotlib Plotting PieChart

This is our fifth article in Python Matplotlib, in this article we are going to learn about Matplotlib Plotting PieChart, so a pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportion. In a pie chart, the arc length of each slice, is proportional to the quantity it represents. you can read the previous articles for Python Matplotlib in the below links.

 

 

 

Matplotlib Tutorials For Beginners

1: Matplotlib Introduction & Installation 

2: Matplotlib Plotting Multiple Curves

3: Matplotlib Plotting Scatter (Points)

4: Matplotlib Plotting BarChart

 

 

 

 

So now let’s create our first PieChart in Matplotlib.

 

 

At the top first we have imported our matplotlib library, after that the pyplot.pie() function simply takes a list of values as the input. Note that the input data is a list; it could be a NumPy array. you do not have to adjust the data so that it adds up to 1 or 100. You just have to give values to matplotlib and it will automatically compute the relative areas of the pie chart.

 

 

 

 

 

If you run the code this will be the result

Python Matplotlib Plotting PieChart
Python Matplotlib Plotting PieChart

 

 

 

 

 

Customizing Color of PieChart

Also you can change the color in the piechart, pie charts are also used in contexts where the color scheme might matter a lot. Pie chart coloring works mostly like in bar charts. In this example, we are going to see how to color pie charts with our own colors.

 

 

 

Pie charts accept a list of colors using the colors parameter (beware, it is colors, not  color). However, the color list does not have as many elements as the input list of values. If there are less colors than values, then pyplot.pie() will simply cycle through the color list. In the preceding example, we gave a list of four colors to color a pie chart that consisted of four values. but you can add more values, and the pyplot.pie() function cycle through the color list.

 

 

 

 

So if you run the code this will be the result

Python Matplotlib Plotting PieChart
Python Matplotlib Plotting PieChart

 

 

 

 

Also you can give title to your PieChart,

 

OK in the above code, we have created our data, after that also we have created a color set to give different colors for piechart slices. also we want to explode the first and fourth slice in our PieChart.

 

 

 

 

 

If you run the code this will be the result

Matplotlib Customizing PieChart Color
Matplotlib Customizing PieChart Color

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×