如何去除Seaborn条形图上的黑色线条?
如何去除Seaborn条形图上的黑色线条?
你的问题场景
你运行了以下代码绘制条形图,但图表出现了黑色线条:
plt.figure(figsize=(14, 10)) sns.set(rc={ "xtick.labelsize": 15, "ytick.labelsize": 15 }) sns.barplot(x='Industry', y='Revenue', data=df, hue='Industry', palette='viridis') plt.title('Total Revenue by Industry') plt.ylabel('Revenue (in $ millions)') plt.xticks(rotation=70)
对应的图表:
问题原因
这些黑色线条是边缘线(edge lines),出现的核心原因是你同时把x和hue参数都设成了Industry:
Seaborn的barplot里,hue是用来做额外分组的,默认会给不同hue组的条形加边缘线来区分。但你的x轴已经按Industry分好组了,每个x类别本身就是独立的hue组,所以每条条形都被多画了一圈黑边。
解决方法
有两种简单的方式可以去除这些线条:
方法1:移除多余的hue参数
既然x轴已经是Industry,完全没必要用hue重复分组,直接删掉这个参数就行:
plt.figure(figsize=(14, 10)) sns.set(rc={ "xtick.labelsize": 15, "ytick.labelsize": 15 }) # 移除hue参数 sns.barplot(x='Industry', y='Revenue', data=df, palette='viridis') plt.title('Total Revenue by Industry') plt.ylabel('Revenue (in $ millions)') plt.xticks(rotation=70)
方法2:手动关闭边缘线
如果你因为后续调整等原因必须保留hue参数,可以通过设置edgecolor='none'或者linewidth=0来直接关闭边缘线:
plt.figure(figsize=(14, 10)) sns.set(rc={ "xtick.labelsize": 15, "ytick.labelsize": 15 }) # 添加edgecolor参数关闭边缘线 sns.barplot(x='Industry', y='Revenue', data=df, hue='Industry', palette='viridis', edgecolor='none') plt.title('Total Revenue by Industry') plt.ylabel('Revenue (in $ millions)') plt.xticks(rotation=70)
两种方法都能解决问题,第一种更简洁,完全适配你当前的场景~
备注:内容来源于stack exchange,提问作者CDac
相关产品推荐
相关产品推荐

