0

I make a loop over two cases and for each case I try to make a plot.

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])

As a result I get one plot in my IPython notebook. Does anyone know how to overcome this problem?

Charles
  • 50,943
  • 13
  • 104
  • 142
Roman
  • 124,451
  • 167
  • 349
  • 456
  • See http://stackoverflow.com/questions/14254379/how-can-i-attach-a-pyplot-function-to-a-figure-instance/14261698#14261698 . You are using the state machine interface, you probably want to be using the OO interface. – tacaswell Jul 23 '13 at 15:28

2 Answers2

3

You need to call figure() more than once.

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])
A.Wan
  • 1,818
  • 3
  • 21
  • 34
1

I would just use two figures if I want them on different windows.

Something like this ought to work.

>>> 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()

It gave me six consecutive figures.

Since, you need a new figure for every iteration, do.

for index, col_name in ['col2','col3']:
    plt.figure(index)
    # Do the plotting.
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71