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

标签 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 有问题?

最佳答案

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

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

这将产生所需的图。

enter image description here

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

关于python - 使用 matplotlib 散点图函数指向错误的 z 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44560486/

相关文章:

python - 使用我自己的数据进行文本分类的 Tensorflow 错误

python - Matplotlib - 绘制非均匀线分布

python - 如何在 pandas 的不同位置绘制图表?

r - 创建加权值的直方图

plot - Gnuplot:按特定列对数据进行分组以进行绘图

python - 删除 Matplotlib 中数据点周围的填充

python - PySide2 引发错误, "QPaintDevice: Cannot destroy paint device that is being painted"

python - python 中重叠列表函数在 True 时返回 False

python - 如何制作 matplotlib 散点图正方形?

python - NetworkX 有向图,无权值和自弧