如何将 Pandas 饼图保存到文件中? [英] How to save Pandas pie plot to a file?

查看:35
本文介绍了如何将 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'

推荐答案

好吧 pie 是一个 numpy 数组,因为 DataFrame.plot()matplotlib.AxesSubplot<的 numpy 数组/code> 对象.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆