如何将 pandas 饼图保存到文件? [英] How to save Pandas pie plot to a file?
本文介绍了如何将 pandas 饼图保存到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
import pandas as pd
import matplotlib
matplotlib.style.use('ggplot')
df = pd.DataFrame({ 'sample1':['foo','bar','bar','qux'], 'score':[5,9,1,7]})
sum_df = df.groupby("sample1").sum()
pie = sum_df.plot(kind="pie", figsize=(6,6), legend = False, use_index=False, subplots=True, colormap="Pastel1")
制作饼图.然后,我要做的就是将其保存到文件中. 但是为什么失败了?
Which makes the pie chart. What I want to do then is to save it to a file. But why this fail?
fig = pie.get_figure()
fig.savefig("~/Desktop/myplot.pdf")
我收到此错误:
'numpy.ndarray' object has no attribute 'get_figure'
推荐答案
Well pie
is a numpy array because the return type for DataFrame.plot()
is a numpy array of matplotlib.AxesSubplot
objects.
fig = pie[0].get_figure()
fig.savefig("~/Desktop/myplot.pdf")
这篇关于如何将 pandas 饼图保存到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文