Python Matplotlib 返回一个绘图对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43925337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib returning a plot object
提问by Simon
I have a function that wraps pyplot.plt
so I can quickly create graphs with oft-used defaults:
我有一个包装函数,pyplot.plt
因此我可以使用常用的默认值快速创建图形:
def plot_signal(time, signal, title='', xlab='', ylab='',
line_width=1, alpha=1, color='k',
subplots=False, show_grid=True, fig_size=(10, 5)):
# Skipping a lot of other complexity here
f, axarr = plt.subplots(figsize=fig_size)
axarr.plot(time, signal, linewidth=line_width,
alpha=alpha, color=color)
axarr.set_xlim(min(time), max(time))
axarr.set_xlabel(xlab)
axarr.set_ylabel(ylab)
axarr.grid(show_grid)
plt.suptitle(title, size=16)
plt.show()
However, there are times where I'd want to be able to return the plot so I can manually add/edit things for a specific graph. For example, I want to be able to change the axis labels, or add a second line to the plot after calling the function:
但是,有时我希望能够返回绘图,以便我可以手动添加/编辑特定图形的内容。例如,我希望能够在调用函数后更改轴标签,或在绘图中添加第二行:
import numpy as np
x = np.random.rand(100)
y = np.random.rand(100)
plot = plot_signal(np.arange(len(x)), x)
plot.plt(y, 'r')
plot.show()
I've seen a few questions on this (How to return a matplotlib.figure.Figure object from Pandas plot function?and AttributeError: 'Figure' object has no attribute 'plot') and as a result I've tried adding the following to the end of the function:
我已经看到了一些关于此的问题(How to return a matplotlib.figure.Figure object from Pandas plot function?and AttributeError: 'Figure' object has no attribute 'plot'),因此我尝试添加以下内容到函数的结尾:
return axarr
return axarr.get_figure()
return plt.axes()
return axarr
return axarr.get_figure()
return plt.axes()
However, they all return a similar error: AttributeError: 'AxesSubplot' object has no attribute 'plt'
但是,它们都返回类似的错误: AttributeError: 'AxesSubplot' object has no attribute 'plt'
Whats the correct way to return a plot object so it can be edited later?
返回绘图对象以便以后可以编辑的正确方法是什么?
采纳答案by ImportanceOfBeingErnest
I think the error is pretty self-explanatory. There is no such thing as pyplot.plt
, or similar. plt
is the quasi standard abbreviated form of pyplot when being imported, i.e. import matplotlib.pyplot as plt
.
我认为这个错误是不言自明的。没有这样的东西pyplot.plt
,或者类似的东西。plt
是 pyplot 导入时的准标准缩写形式,即import matplotlib.pyplot as plt
.
Concerning the problem, the first approach, return axarr
is the most versatile one. You get an axes, or an array of axes, and can plot to it.
关于这个问题,第一种方法return axarr
是最通用的方法。您可以获得一个轴或一组轴,并且可以对其进行绘图。
The code may look like
代码可能看起来像
def plot_signal(x,y, ..., **kwargs):
# Skipping a lot of other complexity her
f, ax = plt.subplots(figsize=fig_size)
ax.plot(x,y, ...)
# further stuff
return ax
ax = plot_signal(x,y, ...)
ax.plot(x2, y2, ...)
plt.show()