After review this question (How do I tell Matplotlib to create a second (new) plot, then later plot on the old one?) I thought I had figured this out, but I think I'm running into an issue with my for loops. Here is a pared down version of what I'm doing.
import matplotlib.pyplot as plt
import numpy as np
for m in range(2):
x=np.arange(5)
y=np.exp(m*x)
plt.figure(1)
plt.plot(x, y)
plt.show()
...
z=np.sin(x+(m*math.pi))
plt.figure(2)
plt.plot(x,z)
...
plt.figure(2)
plt.show()
My hope was that this would display three plots: a plot for e^(0) vs x the first time through, a plot of e^x vs x the second time through, and then one plot with both sin(x) and sin(x+pi) vs x.
But instead I get the first two plots and a plot with just sin(x) and plot with just sin(x+pi).
How do I get all the data I want on to figure 2? It seems to be some sort of issue with the set figure resetting when I return to the beginning of the loop.
This minimal change will probably do what you want (although it is not the best code).
Replace plt.figure(1)
with plt.figure()
. Remove any plt.show()
from inside the loop.
The loop will end and then all 3 figures will be shown. The e^x
curves will be in figures #1 and #3.