在matplotlib中的颜色与散点图

时间:2016-08-23 20:43:59

标签: python matplotlib plot colors 3d

我在python中使用matplotlib中的散点3d绘图来绘制以下数据,

      x              y            z        b2
3.28912855713 4.64604863545 3.95526335139 1.0
3.31252670518 4.65385917898 3.96834118896 1.0

当情节为2d时,

fig = plt.figure()
ax = fig.add_subplot(111)
line =ax.scatter(x,y,c=b2,s=500,marker='*',edgecolors='none')
cb = plt.colorbar(line)

它生成以下图像,这是完全正确的。 enter image description here

现在,我想绘制3d点,代码为:

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
line = ax.scatter(x,y,z,c=b2,s=1500,marker='*',edgecolors='none',depthshade=0)
cb = plt.colorbar(line)

我得到了以下情节,这是不对的,因为颜色应该像之前的情况一样是绿色的(b2不会改变)。 enter image description here

我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

可能不是。我不知道您使用的是什么版本的Matplotlib,但在某些时候存在此问题的错误,显然仍然会对我今天使用的版本产生影响,即1.5.1(检查{{3}有关此内容的更多信息)。

使@Yann解决方案适应您的问题应解决该问题。我在计算机上测试了它,结果如下:

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

x = [3.28912855713, 3.31252670518]
y = [4.64604863545, 4.65385917898]
z = [3.95526335139, 3.96834118896]
b2 = [1, 1]

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
line = ax.scatter(x, y ,z , c=b2, s=1500, marker='*', edgecolors='none', depthshade=0)
cb = plt.colorbar(line)

def forceUpdate(event):
    global line
    line.changed()

fig.canvas.mpl_connect('draw_event', forceUpdate)

plt.show()

入口处和旋转后的绘图图像为:

this question

3D matplotlib scatterplot with color

相关问题