问题
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
回答1:
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)
来源:https://stackoverflow.com/questions/25071136/change-default-colorbar-for-3d-scatter-plot-with-matplotlib