2013-07-23 151 views
0

我對兩種情況做了一個循環,對於每種情況,我嘗試做一個情節。如何用matplotlib繪製多個圖像?

for col_name in ['col2','col3']: 
    x_min = min(df['col1'].min(), df[col_name].min()) 
    x_max = max(df['col1'].max(), df[col_name].max()) 
    plt.xlim([x_min,x_max]) 
    plt.ylim([x_min,x_max]) 
    plt.axes().set_aspect('equal') 
    plt.scatter(df['col1'], df[col_name]) 

結果我得到了我的IPython的筆記本一個情節。有誰知道如何克服這個問題?

+0

見http://stackoverflow.com/questions/14254379/how-can-i-attach-a-pyplot-function-to-a-figure-instance/14261698#14261698。您正在使用狀態機界面,您可能想要使用OO界面。 – tacaswell

回答

2

你需要調用一次figure()更多。

for col_name in ['col2','col3']: 
    plt = figure() #This gives you a new figure to plot in 
    x_min = min(df['col1'].min(), df[col_name].min()) 
    x_max = max(df['col1'].max(), df[col_name].max()) 
    plt.xlim([x_min,x_max]) 
    plt.ylim([x_min,x_max]) 
    plt.axes().set_aspect('equal') 
    plt.scatter(df['col1'], df[col_name]) 
1

如果我想讓它們在不同的窗口上,我只會使用兩個數字。

像這樣的東西應該工作。

>>> for i in range(3): 
     xAxis = [randint(1, 5) for _ in range(10)] 
     plt.figure(1) 
     plt.plot(xAxis) 
     plt.show() 
     xAxis2 = [randint(1, 5) for _ in range(10)] 
     plt.figure(2) 
     plt.plot(xAxis2) 
     plt.show() 

它給了我連續六個數字。

因爲,每次迭代都需要一個新的數字。

for index, col_name in ['col2','col3']: 
    plt.figure(index) 
    # Do the plotting.