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.

2

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.