Issue
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
Solution
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.
Answered By - Ajean
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.