【问题标题】:Is there any way to change the legends in seaborn?有什么办法可以改变seaborn的传说吗?
【发布时间】:2021-11-15 03:55:47
【问题描述】:

我想更改图例框中 pIC50 的格式。我希望它是“根据没有填充颜色的大小的圆形”。欢迎提出任何建议!

plt.figure(figsize=(7, 7))

sns.scatterplot(x='MW', y='LogP', data=df_2class, hue='class', size='pIC50', edgecolor='black', alpha=0.2)
sns.set_style("whitegrid", {"ytick.major.size": 100,"xtick.major.size": 2, 'grid.linestyle': 'solid'})

plt.xlabel('MW', fontsize=14, fontweight='bold')
plt.ylabel('LogP', fontsize=14, fontweight='bold')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)

【问题讨论】:

    标签: seaborn python-3.7 scatter-plot


    【解决方案1】:

    在这种情况下,您可以遍历最后一个图例句柄并更改点的颜色。下面是一个使用 iris 数据集的例子:

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    iris = sns.load_dataset('iris')
    ax = sns.scatterplot(data=iris, x='sepal_length', y='petal_length', hue='species', size='sepal_width')
    handles, labels = ax.get_legend_handles_labels()
    for h in handles[-5:]: # changes the 5 last handles, this number might be different in your case
        h.set_facecolor('none')
    ax.legend(handles=handles, labels=labels, bbox_to_anchor=[1.02, 1.02], loc='upper left')
    plt.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 2019-12-26
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多