我有一个包装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()
但是,有时候我希望能够返回图表,以便我可以手动添加/编辑特定图形的内容.例如,我希望能够更改轴标签,或者在调用函数后向图中添加第二行:
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()
>返回axarr
> return axarr.get_figure()
> return plt.axes()
但是,它们都返回一个类似的错误:AttributeError:’AxesSubplot’对象没有属性’plt’
什么是返回绘图对象的正确方法,以便以后编辑?