我对matplotlib
Python 模块有一个未解决的大问题。
如果我创建一个名为[Figure1]
2 个轴的[Ax1, Ax2]
图形和另一个图形[Figure2]
,是否有一个函数或方法可以让我从中导出Ax1
对象Figure1
并将其重绘到Figure2
对象?
我对matplotlib
Python 模块有一个未解决的大问题。
如果我创建一个名为[Figure1]
2 个轴的[Ax1, Ax2]
图形和另一个图形[Figure2]
,是否有一个函数或方法可以让我从中导出Ax1
对象Figure1
并将其重绘到Figure2
对象?
一般来说,轴绑定到一个图形。原因是,matplotlib 通常会在后台执行一些操作,以使它们在图中看起来不错。
有一些解决这个问题的方法,还有这个,但普遍的共识似乎是应该避免尝试复制轴。
另一方面,这根本不需要成为问题或限制。
您始终可以定义一个执行绘图的函数并在几个图形上使用它,如下所示:
import matplotlib.pyplot as plt
def plot1(ax, **kwargs):
x = range(5)
y = [5,4,5,1,2]
ax.plot(x,y, c=kwargs.get("c", "r"))
ax.set_xlim((0,5))
ax.set_title(kwargs.get("title", "Some title"))
# do some more specific stuff with your axes
#create a figure
fig, (ax1, ax2) = plt.subplots(1,2)
# add the same plot to it twice
plot1(ax1)
plot1(ax2, c="b", title="Some other title")
plt.savefig(__file__+".png")
plt.close("all")
# add the same plot to a different figure
fig, ax1 = plt.subplots(1,1)
plot1(ax1)
plt.show()