猿问

根据条件更改 3D 散点图中的标记/颜色

我想用 matplotlib 在 Python 中做一个 3D 散点图,例如点 > 5 显示为红色,其余为蓝色。


问题是我仍然用标记/颜色绘制了所有值,我也知道为什么会这样,但我对 Python 的思考不够深入,无法解决这个问题。


X = [3, 5, 6, 7,]

Y = [2, 4, 5, 9,]

Z = [1, 2, 6, 7,]


#ZP is for differentiate between ploted values and "check if" values


ZP = Z


for ZP in ZP:


    if ZP > 5:

        ax.scatter(X, Y, Z, c='r', marker='o')

    else:

        ax.scatter(X, Y, Z, c='b', marker='x')


plt.show()

也许解决方案也是我还没有学到的东西,但在我看来,让它发挥作用应该不难。


隔江千里
浏览 344回答 2
2回答

杨__羊羊

您可以使用 NumPy 索引。由于 NumPy 已经是 的依赖项matplotlib,您可以通过将列表转换为数组来使用数组索引。import matplotlib.pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3D&nbsp;fig = plt.figure()ax = fig.add_subplot(111, projection='3d')X = np.array([3, 5, 6, 7])Y = np.array([2, 4, 5, 9])Z = np.array([1, 2, 6, 7])ax.scatter(X[Z>5], Y[Z>5], Z[Z>5], s=40, c='r', marker='o')ax.scatter(X[Z<=5], Y[Z<=5], Z[Z<=5], s=40, c='b', marker='x')plt.show()

慕田峪4524236

为每个条件创建单独的点:X1,Y1,Z1 = zip(*[(x,y,z) for x,y,z in zip(X,Y,Z) if z<=5])X2,Y2,Z2 = zip(*[(x,y,z) for x,y,z in zip(X,Y,Z) if z>5])ax.scatter(X1, Y1, Z1, c='b', marker='x')&nbsp; &nbsp;ax.scatter(X2, Y2, Z2, c='r', marker='o')
随时随地看视频慕课网APP

相关分类

Python
我要回答