Change default colorbar for 3D scatter plot with matplotlib

跟風遠走 提交于 2021-02-19 08:25:19

问题


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)

enter image description here



来源:https://stackoverflow.com/questions/25071136/change-default-colorbar-for-3d-scatter-plot-with-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!