Matplotlib 3d散点图缺少颜色图

时间:2013-02-25 17:13:15

标签: python matplotlib color-mapping

我之前发过一篇关于我试图在三维散点图上绘制四维数据(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 个答案:

答案 0 :(得分: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()

制作example image

请参阅此Matplotlib 3D scatter color lost after redraw报告版本1.1.0中的相同内容(并且有一个解决方法)。这是mpl中的一个旧错误,已在以后的版本中修复(PR here