2

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.

Tyberius
  • 625
  • 2
  • 12
  • 20
  • 1
    Don't call `plt.show()` after each call to `plt.plot()`, but rather only once after you've made all your calls to `plt.plot()`. – cmaher Feb 08 '18 at 00:02
  • 1
    Would you mind providing a [mcve] with real code in your question, such that people can understand and reproduce what you are doing? Also clearly state the desired output. "a plot for figure 1" is not really understandable. Rather say which data you'd expect to appear on which figure. – ImportanceOfBeingErnest Feb 08 '18 at 00:06
  • @cmaher To clarify my understanding of what I wrote (which may be incorrect): first time through the loop I plot and display figure 1 and then plot , but don't display figure 2. The next time through the loop, I again plot and display a figure 1 with new data and plot another set of data to figure 2, but don't plot. Then after leaving the loop, I try to display figure 2. To me, that seems like I'm calling plt.show() only after I have plotted all my data. – Tyberius Feb 08 '18 at 00:09
  • @ImportanceOfBeingErnest I will edit the code and question accordingly. – Tyberius Feb 08 '18 at 00:10
  • @ImportanceOfBeingErnest is this fleshed out enough? I believe this is verifiable and seems to cover the crux of my problem. – Tyberius Feb 08 '18 at 03:49
  • Your code creates 2 curves on figure#1 and two in figure #2. What did you wanted it to do? – Aguy Feb 08 '18 at 03:59
  • @Aguy when I run this example code, I get a figure #1 with e^0. When I close that, I get a figure #1 again with e^x. For these two plots, this is exactly what I want. However, then I get plot that just has sin(x) as figure #2 and when I close that I get another figure #2 that is just sin(x+pi). I would like to have sin(x) and sin(x+pi) show up on the same plot. – Tyberius Feb 08 '18 at 04:20

1 Answers1

2

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.

Aguy
  • 7,851
  • 5
  • 31
  • 58