Cobin
Cobin

Reputation: 930

How to basemap scatter with colorbar?

I want to plot a series scatter points xx,yy, and r is point values. I want to create colorbar for r values. It is similar to 2-D coordination plot,just change in basemap in this example.

sc=map.scatter(xx,yy,r,s=30,cmap=plt.cm.jet)
plt.colorbar(sc)

But hint:

TypeError: scatter() got multiple values for argument 's'

This answer explained that 3rd arg in scatter is size implicit? So, what's wrong with it? And how to m.scatter points with colorbar?

Upvotes: 1

Views: 2252

Answers (1)

tmdavison
tmdavison

Reputation: 69116

If you don't label the options, scatter will assume the 3rd one is s (the size). In your case, you want r to be the color of the points, so you need to tell it that (i.e. use the kwarg option c):

sc=map.scatter(xx, yy, c=r, s=30, cmap=plt.cm.jet)

As mentioned in the answer you linked, by not naming the 3rd argument, and then setting the 4th argument to s, you have specified s twice, the first time implicitly (although you didn't mean to), and the second time explicitly.

Another way, if you don't want to name your arguments, is to make sure you have them in the same order as scatter is expecting:

sc=map.scatter(xx, yy, 30, r, cmap=plt.cm.jet)

From the docs, you can see the expected order:

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)

Upvotes: 4

Related Questions