Matplotlib 3D 散点图缺失颜色映射
我之前发过一个帖子,讲的是我正在尝试制作一个程序,用来在三维散点图上绘制四维数据(包括xyz坐标和电压),并且希望根据电压的大小来显示不同的颜色。
我的程序可以正常运行,没有错误(这说明我没有缺少任何必要的包),但是我的数据点总是显示为蓝色,这表明颜色映射没有正确应用。有没有人能帮我看看问题出在哪里?如果能帮忙,我会非常感激。
from pylab import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = array([-4,-4, -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,4,4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, -6.5, -6, -5.5,
])
y = array([-0.1, 3.95, 5.8,4.4,0.1,-4,-5.8,-4,1,2.4,3.2, 1.6,-0.8,-2.6,-3.3,-1.4,-0.1,3.95, 5.8,4.4,0.1,-4,-5.8,-4,1,2.4, 3.2, 1.6, -0.8, -2.6, -3.3, -1.4, -0.1, 3.95, 5.8, 4.4, 0.1, -4, -5.8, -4, 1, 2.4, 3.2, 1.6, -0.8, -2.6, -3.3, -1.4, -0.1, 3.95, 5.8, 4.4, 0.1, -4, -5.8, -4, 1, 2.4, 3.2, 1.6, -0.8, -2.6, -3.3, -1.4, -0.1, 3.95, 5.8, 4.4, 0.1, -4, -5.8, -4, 1, 2.4, 3.2, 1.6, -0.8, -2.6, -3.3, -1.4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
z = array([5.85, 4, 0.2,-3.8,-5.85,-4.1,-0.15,4,2.75,1.4,-0.3, -2.6, -2.75,-2.7, 1.2, 3.2,5.85,4, 0.2,-3.8,-5.85,-4.1,-0.15, 4, 2.75, 1.4, -0.3, -2.6, -2.75, -2.7, 1.2, 3.2, 5.85, 4, 0.2, -3.8, -5.85, -4.1, -0.15, 4, 2.75, 1.4, -0.3, -2.6, -2.75, -2.7, 1.2, 3.2, 5.85, 4, 0.2, -3.8, -5.85, -4.1, -0.15, 4, 2.75, 1.4, -0.3, -2.6, -2.75, -2.7, 1.2, 3.2, 5.85, 4, 0.2, -3.8, -5.85, -4.1, -0.15, 4, 2.75, 1.4, -0.3, -2.6, -2.75, -2.7, 1.2, 3.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
v = array([0.29, 0.32, 0.3, 0.27, 0.3, 0.28, 0.31, 0.28, 0.19, 0.18, 0.19, 0.18, 0.19, 0.19, 0.2, 0.19, 0.12, 0.12, 0.11, 0.1, 0.11, 0.15, 0.14, 0.13, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.32, 0.31, 0.3, 0.31, 0.29, 0.3, 0.32, 0.31, 0.2, 0.21, 0.19, 0.19, 0.19, 0.19, 0.19, 0.2, 0.13, 0.12, 0.14, 0.14, 0.17, 0.17, 0.16, 0.14, 0.18, 0.18, 0.18, 0.19, 0.19, 0.19, 0.19, 0.18, 0.16, 0.18, 0.2, 0.19, 0.18, 0.15, 0.16, 0.18, 0.18, 0.18, 0.18, 0.17, 0.17, 0.17, 0.18, 0.18, 0.13, 0.14, 0.15, 0.16, 0.17, 0.17, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.17, 0.17, 0.16, 0.15, 0.14, 0.12, 0.08, 0.09, 0.11, 0.09, 0.1, 0.11,
])
c = abs(v)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = plt.get_cmap("hot")
cax = ax.scatter(x, y, z, v, s=50, c = c, cmap = cmhot)
plt.show()
非常感谢愿意帮我看看这个问题的人。
1 个回答
2
from pylab import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
th = np.linspace(0, 2 * pi, 100)
x = cos(th)
y = sin(th)
z = th
v = cos(th) * sin(th)
c = abs(v)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = plt.get_cmap("hot")
cax = ax.scatter(x, y, z, v, s=50, c = c, cmap = cmhot)
plt.show()
这个代码会生成一张图片,如下所示:
可以查看这个链接 Matplotlib 3D 散点图颜色在重绘后丢失,里面提到从版本1.1.0开始就有同样的问题(并且提供了一个解决方法)。这是一个在mpl中存在已久的bug,后来在更新的版本中已经修复了(详细信息可以在这个PR 这里找到)。