想象一下这样的数据帧:
i X Y Z label
1 23 45 23 0
2 56 67 24 0
3 34 87 25 0
4 43 78 26 0
5 45 45 37 1
6 34 98 38 1
7 23 45 39 1
8 34 76 40 1
9 54 87 41 1我知道如何使用matplot可视化x,y,z,但问题是我想使用标签列设置每个数据的颜色,例如,所有0个标记的行都应该是绿色的,而1个标记的行应该是橙色的。我是python的新手,如果能给出一个实现的例子,那就太棒了。
非常感谢你的帮助。
发布于 2021-10-11 11:42:36
您可以通过为每个label和plot scatter设置颜色来完成此操作,如下所示:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
np.random.seed(365)
df = pd.DataFrame({
'X': np.random.rand(200),
'Y': np.random.rand(200),
'Z': np.random.rand(200),
'label' : np.hstack((np.zeros(100),np.ones(100)))})
fig = plt.figure(figsize=(12,7))
ax = fig.add_subplot(projection = '3d')
colors = {0 : 'orange', 1:'g'}
for l in df['label'].unique():
ax.scatter(xs = df.loc[df.label == l, 'X'],
ys = df.loc[df.label == l, 'Y'],
zs = df.loc[df.label == l, 'Z'],
color = colors[l])
plt.show()输出:

https://stackoverflow.com/questions/69525248
复制相似问题