views:

1377

answers:

2

I'm have functions that contribute small parts of a figure generation. What I'm trying to do is to use these functions to generate multiple figures? So something like this:

  1. work with Figure 1
  2. do something else
  3. work with Figure 2
  4. do something else
  5. work with Figure 1
  6. do something else
  7. work with Figure 2

If anyone could help, that'd be great!

+6  A: 

There are several ways to do this, and the simplest is to use the figure numbers. The code below makes two figures, #0 and #1, each with two lines. #0 has the points 1,2,3,4,5,6, and #2 has the points 10,20,30,40,50,60.

from pylab import *

figure(0)
plot([1,2,3])

figure(1)
plot([10, 20, 30])

figure(0)
plot([4, 5, 6])

figure(1)
plot([40, 50, 60])

show()
tom10
That's the pylab "state machine" style interface. A better option for serious software development is to use the object-oriented way where you have figure objects containing axes objects whose plot methods you call. But the pylab approach is much simpler for interactive command-line usage.
Jouni K. Seppänen
Ah ha! But the second time you refer to the figure, you don't need to use the "ax" variables?
aspade
I have a follow up question [here](http://stackoverflow.com/questions/1413681/python-with-matplotlib-reusing-drawing-functions)
aspade
@aspade - the "ax" variable were superfluous, so I've removed them. At the time, when I was writing the code I was deciding between the pylab approach and the "artist objects" approach that EOL and JKS are refering to, and the "ax" variables were just a left-over from that.
tom10
@tom10 - I see. Thanks for the clarification!
aspade
+1  A: 

For a more general answer to this question and to questions you may soon have, I would recommend the official tutorial.

EOL
Thank you for the link.
aspade