如何在 countplot 中添加百分比?

huangapple go评论57阅读模式
英文:

How do I add a percentage to a countplot?

问题

I need to show a percentage for my bar graph. However I am not sure how to do it.

sns.set_style('whitegrid')
sns.countplot(y='type', data=df, palette='colorblind')
plt.xlabel('Count')
plt.ylabel('Type')
plt.title('Movie Type in Disney+')
plt.show()

如何在 countplot 中添加百分比?

英文:

I need to show a percentage for my bar graph. However I am not sure how to do it.

sns.set_style('whitegrid')
sns.countplot(y='type',data=df,palette='colorblind')
plt.xlabel('Count')
plt.ylabel('Type')
plt.title('Movie Type in Disney+')
plt.show()

如何在 countplot 中添加百分比?

答案1

得分: 5

ax = sns.countplot(y='type', data=df, palette='colorblind')

# 获取类型列的总计数
total = df['type'].count()

# 使用来自matplotlib v3.7.0的fmt注释条形图
ax.bar_label(ax.containers[0], fmt=lambda x: f'{(x/total)*100:0.1f}%')

# 在条形图的末尾添加标签的空间
ax.margins(x=0.1)

ax.set(xlabel='Count', ylabel='Type', title='Movie Type in Disney+')
plt.show()

如何在 countplot 中添加百分比?

  • 同样的实现也适用于垂直条形图
ax = sns.countplot(x='type', data=df, palette='colorblind')

# 获取类型列的总计数
total = df['type'].count()

# 使用来自matplotlib v3.7.0的fmt注释条形图
ax.bar_label(ax.containers[0], fmt=lambda x: f'{(x/total)*100:0.1f}%')
plt.show()

如何在 countplot 中添加百分比?

  • v3.4.0 <= matplotlib < v3.7.0 使用 labels 参数。
# 对于水平条形图
labels = [f'{(w/total)*100:0.1f}%' if (w := v.get_width()) > 0 else '' for v in ax.containers[0]]

# 对于垂直条形图
# labels = [f'{(h/total)*100:0.1f}%' if (h := v.get_height()) > 0 else '' for v in ax.containers[0]]

ax.bar_label(ax.containers[0], labels=labels)
英文:
ax = sns.countplot(y=&#39;type&#39;, data=df, palette=&#39;colorblind&#39;)

# get the total count of the type column
total = df[&#39;type&#39;].count()

# annotate the bars with fmt from matplotlib v3.7.0
ax.bar_label(ax.containers[0], fmt=lambda x: f&#39;{(x/total)*100:0.1f}%&#39;)

# add space at the end of the bar for the labels
ax.margins(x=0.1)

ax.set(xlabel=&#39;Count&#39;, ylabel=&#39;Type&#39;, title=&#39;Movie Type in Disney+&#39;)
plt.show()

如何在 countplot 中添加百分比?

  • The same implementation also works for vertical bars
ax = sns.countplot(x=&#39;type&#39;, data=df, palette=&#39;colorblind&#39;)

# get the total count of the type column
total = df[&#39;type&#39;].count()

# annotate the bars with fmt from matplotlib v3.7.0
ax.bar_label(ax.containers[0], fmt=lambda x: f&#39;{(x/total)*100:0.1f}%&#39;)
plt.show()

如何在 countplot 中添加百分比?

  • v3.4.0 &lt;= matplotlib &lt; v3.7.0 use the labels parameter.
# for horizontal bars
labels = [f&#39;{(w/total)*100:0.1f}%&#39; if (w := v.get_width()) &gt; 0 else &#39;&#39; for v in ax.containers[0]]

# for vertical bars
# labels = [f&#39;{(h/total)*100:0.1f}%&#39; if (h := v.get_height()) &gt; 0 else &#39;&#39; for v in ax.containers[0]]

ax.bar_label(ax.containers[0], labels=labels)

答案2

得分: 3

开始的代码看起来很棒!你只需通过将每个柱的宽度除以总计数并乘以100来计算每个柱的百分比值。然后使用annotate函数将你计算的这些值作为文本添加到柱上。尝试下面的代码,看看它是否适用于你!

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_style('whitegrid')

# 创建countplot并将其命名为'plot'。
plot = sns.countplot(y='type', data=df, palette='colorblind')

plt.xlabel('Count')
plt.ylabel('Type')
plt.title('Movie Type in Disney+')

total = len(df['type'])

for p in plot.patches:
    percentage = '{:.1f}%'.format(100 * p.get_width() / total)
    x = p.get_x() + p.get_width() + 0.02
    y = p.get_y() + p.get_height() / 2
    plot.annotate(percentage, (x, y))
plt.show()
英文:

The beginning code looks great! You just have to calculate the percentage values for each bar by dividing the width of the bar by the total count and multiplying by 100. Then use the annotate function to add those values you calculated as text to the bar. Try the code below and see if it works out for you!

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_style(&#39;whitegrid&#39;)

# Create the countplot and naming it &#39;plot&#39;. 
plot = sns.countplot(y=&#39;type&#39;, data=df, palette=&#39;colorblind&#39;)

plt.xlabel(&#39;Count&#39;)
plt.ylabel(&#39;Type&#39;)
plt.title(&#39;Movie Type in Disney+&#39;)

total = len(df[&#39;type&#39;]) 

for p in plot.patches:
    percentage = &#39;{:.1f}%&#39;.format(100 * p.get_width() / total)
    x = p.get_x() + p.get_width() + 0.02
    y = p.get_y() + p.get_height() / 2
    plot.annotate(percentage, (x, y))
plt.show()

huangapple
  • 本文由 发表于 2023年5月13日 09:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定