15

I am generating a total of 50+ figures but I do not wish to display them all. I would like to pick out the figures that I want to display. Is there any way to do it?

For example: I have 50 plots which are generated over the course of the program. At the end, when I enter plt.show(), it shows all the figures. However, I would like to display only 3 or 4 figures (but they aren't fixed i.e, I could plot figures 1, 2, 3, 4 at one time and another time I could plot figures 10, 27, 33, 45). Also, a separate function is generating these figures and I am returning all the figures.

Sample main script:

import matplotlib.pylab as plt
import numpy as np
from sampleplotfn import *


A = 1
omega = np.linspace(10,35,50)

for i in range(len(omega)):
    fig1 = sinewave(A,omega[i])

plt.show()

samplepltfn.py

import numpy as np
import matplotlib.pylab as plt


def sinewave(A,omega):
      t = np.linspace(0,10,25)
      f = A*np.sin(omega*t)
      fig1 = plt.figure()
      plt.plot(f,t)
      return fig1
kmario23
  • 57,311
  • 13
  • 161
  • 150
Karthik Venkatesh
  • 329
  • 2
  • 3
  • 8
  • You could close the figures you don't want to see before you call `plt.show()`, ( `if (....) plt.close(fig1)` ). – Jan Kuiken Jul 06 '15 at 15:06
  • 1
    But why do you need to plot them all ? Why can't you apply your decision criteria whatever it is before actually plotting the figure ? – jrjc Jul 06 '15 at 15:19
  • Thanks @jeanrjc ! I wasn't thinking quite well at that time. I found a way to plot only the figures that I wanted before plotting all. – Karthik Venkatesh Jul 07 '15 at 07:47

2 Answers2

10

It's going to be tricky to do what you want in a script; I agree with jrjc's comment - you should weed the plots you want before making them instead of trying to keep a ton of non-shown plots (they still take up memory and you have to remember to clean them up!). If you are in interactive mode, you can show individual plots using the show method of the individual figures (e.g. fig2.show()), as long as you've made the figures using plt.figure, but in that case they would have been shown as they were created anyway.

Additionally, I would highly recommend against using pylab, and in particular, against using "plt" as an import alias for it. "plt" is traditionally used for pyplot, not pylab. (import matplotlib.pyplot as plt). Pylab includes a ton of other stuff and is discouraged from use except for interactive work.

Lastly, you are overwriting "fig1" in your loop. Try saving the figures into a list instead of a single variable.

Ajean
  • 5,528
  • 14
  • 46
  • 69
8

According that you have created 2 figures, but you want to show only the first one:

import random

f1, a1 = plt.subplots()
a1.plot(random.sample(range(100), 20))

f2, a2 = plt.subplots()
a2.plot(random.sample(range(100), 20))

f1.show() will not work in a script.

As suggested in comments, you could instead close the figures you don't want to see before you call:

plt.close(f2)
plt.show()

A more generic solution that work for a large number of figures is to iterate over all figures and close them, except the one you want to show:

for fig_num in plt.get_fignums():
     if figure.number != fig_num:
         plt.close(figure.number)
plt.show()

Or, using generators:

[plt.close(f) for f in plt.get_fignums() if f != f1.number]
plt.show()
roipoussiere
  • 5,142
  • 3
  • 28
  • 37
  • 1
    What if I don't want to close the figures? Suppose a long script, possibly interactive, is generating fig1, but I want to plot and show a fig2 somewhere in-between before fig1 is ready? Feels like I should be able to do this. – Neinstein Mar 22 '23 at 09:44
  • Closing the figures also destroys them. In your example, creating f2 is utterly pointless. – Neinstein Apr 06 '23 at 08:23