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?

- 27,088
- 20
- 102
- 114

- 32,876
- 32
- 87
- 121
3 Answers
To plot a pie chart from a dataframe df
you can use Panda's plot.pie
:
df.plot.pie(y='column_name')
Example:
import pandas as pd
df = pd.DataFrame({'activity': ['Work', 'Sleep', 'Play'],
'hours': [8, 10, 6]})
df.set_index('activity', inplace=True)
print(df)
# hours
# activity
# Work 8
# Sleep 10
# Play 6
plot = df.plot.pie(y='hours', figsize=(7, 7))
Note that the labels of the pie chart are the index entries, this is the reason for using set_index
to set the index to activity
.
To style the plot, you can use all those arguments that can be passed to DataFrame.plot(), here an example showing percentages:
plot = df.plot.pie(y='hours', title="Title", legend=False, \
autopct='%1.1f%%', explode=(0, 0, 0.1), \
shadow=True, startangle=0)

- 27,088
- 20
- 102
- 114
-
2Funny how yours is the only one with an image. It also highlights other useful parameters. This should be the best answer. – Dave Liu Jul 26 '21 at 23:53
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.
-
1This solution is confusing and it seems you assume a number of things. What is df? What is Data? – Olu Adeyemo Feb 22 '19 at 22:36
import matplotlib.pyplot as plt
plt.pie(DataFrame([1,2,3]))
seems to work as expected. If the DataFrame has more than one column, it will raise.

- 34,073
- 6
- 70
- 63
-
1Thanks very much for your answer. It works fine. But how to put chart labels based on DataFrame column names? – Nilani Algiriyage Jan 14 '14 at 15:44
-
1Read the documentation for ``pie``, and use the DataFrame's ``.columns``. – Dan Allan Jan 14 '14 at 16:03