繁体   English   中英

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

[英]Change marker/color in 3D scatter plot based on condition

我想用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()

也许解决方案也是我还没有学到的东西,但是在我看来,要使它起作用并不难。

您可以只使用NumPy索引。 由于NumPy已经是matplotlib的依赖项,因此您可以通过将列表转换为数组来使用数组索引。

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

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

在此处输入图片说明

为每个条件创建单独的点:

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')   
ax.scatter(X2, Y2, Z2, c='r', marker='o')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM