使用Colorbar的Matplotlib 3D散点图

借用Matplotlib文档页面上的示例并稍微修改代码,
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def randrange(n, vmin, vmax):
    return (vmax-vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zl, zh)
    cs = randrange(n, 0, 100)
    ax.scatter(xs, ys, zs, c=cs, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()
将给出每个点具有不同颜色的3D散点图(在该示例中为随机颜色)。将彩色条添加到图中的正确方法是什么,因为添加
plt.colorbar()
ax.colorbar()
似乎不起作用。     
已邀请:
这会产生一个颜色条(尽管可能不是你需要的颜色条): 替换此行:
ax.scatter(xs, ys, zs, c=cs, marker=m)
p = ax.scatter(xs, ys, zs, c=cs, marker=m)
然后用
fig.colorbar(p)
接近尾声     

要回复问题请先登录注册