1

I have 3d coordinates defining positions of points in my 3D scatter plot with a corresponding number of values who I want to create a colour scale for representing the range of values as such:

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

xs,ys,zs = np.random.random(50),np.random.random(50),np.random.random(50)
values = np.random.random(50)*10

p = ax.scatter3D(xs, ys, zs=zs, c=values)
cbar = fig.colorbar(p)
cbar.cmap(plt.cm.Greys)

without the last line cbar.cmap(plt.cm.Greys) I get a default colormap (jet I think) but when I try to modify this I get the error TypeError: Cannot cast array data from dtype('O') to dtype('int64') according to the rule 'safe'. I would like to be able to change this and I have been looking through the documentation but it seems like there are so many different methods for doing similar things. How can I change the default colormap from this point?

I'm

pbreach
  • 16,049
  • 27
  • 82
  • 120
  • 1
    try `ax.scatter3D(..., cmap='gray')` – tacaswell Aug 01 '14 at 01:18
  • If I take out the last line and do what you mention I get an error at `cbar = fig.colorbar(p)` saying, `TypeError: You must first set_array for mappable`. If I take out the last two lines there is no colour bar and the points are different shades of blue as opposed to the gray colorscale. – pbreach Aug 01 '14 at 03:04

1 Answers1

10

You have to give the cmap Keyword for scatter. Here are all possible cmaps: http://matplotlib.org/examples/color/colormaps_reference.html and you might want to add the ax to the colorbar.

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

xs,ys,zs = np.random.random(50),np.random.random(50),np.random.random(50)
values = np.random.random(50)*10

p = ax.scatter3D(xs, ys, zs=zs, c=values, cmap='hot')

fig.colorbar(p, ax=ax)

enter image description here

MaxNoe
  • 14,470
  • 3
  • 41
  • 46