bho*_*ard 50 python matplotlib pandas
我正在使用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)
小智 90
gcf方法在V 0.14中进行了描述,以下代码适用于我:
plot = dtf.plot()
fig = plot.get_figure()
fig.savefig("output.png")
Run Code Online (Sandbox Code Playgroud)
joe*_*lom 24
您可以ax.figure.savefig()
按照对问题的评论中的建议使用:
import pandas as pd
df = pd.DataFrame([0, 1])
ax = df.plot.line()
ax.figure.savefig('demo-file.pdf')
Run Code Online (Sandbox Code Playgroud)
ax.get_figure().savefig()
与其他答案中的建议相比,这没有实际好处,因此您可以选择您认为最美观的选项。事实上,get_figure()
只需返回self.figure
:
# Source from snippet linked above
def get_figure(self):
"""Return the `.Figure` instance the artist belongs to."""
return self.figure
Run Code Online (Sandbox Code Playgroud)
bho*_*ard 14
所以我不完全确定为什么会这样,但它会保存我的情节图像:
dtf = pd.DataFrame.from_records(d,columns=h)
dtf2.plot()
fig = plt.gcf()
fig.savefig('output.png')
Run Code Online (Sandbox Code Playgroud)
我猜我原始帖子的最后一个片段保存了空白,因为这个数字永远不会得到熊猫生成的轴.使用上面的代码,图形对象通过gcf()调用(获取当前图形)从某个魔术全局状态返回,它自动烘焙在上面的线条中绘制的轴.
小智 9
对于我来说,使用plt.savefig()
函数后的plot()
函数似乎很容易:
import matplotlib.pyplot as plt
dtf = pd.DataFrame.from_records(d,columns=h)
dtf.plot()
plt.savefig('~/Documents/output.png')
Run Code Online (Sandbox Code Playgroud)
numpy.ndarray
的matplotlib.axes.Axes
import pandas as pd
import seaborn as sns # for sample data
import matplotlib.pyplot as plt
# load data
df = sns.load_dataset('iris')
# display(df.head())
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
Run Code Online (Sandbox Code Playgroud)
pandas.DataFrame.plot()
kind='hist'
, 但在指定除'hist'
[0]
获取其中之一axes
,并使用 提取图形.get_figure()
。fig = df.plot(kind='hist', subplots=True, figsize=(6, 6))[0].get_figure()
plt.tight_layout()
fig.savefig('test.png')
Run Code Online (Sandbox Code Playgroud)
pandas.DataFrame.hist()
df.hist
给Axes
created with plt.subplots
,并保存它fig
。4
和1
分别用于nrows
和ncols
,但也可以使用其他配置,例如2
和2
。fig, ax = plt.subplots(nrows=4, ncols=1, figsize=(6, 6))
df.hist(ax=ax)
plt.tight_layout()
fig.savefig('test.png')
Run Code Online (Sandbox Code Playgroud)
.ravel()
扁平化的阵列Axes
fig = df.hist().ravel()[0].get_figure()
plt.tight_layout()
fig.savefig('test.png')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
70737 次 |
最近记录: |