Baron Yugovich
Baron Yugovich

Reputation: 4307

Matplotlib - scatter on top of regular plot

I have this:

 fig = plt.subplot2grid((count, count), (i, 0), rowspan=1, colspan=count)
 fig.plot(x, y, label='trup')

I also want to scatter another time series, on the same figure, with the same axes and scale, something like

fig.scatter(x2, y2, label='scatter')

How do I do this?

Edit: This works, but I am getting 2 different pairs of axes:

fig = plt.subplot2grid((1, 1), (0, 0), rowspan=1, colspan=count)
fig.plot(x,y)
plt.scatter(u, v)

How do I make sure they are on the same exact axis?

Upvotes: 2

Views: 2856

Answers (1)

tom10
tom10

Reputation: 69172

You probably need to set plt.hold(True)

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 10)
u = np.arange(0, 10)

plt.hold(True)
plt.subplot2grid((1, 1), (0, 0))
plt.plot(x,x)
plt.scatter(u, u[::-1])
plt.show()

enter image description here

Above is the pyplot version. You can also plot directly to the axes of your choice, so the following will give the same plot as above:

ax = plt.subplot2grid((1, 1), (0, 0))
ax.plot(x,x)
ax.scatter(u, u[::-1])

Upvotes: 2

Related Questions