39

I am using Python matplotlib. i want to superimpose scatter plots. I know how to superimpose continuous line plots with commands:

>>> plt.plot(seriesX)
>>> plt.plot(Xresampl)
>>> plt.show()

But it does not seem to work the same way with scatter. Or maybe using plot() with a further argument specifying line style. How to proceed? thanks

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
kiriloff
  • 25,609
  • 37
  • 148
  • 229

3 Answers3

55

You simply call the scatter function twice, matplotlib will superimpose the two plots for you. You might want to specify a color, as the default for all scatter plots is blue. This is perhaps why you were only seeing one plot.

import numpy as np
import pylab as plt

X = np.linspace(0,5,100)
Y1 = X + 2*np.random.random(X.shape)
Y2 = X**2 + np.random.random(X.shape)

plt.scatter(X,Y1,color='k')
plt.scatter(X,Y2,color='g')
plt.show()

enter image description here

Hooked
  • 84,485
  • 43
  • 192
  • 261
20

If you wish to continue using plot you can use the axis object returned by subplots:

import numpy as np
import pylab as plt

X = np.linspace(0,5,100)
Y1 = X + 2*np.random.random(X.shape)
Y2 = X**2 + np.random.random(X.shape)

fig, ax = plt.subplots()
ax.plot(X,Y1,'o')
ax.plot(X,Y2,'x')
plt.show()
Gadi Oron
  • 880
  • 8
  • 6
1

Here's an other way:

X = np.linspace(0,5,100)
Y1 = X + 2*np.random.random(X.shape)
Y2 = X**2 + np.random.random(X.shape)

plt.plot(Y1, label = "Y1")
plt.plot(Y2, label = "Y2")
plt.tight_layout()
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
Erick Audet
  • 349
  • 5
  • 13