0

I am trying to plot a diagram inside a loop and I expect to get two separate figures, but Python only shows one figure instead. In fact, it seems Python plots the second figure over the first one. This is the code I am using:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10)
y = np.arange(0,10)

for _ in range(2):
   plt.plot(x,y)
   plt.show()

It worth noting that I am working with Python 2.7 in PyCharm environment. Any kind of advice is appreciated.

amiref
  • 2,881
  • 6
  • 32
  • 52

2 Answers2

2

Try the following:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10)
y = np.arange(0,10)

for _ in range(2):
   plt.figure() # add this statement before your plot
   plt.plot(x,y)
   plt.show()
aleeciu
  • 36
  • 2
1

This could do:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10)
y = np.arange(0,10)

f, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(x, y)
ax2.plot(x, y)
plt.show()
Learning is a mess
  • 5,905
  • 5
  • 30
  • 64