相关疑难解决方法(0)

保存pandas.Series直方图图到文件

在ipython Notebook中,首先创建一个pandas Series对象,然后通过调用实例方法.hist(),浏览器显示该图.

我想知道如何将这个数字保存到文件(我的意思是不是通过右键单击并另存为,但脚本中需要的命令).

python histogram pandas

58
推荐指数
3
解决办法
7万
查看次数

使用matplotlib的savefig保存从python pandas生成的图(AxesSubPlot)

我正在使用pandas从数据框生成一个图,我想将其保存到文件中:

dtf = pd.DataFrame.from_records(d,columns=h)
fig = plt.figure()
ax = dtf2.plot()
ax = fig.add_subplot(ax)
fig.savefig('~/Documents/output.png')
Run Code Online (Sandbox Code Playgroud)

似乎最后一行,使用matplotlib的savefig,应该可以解决问题.但该代码产生以下错误:

Traceback (most recent call last):
  File "./testgraph.py", line 76, in <module>
    ax = fig.add_subplot(ax)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 890, in add_subplot
    assert(a.get_figure() is self)
AssertionError
Run Code Online (Sandbox Code Playgroud)

或者,尝试直接在绘图上调用savefig也会出错:

dtf2.plot().savefig('~/Documents/output.png')


  File "./testgraph.py", line 79, in <module>
    dtf2.plot().savefig('~/Documents/output.png')
AttributeError: 'AxesSubplot' object has no attribute 'savefig'
Run Code Online (Sandbox Code Playgroud)

我想我需要以某种方式将plot()返回的子图添加到图中以便使用savefig.我也想知道如果这或许与该做魔术的AxesSubPlot类的后面.

编辑:

以下作品(没有提出任何错误),但留下了一个空白页面图像....

fig = plt.figure()
dtf2.plot()
fig.savefig('output.png')
Run Code Online (Sandbox Code Playgroud)

python matplotlib pandas

50
推荐指数
5
解决办法
7万
查看次数

标签 统计

pandas ×2

python ×2

histogram ×1

matplotlib ×1