我有以下代码:
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")
制作饼图。然后我想做的是将它保存到一个文件中。 但是为什么会失败呢?
fig = pie.get_figure()
fig.savefig("~/Desktop/myplot.pdf")
我收到这个错误:
'numpy.ndarray' object has no attribute 'get_figure'
最佳答案
pie
是一个 numpy 数组,因为 DataFrame.plot()
的返回类型是 matplotlib.AxesSubplot
对象的 numpy 数组。
fig = pie[0].get_figure()
fig.savefig("~/Desktop/myplot.pdf")
关于python - 如何将 Pandas 饼图保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32219350/