2016-08-18 42 views
1

我在Win7機器中使用Canopy,使用%pylab作爲後端啓用和交互(Qt4)。恕我直言,我得到了我認爲是奇怪的行爲matplotlib在matplotlib中顯示圖的內容inmmediately

如果代碼是逐行執行的,則圖形的框架顯示爲我所期望的,但不是圖形本身的內容。如果在繪圖之後,我需要關於這些圖表的信息,因爲我看不到它們,我無法正確回答。一旦我用虛擬答案回答問題,圖表就會出現。

我想實現的是圖表顯示問題將被問到,以便有信息回覆。

在此先感謝。

這是一個MWE

import numpy as np 
import matplotlib.pyplot as plt 

N = 8 
y = np.zeros(N) 
x1 = np.linspace(0, 10, N, endpoint=True) 
x2 = np.linspace(0, 10, N, endpoint=False) 
plt.figure() 
plt.plot(x1, y, 'o') 
plt.plot(x2, y + 0.5, 'o') 
plt.ylim([-0.5, 1]) 
plt.show() 


y1 = np.random.random(8) 
plt.figure() 
plt.plot(x1, y1) 
plt.show() 

dummy = raw_input("What is the third point in the second graph?") 

編輯:如果我改變從互動(Qt4的)在樹冠後端互動(WX),它按預期工作。

+0

退房http://stackoverflow.com/questions/5524858/matplotlib-show-doesnt-work-twice –

回答

1

如果我明白了,問題是plt.show會阻塞,第二個數字將不會被繪製,直到第一個被關閉。對於不同的後端,行爲可能會有所不同,但不應多次呼叫節目(請參閱matplotlib show() doesn't work twice)。我建議在這裏使用兩個subplots,也許可以將raw_input塊關閉,並且可以輸入一個顯示數字的輸入。你的代碼會看起來像這樣,

import numpy as np 
import matplotlib.pyplot as plt 

N = 8 
y = np.zeros(N) 
x1 = np.linspace(0, 10, N, endpoint=True) 
x2 = np.linspace(0, 10, N, endpoint=False) 
fig,ax = plt.subplots(2,1) 
ax[0].plot(x1, y, 'o') 
ax[0].plot(x2, y + 0.5, 'o') 
ax[0].set_ylim([-0.5, 1]) 

y1 = np.random.random(8) 
ax[1].plot(x1, y1) 
plt.show(block=False) 

dummy = raw_input("What is the third point in the second graph?") 
print("dummy = ", dummy) 

它適合我。

+0

感謝您的回答。不幸的是,它在我的情況下都不起作用。另一方面,如果我將Canopy中的後端從交互式(Qt4)更改爲交互式(wx),那麼您的代碼和我的代碼都可以工作! –

1

正如艾德史密斯所說,只有第一次致電plt.show()的作品。 如果要強制的身影重繪,你可以使用figure.canvas.draw()

import matplotlib.pyplot as plt 

fig = plt.figure() 
plt.plot([0, 1], [2, 3]) 
plt.show() 

#do more stuff, get user input 

plt.plot([5,6], [-7, -8]) 
fig.canvas.draw() 

enter image description here