2

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?

8
  • Tried, did not work, if you're not willing too help, might as well make yourself scarce. Commented Jun 16, 2014 at 20:07
  • google.com/… Commented Jun 16, 2014 at 20:08
  • I also attempted something like ax2 = plt.gca().twinx() ax2. but that created different axis that was not necessarily alligned with the original. Commented Jun 16, 2014 at 20:10
  • Can you please explain exactly what you had in mind, i.e. type it out in terms of my notation from above? Commented Jun 16, 2014 at 20:14
  • I work in notebook, so there's no error trace, nor a figure. Commented Jun 16, 2014 at 20:22

1 Answer 1

2

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

5 Comments

Stupid question, but why is this required? For ax = plt.figure().add_subplot(111), you can add as many scatter/plots without plt.hold(True)? The underlying default setting of hold changes for subplot2grid?
@jonnybazookatone the status hold get's confused when you mix the object-oriented interface with the pyplot interface. In other words, pyplot commands will always try to make a new figure/axes unless you tell it not to. [soapbox]This is exactly why pyplot should be avoided when methods of the axes objects are so easily accessible.[/soapbox]
@PaulH Yes, this makes sense. But from his first post, he does indeed access the object form as it noted in the solution. Only on his second attempt does he mix the objects and direct calls of pyplot. So, his unedited code should work fine.
@jonnybazookatone: whether the original solution would work depends on the "hold state" since the OP never directly plots to the axes (eg, by using an axes object), and since the original approach didn't work, apparently, hold was False. The hold state can be set elsewhere in the code, or in the matplotlibrc file.
Weird. I would have thought the default setting upon installation of matplotlib would be standard for all builds and versions. Given he knew nothing of it, I don't see him editing the matplotlibrc file. Fair enough.

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.