【问题标题】:How to make 3D scatter plot with a hue?如何制作带有色调的 3D 散点图?
【发布时间】:2021-02-13 18:52:19
【问题描述】:

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

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

但不知道如何添加z轴

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    你需要为此使用 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()
    

    【讨论】:

    • 谢谢!有用!但是还有一个问题,我怎样才能改变颜色?
    猜你喜欢
    • 2023-03-08
    • 2016-12-30
    • 2012-02-12
    • 2011-07-26
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多