Plotting Pandas DataFrames in to Pie Charts using matplotlib

雨燕双飞 提交于 2019-11-28 01:05:19

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21090316/plotting-pandas-dataframes-in-to-pie-charts-using-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!