amiref
amiref

Reputation: 3431

Plotting multiple figures in a loop

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.

Upvotes: 0

Views: 9173

Answers (2)

aleeciu
aleeciu

Reputation: 36

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()

Upvotes: 2

Learning is a mess
Learning is a mess

Reputation: 8277

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()

Upvotes: 1

Related Questions