2014-01-30 59 views
33

在Matlab的hold on的matplotlib中是否有顯式的等價命令?我試圖在同一軸上繪製所有的圖形。一些圖表是一個for循環中產生的,而這些都是從susl分別畫在:相當於在Matlab中保留的Python

import numpy as np 
import matplotlib.pyplot as plt 

for i in np.arange(1,5): 
    z = 68 + 4 * np.random.randn(50) 
    zm = np.cumsum(z)/range(1,len(z)+1) 
    plt.plot(zm) 
    plt.axis([0,50,60,80]) 

plt.show() 

n = np.arange(1,51) 
su = 68 + 4/np.sqrt(n) 
sl = 68 - 4/np.sqrt(n) 

plt.plot(n,su,n,sl) 

plt.axis([0,50,60,80]) 
plt.show() 
+6

你的意思是'plt.hold(True)'? –

+0

[多重圖中一個蟒蛇圖]可能的重複(http://stackoverflow.com/questions/21254472/multiple-plot-in-one-figure-in-python/21254861) – Kraay89

+0

@ Cody Piersall - 它可能適用於其他人,但顯然不適合我的例子。 –

回答

28

就叫plt.show()末:

import numpy as np 
import matplotlib.pyplot as plt 

plt.axis([0,50,60,80]) 
for i in np.arange(1,5): 
    z = 68 + 4 * np.random.randn(50) 
    zm = np.cumsum(z)/range(1,len(z)+1) 
    plt.plot(zm)  

n = np.arange(1,51) 
su = 68 + 4/np.sqrt(n) 
sl = 68 - 4/np.sqrt(n) 

plt.plot(n,su,n,sl) 

plt.show() 
+1

注意:這不起作用來自命令行的交互式輸入的情況。如果在腳本中運行,則工作正常。 – ZZZ

19

您可以使用以下方法:

plt.hold(True) 
+2

此功能已棄用,這樣的任何替代解決方案? – Jyotirmay

1

默認情況下hold on功能在matplotlib.pyplot中打開。因此,每次您在plt.show()之前引用plt.plot()時,都會在圖中添加圖紙。在函數plt.show()導致重繪整個圖片後啓動plt.plot()

0

檢查pyplot文檔。爲了完整性,

import numpy as np 
import matplotlib.pyplot as plt 

#evenly sampled time at 200ms intervals 
t = np.arange(0., 5., 0.2) 

# red dashes, blue squares and green triangles 
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') 
plt.show()