Tjs*_*s01 6 python matplotlib pandas seaborn
我有一个看起来像的数据框:
User A B C
ABC 100 121 OPEN
BCD 200 255 CLOSE
BCD 500 134 OPEN
DEF 600 125 CLOSE
ABC 900 632 OPEN
ABC 150 875 CLOSE
DEF 690 146 OPEN
Run Code Online (Sandbox Code Playgroud)
我正在尝试在“用户”列上显示计数图。代码如下:
fig, ax1 = plt.subplots(figsize=(20,10))
graph = sns.countplot(ax=ax1,x='User', data=df)
graph.set_xticklabels(graph.get_xticklabels(),rotation=90)
for p in graph.patches:
height = p.get_height()
graph.text(p.get_x()+p.get_width()/2., height + 0.1,
'Hello',ha="center")
Run Code Online (Sandbox Code Playgroud)
输出看起来像:
但是,我想用列“用户”的 value_counts 替换字符串“Hello”。当我添加代码以向图形添加标签时:
for p in graph.patches:
height = p.get_height()
graph.text(p.get_x()+p.get_width()/2., height + 0.1,
df['User'].value_counts(),ha="center")
Run Code Online (Sandbox Code Playgroud)
我得到的输出为:
tdy*_*tdy 66
我们现在可以使用内置的自动注释条形图Axes.bar_label,因此我们需要做的就是访问/提取seaborn图的Axes.
Seaborn 提供了多种绘制计数的方法,每种方法的计数聚合和Axes处理略有不同:
seaborn.countplot(最简单)这会自动聚合计数并返回一个Axes,所以只需直接 label ax.containers[0]:
ax = sns.countplot(x='User', data=df)
ax.bar_label(ax.containers[0])
Run Code Online (Sandbox Code Playgroud)
seaborn.catplot( kind='count')这会将 a 绘制countplot到小平面网格上,因此Axes在标记之前从网格中提取 a ax.containers[0]:
g = sns.catplot(x='User', kind='count', data=df)
for ax in g.axes.flat:
ax.bar_label(ax.containers[0])
Run Code Online (Sandbox Code Playgroud)
seaborn.barplot这会返回一个Axes但不会聚合计数,因此Series.value_counts在标记之前首先计算ax.containers[0]:
counts = df['User'].value_counts().rename_axis('user').reset_index(name='count')
ax = sns.barplot(x='user', y='count', data=counts)
ax.bar_label(ax.containers[0])
Run Code Online (Sandbox Code Playgroud)
如果您正在使用hue:
hue绘图将包含多个条形容器,因此ax.containers需要迭代:
ax = sns.countplot(x='User', hue='C', data=df)
for container in ax.containers:
ax.bar_label(container)
Run Code Online (Sandbox Code Playgroud)
Dan*_*bbe 10
df['User'].value_counts()将返回一个包含列 User 的唯一值计数的系列。
无需详细分析您的代码,您可以通过使用计数器索引 value_counts 的结果来更正它:
fig, ax1 = plt.subplots(figsize=(20,10))
graph = sns.countplot(ax=ax1,x='User', data=df)
graph.set_xticklabels(graph.get_xticklabels(),rotation=90)
i=0
for p in graph.patches:
height = p.get_height()
graph.text(p.get_x()+p.get_width()/2., height + 0.1,
df['User'].value_counts()[i],ha="center")
i += 1
Run Code Online (Sandbox Code Playgroud)
使用您的示例数据,它会生成以下图:
正如@ImportanceOfBeingErnest 所建议的那样,以下代码使用更简单的代码生成相同的输出,使用高度变量本身而不是索引的 value_counts:
fig, ax1 = plt.subplots(figsize=(20,10))
graph = sns.countplot(ax=ax1,x='User', data=df)
graph.set_xticklabels(graph.get_xticklabels(),rotation=90)
for p in graph.patches:
height = p.get_height()
graph.text(p.get_x()+p.get_width()/2., height + 0.1,height ,ha="center")
Run Code Online (Sandbox Code Playgroud)
注意:此解决方案不会尝试在栏顶部显示计数。相反,这个简单的解决方案将打印条形内的值。对于某些场合,这可能是一个优雅的解决方案。
import seaborn as sns
ax=sns.countplot(x=df['category'], data=df);
for p in ax.patches:
ax.annotate(f'\n{p.get_height()}', (p.get_x()+0.2, p.get_height()), ha='center', va='top', color='white', size=18)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9864 次 |
| 最近记录: |