如何在Seaborn countplot中隐藏0值的柱状图标签
问题:如何在Seaborn countplot中隐藏0值的标签?

我不希望在countplot中显示0值,有没有办法仅从图表的标签中去掉0值的显示?
用户当前代码:
ax = sbn.countplot(data=df,x='link',hue='Status', width=0.2) for container in ax.containers: ax.bar_label(container,)
解决方案
你可以通过自定义bar_label的标签内容,过滤掉值为0的条目,修改后的代码如下:
ax = sbn.countplot(data=df, x='link', hue='Status', width=0.2) for container in ax.containers: # 只保留非0值的标签,0值对应位置设为空字符串 labels = [str(val) if val != 0 else "" for val in container.datavalues] ax.bar_label(container, labels=labels)
这样处理后,图表上就只会显示非0数值的标签,0值的位置不会有任何文字显示。
内容的提问来源于stack exchange,提问作者saas
相关产品推荐
相关产品推荐

