如何在 Seaborn 散点图中更改数据点的边缘颜色

33

我使用 seaborn 创建了一个散点图:

import seaborn as sns
sns.set(style="ticks", color_codes=True)
        
g=sns.scatterplot(x="length", y="coverage", data=df, hue = 'Products', edgecolors=None, alpha = 0.7)
g.set(yscale="log")
plt.xlabel("Length(bp)")
plt.ylabel("Coverage")
plt.legend(loc = 'lower right')
plt.savefig('ATN_plot.png',dpi = 600)

原始数据(部分):

contig  length  coverage    pathway Products
53  1230    2423    stigmatellin    Yes
58  1195    885 curacin Yes
65  1161    598 jamaicamide Yes
68  1159    8001    jamaicamide Yes
79  1113    1423    curacin Yes
105 1042    1106    stigmatellin    Yes
727 666 223 HSAF (heat stable antifungal factor)    Yes
787 655 37  curacin Yes
791 654 13  stigmatellin    Yes
798 652 49  stigmatellin    Yes
844 642 5774    jamaicamide Yes
89  1090    13  No  No
90  1089    10  No  No
91  1086    26  No  No
92  1080    16  No  No
93  1079    10  No  No
94  1079    13  No  No

最终的图形如下所示:

enter image description here

我不喜欢斑点周围的白边。 图像左下角的大部分斑点看起来太白了。 我已经在我的代码中包含了 edgecolors=None,但似乎没有起作用。 有人知道如何消除边缘颜色或将其更改为其他颜色吗?

3个回答

46

使用linewidth=0替代edgecolors=None

类似于:

g=sns.scatterplot(x="length", y="coverage", data=df, hue = 'Products', 
                  linewidth=0, alpha = 0.7)

非常感谢!这也回答了如何更改点的线宽。 - Steve Xu

20

在Matplotlib中,大多数参数都采用None表示“使用默认值”。但在这里,您不想使用默认值,而是没有边缘颜色。这可以通过"none"来实现。

sns.scatterplot(..., edgecolor="none")

8

您可以通过传递kwargs(关键字参数)来更改选定的颜色

kwargs  =   {'edgecolor':"r", # for edge color
             'linewidth':2.7, # line width of spot
             'linestyle':'--', # line style of spot
            }
sns.scatterplot(x = "tip", y = "total_bill", data = tips_df, hue = "sex", 
                size ="sex", sizes = (100, 300), palette = "nipy_spectral" ,**kwargs)

或者您可以直接将它作为参数传递,例如:

sns.scatterplot(x = "tip", y = "total_bill", data = tips_df, hue = "sex", 
                size ="sex", sizes = (100, 300), palette = "nipy_spectral",
                edgecolor='r',
                linewidth=2,
                linestyle='--',)

输出 >>>

输入图像描述

我希望我已经解决了你的疑问。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接