想象一個像這樣的資料框:
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很陌生,如果給出一個示例實作,那就太好了。
非常感謝你的幫助。
uj5u.com熱心網友回復:
您可以為每個設定顏色label并繪制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://www.uj5u.com/net/317830.html
標籤:Python 熊猫 matplotlib
