abcdef
abcdef

Reputation: 21

matplotlib will only show the last plot (cannot superimpose plots)

My code uses several plt.plot() instructions to superimpose curves, and used to work perfectly until today. Now it looks like the last plt.plot() will delete the previous ones, so that I can't superimpose them.

import matplotlib.pyplot as plt
X = [i for i in range(5)]
plt.plot(X,[0]*5)
plt.plot(X,[1]*5)
plt.show()

Instead of getting two lines, which I did until now, it will only show the last one. I don't understand why that would happen, especially since I don't remember updating libraries. Do you know why it happens ? Thanks

Upvotes: 2

Views: 1354

Answers (2)

Jacob Rodal
Jacob Rodal

Reputation: 650

Your code worked with my version of python and matplotlib, so I can't guarantee that this alternative solution will work for you, but you could try creating a figure and then adding plots to it like so:

import matplotlib.pyplot as plt

fig = plt.figure()
X = list(range(5))
for y in range(2):
    plt.plot(X, [y]*5)
plt.show()

Upvotes: 0

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11073

Try this:

plt.plot(X, [0]*5, 'r--', X, [1]*5, 'g^')
plt.show()

Upvotes: 1

Related Questions