我有一个包裹pyplot.plt
这样我就可以快速创建与经常使用的默认图形功能:Matplotlib返回剧情对象
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()
然而,有次在那里我希望能够返回所以我可以手动添加/编辑特定图形的东西。举例来说,我希望能够改变轴标签,或者第二行添加到情节调用函数后:
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()
我已经看到了这个(How to return a matplotlib.figure.Figure object from Pandas plot function?和AttributeError: 'Figure' object has no attribute 'plot')几个问题,如因此我试着添加以下的函数的末尾:
return axarr
return axarr.get_figure()
return plt.axes()
然而,他们都将返回一个类似的错误:AttributeError: 'AxesSubplot' object has no attribute 'plt'
请告诉我返回一个情节对象,以便以后可以编辑的正确方法是什么?
你试图返回'图= plt.gcf()'? –
除了调用'plot.plt()'而不是'plot.plot()'外,所有内容都是正确的。愚蠢的错误;可以发生在每个人:) –