如何制作带有色调的3D散点图?

我有四列:性别、体重、身高、年龄。我需要用 matplotlib 或 seaborn 构建一个 3dscatter 图,其中 x 轴 = 重量,y = 高度,z = 年龄,并用不同的颜色标记性别。我只能像这样构建二维散点图

sns.scatterplot(x = 'height', y = 'age',hue='sex',data=df, palette=['blue',"pink"])

但不知道如何添加 z 轴

回答

您需要为此使用 matplotlib,我认为 seaborn 中没有 3d scatter 选项:

import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import pandas as pd

df = pd.DataFrame({'height':np.random.uniform(160,190,20),
                   'weight':np.random.uniform(60,80,20),
                   'age':np.random.randint(20,60,20),
                   'sex':np.random.choice(['M','F'],20)
                  })

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

for s in df.sex.unique():
    ax.scatter(df.height[df.sex==s],df.weight[df.sex==s],df.age[df.sex==s],label=s)
    
ax.legend()


以上是如何制作带有色调的3D散点图?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>