2013-07-23 127 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.