Generating pie charts

Now, to go back to the real world, we can remove XKCD mode by calling rcdefaults() on Matplotlib, and we can get back to normal mode here.

If you want a pie chart, all you have to do is call plt.pie and give it an array of your values, colors, labels, and whether or not you want items exploded, and if so, by how much. Here's the code:

# Remove XKCD mode: 
plt.rcdefaults() 
 
values = [12, 55, 4, 32, 14] 
colors = ['r', 'g', 'b', 'c', 'm'] 
explode = [0, 0, 0.2, 0, 0] 
labels = ['India', 'United States', 'Russia', 'China', 'Europe'] 
plt.pie(values, colors= colors, labels=labels, explode = explode) 
plt.title('Student Locations') 
plt.show() 

You can see in this code that I'm creating a pie chart with the values 12, 55, 4, 32, and 14. I'm assigning explicit colors to each one of those values, and explicit labels to each one of those values. I'm exploding out the Russian segment of the pie by 20%, and giving this plot a title of Student Locations and showing it. The following is the output you should see:

That's all there is to it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.138.184