You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Seaborn柱状图未正确展示count值,如何修正该问题?

问题原因与解决方案

问题根源

seaborn的barplot默认会对同一分类(y轴的artikelenlijst)下的所有x值(count)计算均值。看你的数据示例,artikel 14 van de Grondwaterwet有两个count值:40和17,均值为(40+17)/2=28.5,所以柱子显示的是这个均值,而非单个数据点的数值。

解决方案

根据你的需求选择以下两种方式:

1. 展示每个分类的总计数(最可能符合你的需求)

先对数据按artikelenlijst分组求和,确保每个分类只有一行对应的总count值,再绘图:

import seaborn as sns

# 分组求和,重置索引得到新的DataFrame
top30_art_agg = top30_art.groupby('artikelenlijst')['count'].sum().reset_index()

# 绘制柱状图
sns.barplot(x='count', y='artikelenlijst', data=top30_art_agg, label="frequency", color='r', ci=None)

2. 展示所有原始数据点的柱状图(无聚合)

如果需要保留每一行数据对应的柱子(注意重复分类的柱子会重叠),可以直接用matplotlib的barh绘制:

import matplotlib.pyplot as plt

plt.barh(top30_art['artikelenlijst'], top30_art['count'], color='r')
plt.xlabel('count')
plt.ylabel('artikelenlijst')
plt.legend(['frequency'])
plt.show()

若要避免重复分类的柱子重叠,可以给重复的artikelenlijst添加唯一标识(比如后缀序号),再进行绘制。


内容的提问来源于stack exchange,提问作者Datafan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 09:55:16