【问题标题】:Polishing Scatter Plot Legends With Seaborn使用 Seaborn 抛光散点图图例
【发布时间】:2020-11-07 03:54:46
【问题描述】:

下面这段代码使用seaborn生成的散点图如下。

ax = sns.scatterplot(x="Param_1", 
                     y="Param_2", 
                     hue="Process", style='Item', data=df,
                     s=30, legend='full')

我想去掉圆圈中的颜色图例(用于过程),因为圆圈也表示项目“一”的数据。在不与用于 Item 的形状产生差异的情况下,呈现 Process 的颜色图例的最佳方式是什么。

【问题讨论】:

    标签: pandas matplotlib data-visualization seaborn scatter-plot


    【解决方案1】:

    您可以创建所谓的proxy artists 并将它们用作图例符号。

    import seaborn as sns
    import matplotlib.pyplot as plt
    import matplotlib.patches as mpatches
    
    fig,(ax1,ax2) = plt.subplots(ncols=2)
    tips = sns.load_dataset("tips")
    
    hue = "day"
    style = "time"
    
    sns.scatterplot(x="total_bill", y="tip", hue=hue, style=style, data=tips, ax=ax1)
    ax1.set_title("Default Legend")
    sns.scatterplot(x="total_bill", y="tip", hue=hue, style=style, data=tips, ax=ax2)
    ax2.set_title("Custom Legend")
    
    handles, labels = ax2.get_legend_handles_labels()
    for i,label in enumerate(labels):
        if label == hue:
            continue
        if label == style:
            break
        handles[i] = mpatches.Patch(color=handles[i].get_fc()[0])
    ax2.legend(handles, labels)
    

    【讨论】:

    • 谢谢。我收到以下错误,AttributeError: 'PathCollection' 对象在尝试复制代码时没有属性 'get_fc'。这里有什么问题?
    • 嗯,你有什么 matplotlib 版本?我使用3.2.1,它有get_fc()
    猜你喜欢
    • 2019-04-15
    • 2019-09-26
    • 2020-12-04
    • 2021-01-29
    • 2017-05-09
    • 2016-12-14
    • 2020-06-06
    • 1970-01-01
    • 2021-01-22
    相关资源
    最近更新 更多