带颜色条的 3D 散点图

发布于 2024-10-27 23:34:38 字数 860 浏览 5 评论 0原文

借用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2024-11-03 23:34:38

这会产生一个颜色条(尽管可能不是您需要的颜色条):

替换这一行:

ax.scatter(xs, ys, zs, c=cs, marker=m)

然后

p = ax.scatter(xs, ys, zs, c=cs, marker=m)

使用

fig.colorbar(p)

在末尾附近

This produces a colorbar (though possibly not the one you need):

Replace this line:

ax.scatter(xs, ys, zs, c=cs, marker=m)

with

p = ax.scatter(xs, ys, zs, c=cs, marker=m)

then use

fig.colorbar(p)

near the end

旧人 2024-11-03 23:34:38

使用上面的答案并没有解决我的问题。颜色条颜色图未链接到轴(另请注意不正确的颜色条限制):

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

data = np.random.rand(3, 100)
x, y, z = data  # for show
c = np.arange(len(x)) / len(x)  # create some colours

p = ax.scatter(x, y, z, c=plt.cm.magma(0.5*c))
ax.set_xlabel('$\psi_1

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

data = np.random.rand(3, 100)
x, y, z = data  # for show
c = np.arange(len(x)) / len(x)  # create some colours

p = ax.scatter(x, y, z, c=0.5*c, cmap=plt.cm.magma)
ax.set_xlabel('$\psi_1

在此处输入图像描述

) ax.set_ylabel('$\Phi

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap


在此处输入图像描述

) ax.set_zlabel('$\psi_2

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap


在此处输入图像描述

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap


在此处输入图像描述

) ax.set_ylabel('$\Phi

在此处输入图像描述

) ax.set_ylabel('$\Phi

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap


在此处输入图像描述

) ax.set_zlabel('$\psi_2

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap


在此处输入图像描述

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap

在此处输入图像描述

) ax.set_zlabel('$\psi_2

在此处输入图像描述

) ax.set_ylabel('$\Phi

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap

在此处输入图像描述

) ax.set_zlabel('$\psi_2

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap

在此处输入图像描述

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

bad example

解决方案(请参阅此处也)是在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

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap

在此处输入图像描述

) ax.set_zlabel('$\psi_2

bad example

解决方案(请参阅此处也)是在ax.scatter中使用cmap

在此处输入图像描述

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

bad example

解决方案(请参阅此处也)是在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):

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

data = np.random.rand(3, 100)
x, y, z = data  # for show
c = np.arange(len(x)) / len(x)  # create some colours

p = ax.scatter(x, y, z, c=plt.cm.magma(0.5*c))
ax.set_xlabel('$\psi_1

bad example

The solution (see here also) is to use cmap in ax.scatter:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

data = np.random.rand(3, 100)
x, y, z = data  # for show
c = np.arange(len(x)) / len(x)  # create some colours

p = ax.scatter(x, y, z, c=0.5*c, cmap=plt.cm.magma)
ax.set_xlabel('$\psi_1

enter image description here

) ax.set_ylabel('$\Phi

bad example

The solution (see here also) is to use cmap in ax.scatter:


enter image description here

) ax.set_zlabel('$\psi_2

bad example

The solution (see here also) is to use cmap in ax.scatter:


enter image description here

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

bad example

The solution (see here also) is to use cmap in ax.scatter:


enter image description here

) ax.set_ylabel('$\Phi

enter image description here

) ax.set_ylabel('$\Phi

bad example

The solution (see here also) is to use cmap in ax.scatter:


enter image description here

) ax.set_zlabel('$\psi_2

bad example

The solution (see here also) is to use cmap in ax.scatter:


enter image description here

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

bad example

The solution (see here also) is to use cmap in ax.scatter:

enter image description here

) ax.set_zlabel('$\psi_2

enter image description here

) ax.set_ylabel('$\Phi

bad example

The solution (see here also) is to use cmap in ax.scatter:

enter image description here

) ax.set_zlabel('$\psi_2

bad example

The solution (see here also) is to use cmap in ax.scatter:

enter image description here

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

bad example

The solution (see here also) is to use cmap in ax.scatter:

enter image description here

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

enter image description here

) ax.set_ylabel('$\Phi

bad example

The solution (see here also) is to use cmap in ax.scatter:

enter image description here

) ax.set_zlabel('$\psi_2

bad example

The solution (see here also) is to use cmap in ax.scatter:

enter image description here

) ax.set_box_aspect([np.ptp(i) for i in data]) # equal aspect ratio fig.colorbar(p, ax=ax)

bad example

The solution (see here also) is to use cmap in ax.scatter:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文