2016-11-24 74 views
-1

我有一個關於matplotlib Python模塊的懸而未決的大問題。Python圖形和軸對象

如果我創建了一個名爲[Figure1]人物,2個軸[Ax1, Ax2],和另一個數字[Figure2],是有一個函數或方法,讓我到Ax1對象從Figure1出口,重新繪製到Figure2對象?

回答

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()