32

I have functions that contribute to small parts of a figure generation. I'm trying 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!

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
aspade
  • 575
  • 1
  • 6
  • 10

3 Answers3

35

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
  • 67,082
  • 10
  • 127
  • 137
  • 4
    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 Sep 10 '09 at 06:21
  • Ah ha! But the second time you refer to the figure, you don't need to use the "ax" variables? – aspade Sep 11 '09 at 23:00
  • I have a follow up question [here](http://stackoverflow.com/questions/1413681/python-with-matplotlib-reusing-drawing-functions) – aspade Sep 11 '09 at 23:16
  • 1
    @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 Sep 11 '09 at 23:41
  • I already knew that, but forgot. With SO and your answer, life became much simpler to me, today. Thanks! – heltonbiker Dec 12 '11 at 18:21
7

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

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
0

The best way to show multiple figures is use matplotlib or pylab. (for windows) with matplotlib you can prepare the figures and then when you finish the process with them you can show with the comand "matplotlib.show()" and all figures should be shown.

(on linux) you don´t have problems adding changes to figures because the interactive mode is enable (on windows the interactive mode don't work OK).

Steve Rod
  • 17
  • 1