【发布时间】:2011-09-12 15:51:55
【问题描述】:
查看matplotlib 文档,似乎将AxesSubplot 添加到Figure 的标准方法是使用Figure.add_subplot:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
我希望能够独立于图形创建类似AxesSubPlot 的对象,这样我就可以在不同的图形中使用它们。类似的东西
fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)
这在matplotlib 中是否可行,如果可以,我该怎么做?
更新:我还没有设法解耦轴和图形的创建,但是按照下面答案中的示例,可以轻松地在新的或 olf Figure 实例中重新使用以前创建的轴。这可以用一个简单的函数来说明:
def plot_axes(ax, fig=None, geometry=(1,1,1)):
if fig is None:
fig = plt.figure()
if ax.get_geometry() != geometry :
ax.change_geometry(*geometry)
ax = fig.axes.append(ax)
return fig
【问题讨论】:
-
您的函数 plot_axes 似乎不再起作用了。
标签: python matplotlib