2012-12-10 38 views
0

我想繪製一組軸上的多個圖。在matplotlib內循環

我有一個數據的二維數組,並希望將其分解爲111 1D數組並繪製它們。下面是我的代碼示例至今:

from numpy import * 
import matplotlib.pyplot as plt 

x = linspace(1, 130, 130) # create a 1D array of 130 integers to set as the x axis 
y = Te25117.data # set 2D array of data as y 
plt.plot(x, y[1], x, y[2], x, y[3]) 

此代碼工作正常,但我看不到編寫循環的方式將它的情節本身內循環。如果我每次明確寫入1到111的數字,我只能使代碼工作,這並不理想! (我需要循環的數字範圍是1到111.)

+2

歡迎來到StackOverflow!請你可以給一個[簡單的例子](http://sscce.org/)數據集,這樣我們可以粘貼並去(而不是嘗試創建一個滿足你的標準):) –

回答

3

讓我猜...長時間的matlab用戶? Matplotlib會自動將線圖添加到當前繪圖,如果您不創建一個新圖。所以,你的代碼可以是簡單的:

from numpy import * 
import matplotlib.pyplot as plt 

x = linspace(1, 130, 130) # create a 1D array of 130 integers to set as the x axis 
y = Te25117.data # set 2D array of data as y 
L = len(y) # I assume you can infere the size of the data in this way... 
#L = 111 # this is if you don't know any better 
for i in range(L) 
    plt.plot(x, y[i], color='mycolor',linewidth=1) 
0
import numpy as np 
import matplotlib.pyplot as plt 
x = np.array([1,2]) 
y = np.array([[1,2],[3,4]]) 

In [5]: x 
Out[5]: array([1, 2]) 

In [6]: y 
Out[6]: 
array([[1, 2], 
     [3, 4]]) 

In [7]: for y_i in y: 
    ....:  plt.plot(x, y_i) 

將在一個圖繪製這些。