Python图和轴对象

时间:2016-11-24 20:54:53

标签: python matplotlib figures

我有一个关于matplotlib Python模块的未解决的大问题。

如果我创建一个名为[Figure1]的图形,其中包含2个轴[Ax1, Ax2],另一个图形[Figure2],是否有一个函数或方法可以导出{{1}来自Ax1的对象并将其重绘为Figure1对象?

1 个答案:

答案 0 :(得分:0)

通常,轴与图形绑定。原因是,matplotlib通常在后台执行一些操作,使它们在图中看起来很漂亮。

some hacky ways around this,还有this one,但普遍的共识似乎是应该避免尝试复制轴。

另一方面,这不一定是问题或限制。

你总是可以定义一个绘制函数的函数,并在几个数字上使用它,如下所示:

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()
相关问题