Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I normalize colormap in matplotlib scatter plot?

The matplotlib documentation explain in detail how to normalize colormaps for a pcolormesh, but how can I correctly do it for a scatter plot?

normalize = mcolors.Normalize(vmin=-1, vmax=1)
plt.scatter(x,y,z,cmap=colormap(normalize),marker='*',s=5)

doesn't work (TypeError: Cannot cast array data from dtype('O') to dtype('int64') according to the rule 'safe')

it's just that the z data are not exactly from -1 to 1, I am plotting multiple datasets that have the limits around +/- 0.93 - 98, but I want the colours to be centered at zero and go from -1 to 1 so that I have the same reference for all the various datasets.

Oh, and when I don't attempt to normalize, I get TypeError: scatter() got multiple values for keyword argument 's'. Clearly I don't know how to use colormap in scatter plots.

like image 271
durbachit Avatar asked Mar 31 '17 04:03

durbachit


1 Answers

The syntax you're using is completely different to the one in the linked documentation. There is essentially no difference between normalizing a scatter or a pcolor(mesh) or just any other scalar mappable object.

It's always

colormap = plt.cm.bwr #or any other colormap
normalize = matplotlib.colors.Normalize(vmin=-1, vmax=1)
plt.scatter(x, y, c=z, s=5, cmap=colormap, norm=normalize, marker='*')
like image 100
ImportanceOfBeingErnest Avatar answered Nov 14 '22 21:11

ImportanceOfBeingErnest