Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting Pandas DataFrames in to Pie Charts using matplotlib

Is it possible to print a DataFrame as a pie chart using matplotlib? The Pandas documentation on chart visualization has instructions for plotting lot of chart types including bar, histogram, scatter plot etc. But pie chart is missing?

like image 278
Nilani Algiriyage Avatar asked Jan 13 '14 11:01

Nilani Algiriyage


1 Answers

Pandas has this built in to the pd.DataFrame.plot(). All you have to do is use kind='pie' flag and tell it which column you want (or use subplots=True to get all columns). This will automatically add the labels for you and even do the percentage labels as well.

import matplotlib.pyplot as plt

df.Data.plot(kind='pie')

To make it a little more customization you can do this:

fig = plt.figure(figsize=(6,6), dpi=200)
ax = plt.subplot(111)

df.Data.plot(kind='pie', ax=ax, autopct='%1.1f%%', startangle=270, fontsize=17)

Where you tell the DataFrame that ax=ax. You can also use all the normal matplotlib plt.pie() flags as shown above.

like image 155
cmgerber Avatar answered Sep 17 '22 14:09

cmgerber