将pandas.Series直方图保存到文件 [英] save a pandas.Series histogram plot to file

查看:292
本文介绍了将pandas.Series直方图保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In ipython Notebook, first create a pandas Series object, then by calling the instance method .hist(), the browser displays the figure.

我想知道如何将该图形保存到文件中(不是通过右键单击另存为,而是脚本中所需的命令).

I am wondering how to save this figure to a file (I mean not by right click and save as, but the commands needed in the script).

推荐答案

使用Figure.savefig()方法,如下所示:

ax = s.hist()  # s is an instance of Series
fig = ax.get_figure()
fig.savefig('/path/to/figure.pdf')

它不必以pdf结尾,有很多选择.查看文档.

It doesn't have to end in pdf, there are many options. Check out the documentation.

或者,您可以使用pyplot界面,只需调用savefig作为函数来保存最近创建的图形:

Alternatively, you can use the pyplot interface and just call the savefig as a function to save the most recently created figure:

import matplotlib.pyplot as plt
s.hist()
plt.savefig('path/to/figure.pdf')  # saves the current figure

这篇关于将pandas.Series直方图保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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