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
  • 3,181
  • 7
  • 38
  • 62

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
  • 7,479
  • 7
  • 35
  • 71