带颜色条的 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()
似乎不起作用。
Borrowing from the example on the Matplotlib documentation page and slightly modifying the code,
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()
Will give a 3D scatter plot with different colors for each point (random colors in this example). What's the correct way to add a colorbar to the figure, since adding in plt.colorbar()
or ax.colorbar()
doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会产生一个颜色条(尽管可能不是您需要的颜色条):
替换这一行:
然后
使用
在末尾附近
This produces a colorbar (though possibly not the one you need):
Replace this line:
with
then use
near the end
使用上面的答案并没有解决我的问题。颜色条颜色图未链接到轴(另请注意不正确的颜色条限制):
解决方案(请参阅此处也)是在
ax.scatter
中使用cmap
:) ax.set_zlabel('$\psi_2
) ax.set_ylabel('$\Phi
解决方案(请参阅此处也)是在
ax.scatter
中使用cmap
:) ax.set_zlabel('$\psi_2
解决方案(请参阅此处也)是在
ax.scatter
中使用cmap
:) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)
解决方案(请参阅此处也)是在
ax.scatter
中使用cmap
:) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)
) ax.set_ylabel('$\Phi
解决方案(请参阅此处也)是在
ax.scatter
中使用cmap
:) ax.set_zlabel('$\psi_2
解决方案(请参阅此处也)是在
ax.scatter
中使用cmap
:) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)
解决方案(请参阅此处也)是在
ax.scatter
中使用cmap
:Using the above answer did not solve my problem. The colorbar colormap was not linked to the axes (note also the incorrect colorbar limits):
The solution (see here also) is to use
cmap
inax.scatter
:) ax.set_zlabel('$\psi_2
) ax.set_ylabel('$\Phi
The solution (see here also) is to use
cmap
inax.scatter
:) ax.set_zlabel('$\psi_2
The solution (see here also) is to use
cmap
inax.scatter
:) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)
The solution (see here also) is to use
cmap
inax.scatter
:) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)
) ax.set_ylabel('$\Phi
The solution (see here also) is to use
cmap
inax.scatter
:) ax.set_zlabel('$\psi_2
The solution (see here also) is to use
cmap
inax.scatter
:) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)
The solution (see here also) is to use
cmap
inax.scatter
: