2015-09-25 209 views
0

我想在兩個不同的窗口中繪製兩個圖形,但我希望可以動態更新的圖形。使用pyplot繪製並刷新兩個獨立的窗口

這段代碼就是一個例子。

它爲什麼只繪製其中一個窗口?

如何解決?

謝謝

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
fig_norm = plt.figure() 
fig.show() 
fig_norm.show() 
ax_fig = fig.add_subplot(1, 1, 1) 
ax_fig_norm = fig.add_subplot(1, 1, 1) 
ax_fig.set_title("Figure 1", fontsize='large') 
ax_fig_norm.set_title("Figure Normalized", fontsize='large') 

plt.ion() 

while True: 
    x = np.random.rand(100) 
    y = np.random.rand(100) 
    ax_fig.plot(x, y) 
    ax_fig_norm.plot(x*3, y*3) 
    fig.canvas.draw() 
    fig_norm.canvas.draw() 

enter image description here

回答

1

的方法add_subplot建立在其上的呼籲圖中座標軸對象。

你調用它fig的兩倍,因此得到相同的軸對象。

請嘗試:

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
fig_norm = plt.figure() 
fig.show() 
fig_norm.show() 
ax_fig = fig.add_subplot(1, 1, 1) 
ax_fig_norm = fig_norm.add_subplot(1, 1, 1) 
ax_fig.set_title("Figure 1", fontsize='large') 
ax_fig_norm.set_title("Figure Normalized", fontsize='large') 

plt.ion() 

while True: 
    x = np.random.rand(100) 
    y = np.random.rand(100) 
    ax_fig.plot(x, y) 
    ax_fig_norm.plot(x*3, y*3) 
    fig.canvas.draw() 
    fig_norm.canvas.draw()