查看matplotlib文档,似乎添加AxesSubplot到a 的标准方法Figure是使用Figure.add_subplot:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
Run Code Online (Sandbox Code Playgroud)
我希望能够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)
Run Code Online (Sandbox Code Playgroud)
这是可能的matplotlib,如果可以,我该怎么做?
更新:我还没有设法解除Axes和Figures的创建,但是下面的答案中的示例可以很容易地在new或olf Figure实例中重用以前创建的轴.这可以通过一个简单的功能来说明:
def plot_axes(ax, fig=None, …Run Code Online (Sandbox Code Playgroud) 在使用ImportanceOfBeingErnest 的代码在轴之间移动艺术家时,我认为将它扩展到集合(例如由PathCollections生成plt.scatter)也很容易。没有这样的运气:
import matplotlib.pyplot as plt
import numpy as np
import pickle
x = np.linspace(-3, 3, 100)
y = np.exp(-x**2/2)/np.sqrt(2*np.pi)
a = np.random.normal(size=10000)
fig, ax = plt.subplots()
ax.scatter(x, y)
pickle.dump(fig, open("/tmp/figA.pickle", "wb"))
# plt.show()
fig, ax = plt.subplots()
ax.hist(a, bins=20, density=True, ec="k")
pickle.dump(fig, open("/tmp/figB.pickle", "wb"))
# plt.show()
plt.close("all")
# Now unpickle the figures and create a new figure
# then add artists to this new figure
figA = pickle.load(open("/tmp/figA.pickle", "rb"))
figB = pickle.load(open("/tmp/figB.pickle", "rb")) …Run Code Online (Sandbox Code Playgroud)