I posted earlier about a program I was attempting to make to plot four-dimensional data (xyz coords and voltages) on a 3d scatter plot with a colour map dependent upon the magnitude of the voltages.

I'm able to run my program without error (suggesting I'm not missing any packages) though my datapoints are always coming up as being blue, indicating that the colour map is not being applied properly. Can anybody see what the problem is? I'm most appreciative if so.

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()

Many thanks in advance to whomever is kind enough to have a look at this for me.

有帮助吗?

解决方案

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()

Produces example image

See this Matplotlib 3D scatter color lost after redraw that reports the same thing from version 1.1.0 (and has a work-around). This is a old bug in mpl that has been fixed in later versions (PR here)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top