A Pie Chart In Python

Here is some (experimental) work I have done in the matplotlib package in Python.

 


Sections


Sample Data: Favourite Foods Survey

For this part, I have created some sample (fake) data on a favourite foods survey. The choices are pasta, pizza, sushi and steak with the associated counts of 15, 12, 13 and 20. The colours for the upcoming pie chart pieces are lightblue, yellow, orange and lightgreen.

 


Plotting A Pie Chart With Labels

The main function for creating pie charts from matplotlib is the .pie() function. The user needs to input the data, the labels, colours, the starting angle and whether or not to enable the shadow portion.

A title is added with the use of plt.title() and the plot is shown with the use of plt.show().

 

This pie chart looks clean and crisp for the most part. It could be improved upon by including the counts with the percentages. (Percentages alone can be misleading.)

To include the numbers inside the labels, a new autopct function needs to be made. (Source: https://stackoverflow.com/questions/6170246/how-do-i-use-matplotlib-autopct)

 

This pie chart now has the counts along with the percentages for each pie piece. It may have taken a bit more work and code to obtain the counts but it does help out the viewer.

 


References 

  • References: Python Data Visualization Cookbook
  • https://pythonspot.com/en/matplotlib-pie-chart/
  • https://stackoverflow.com/questions/6170246/how-do-i-use-matplotlib-autopct

2 thoughts on “A Pie Chart In Python

    1. dkmathstats Post author

      Hi there. Sorry for the late reply. From the top of my head, I think how I did the labels was based on one of the references in the post.

      Reply

Leave a Reply