# Colorbar values

Hi all,

I'm trying to plot a colour bar for a scatter plot, and set the
maximum value of the plot to something arbitrary. At the moment it
always comes up as 255. How can I change this?

ax = plt.gca()
pts = ax.scatter(coords[:,0], coords[:,1], c=np.linspace(0,1,n_pts), s=16,
edgecolors='k', linewidth=1)

Also, there seem to be many different ways to make colorbars associate
with certain objects like the colorbar keyword to scatter perhaps, but
the simplest/only way to create a colorbar that I can get to work is:

ax.figure.colorbar(ax.images)

Is there a better way to do this so the vmin and vmax attributes of
the scatter plot get used in the colorbar?

Thanks,

Angus.

···

--
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh

Angus McMorland wrote:

Hi all,

I'm trying to plot a colour bar for a scatter plot, and set the
maximum value of the plot to something arbitrary. At the moment it
always comes up as 255. How can I change this?

ax = plt.gca()
pts = ax.scatter(coords[:,0], coords[:,1], c=np.linspace(0,1,n_pts), s=16,
edgecolors='k', linewidth=1)

Also, there seem to be many different ways to make colorbars associate
with certain objects like the colorbar keyword to scatter perhaps, but
the simplest/only way to create a colorbar that I can get to work is:

ax.figure.colorbar(ax.images)

Is there a better way to do this so the vmin and vmax attributes of
the scatter plot get used in the colorbar?

Thanks,

Angus.

Maybe I am misunderstanding you, but the ipython -pylab approach looks like this:

In :x = rand(20)

In :y = rand(20)

In :z = rand(20)*10

In :scatter?

In :sc = scatter(x, y, c=z)

In :sc
Out:<matplotlib.collections.RegularPolyCollection object at 0x8d8dd6c>

In :colorbar?

In :colorbar(sc)
Out:<matplotlib.colorbar.Colorbar instance at 0x92d29cc>

The more object-oriented approach with ax.scatter, etc. is essentially the same. If color mapping is used by scatter, then the collection it returns is a "mappable", which is the argument colorbar needs to make the connection.

Eric