使用matplotlib散点图函数指向错误的z坐标

时间:2017-06-15 06:47:29

标签: python matplotlib plot

我试图使用matplotlib的散点图函数(Python 2.7,Spyder GUI)绘制这个简单的点集:

X=[0 0 1 1]
Y=[0 1 0 1]
Z=[0 1 1 1]

这实际上代表OR运算符,所以我想为z = 0的点设置不同的标记/颜色。这是我的代码:

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


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

X = np.zeros( (4,1) )
X[0,0] = 0
X[1,0] = 0
X[2,0] = 1
X[3,0] = 1

Y = np.zeros( (4,1) )
Y[0,0] = 0
Y[1,0] = 1
Y[2,0] = 0
Y[3,0] = 1

Z = np.zeros( (4,1) )
Z[0,0] = 0
Z[1,0] = 1
Z[2,0] = 1
Z[3,0] = 1

for i in range(0,len(Z)):

    if Z[i]:
        ax.scatter(X[i],Y[i],Z[i],'x',color="r")

    else:
        ax.scatter(X[i],Y[i],Z[i],'o',color="b")

ax.view_init(elev=0., azim=200)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

这是我得到的情节:

enter image description here

显然,底部中间的红点应位于情节的左上角。

我的代码是否有问题,或者是matplotlib的问题?

1 个答案:

答案 0 :(得分:3)

将标记参数明确设置为marker='x'而不是位置参数。

ax.scatter(X[i],Y[i],Z[i],marker='x',color="r")

这将产生所需的情节。

enter image description here

原因是否则"x"将被解释为zdir参数,为2D散点图设置平面。