本文介绍了如何在seaborn/matplotlib中绘制和注释分组条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的数据框:
我使用条形图来表示每一行的订阅者.这就是我所做的:
data = channels.sort_values('subscribers', Ascending=False).head(5)图表 = sns.barplot(x = 'name', y='subscribers',data=data)chart.set_xticklabels(chart.get_xticklabels(),旋转= 90)对于 chart.patches 中的 p:chart.annotate("{:,.2f}".format(p.get_height(), '.2f'), (p.get_x() + p.get_width()/2., p.get_height()), ha = 'center', va = 'center', xytext = (0, 10), textcoords = 'offset points')
现在我想在同一张图中显示每个用户的video_count".目标是比较订阅者数量与视频数量之间的关系.我如何在图表上描述这一点?
解决方案
数据
- 需要使用
注释资源 - 来自
matplotlib v3.4.2
- 在 matplotlib 条形图上添加值标签
- 如何注释堆积条形图的每一段
- 带有居中标签的堆积条形图
- 如何使用聚合值注释 seaborn 条形图
- matplotlib 中的堆栈条形图并为每个部分添加标签
- 如何向条形图添加多个注释
- 如何绘制和注释分组条形图
使用
绘图seaborn v0.11.1
- 在 3.4.2 版本之前使用
matplotlib
- 请注意,使用
.annotate
和.patches
比使用.bar_label
更加冗长.
# plot图, ax = plt.subplots(figsize=(12, 6))sns.barplot(x='name', y='values', data=dfl,hue='cats', ax=ax)ax.set_xticklabels(chart.get_xticklabels(), 旋转=0)ax.set_yscale('log')对于 ax.patches 中的 p:ax.annotate(f"{p.get_height():.0f}", (p.get_x() + p.get_width()/2., p.get_height()),ha='center', va='center', xytext =(0, 7), textcoords='offset points')
I have a dataframe that looks like this:
I have used a barplot to represent the subscribers for each row. This is what I did:
data = channels.sort_values('subscribers', ascending=False).head(5) chart = sns.barplot(x = 'name', y='subscribers',data=data) chart.set_xticklabels(chart.get_xticklabels(), rotation=90) for p in chart.patches: chart.annotate("{:,.2f}".format(p.get_height(), '.2f'), (p.get_x() + p.get_width() / 2., p.get_height()), ha = 'center', va = 'center', xytext = (0, 10), textcoords = 'offset points')
Now I want to show the 'video_count' for each user on this same plot. The goal is to compare how the number of subscribers relate to the number of videos. How can I depict this on the chart?
解决方案Data
- The data needs to be converted to a long format using
.melt
- Because of the scale of values,
'log'
is used for theyscale
- All of the categories in
'cats'
are included for the example.- Select only the desired columns before melting, or use
dfl = dfl[dfl.cats.isin(['sub', 'vc'])
to filter for the desired'cats'
.
- Select only the desired columns before melting, or use
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # setup dataframe data = {'vc': [76, 47, 140, 106, 246], 'tv': [29645400, 28770702, 50234486, 30704017, 272551386], 'sub': [66100, 15900, 44500, 37000, 76700], 'name': ['a', 'b', 'c', 'd', 'e']} df = pd.DataFrame(data) vc tv sub name 0 76 29645400 66100 a 1 47 28770702 15900 b 2 140 50234486 44500 c # convert to long form dfl = (df.melt(id_vars='name', var_name='cats', value_name='values') .sort_values('values', ascending=False).reset_index(drop=True)) name cats values 0 e tv 272551386 1 c tv 50234486 2 d tv 30704017
Updated as of
matplotlib v3.4.2
- Use
matplotlib.pyplot.bar_label
.bar_label
works formatplotlib
,seaborn
, andpandas
plots.- See the matplotlib: Bar Label Demo page for additional formatting options.
- Tested with
seaborn v0.11.1
, which is usingmatplotlib
as the plot engine.
# plot fig, ax = plt.subplots(figsize=(12, 6)) sns.barplot(x='name', y='values', data=dfl, hue='cats', ax=ax) ax.set_xticklabels(ax.get_xticklabels(), rotation=0) ax.set_yscale('log') for c in ax.containers: # set the bar label ax.bar_label(c, fmt='%.0f', label_type='edge', padding=1) # pad the spacing between the number and the edge of the figure ax.margins(y=0.1)
Annotation Resources - from
matplotlib v3.4.2
- Adding value labels on a matplotlib bar chart
- How to annotate each segment of a stacked bar chart
- Stacked Bar Chart with Centered Labels
- How to annotate a seaborn barplot with the aggregated value
- stack bar plot in matplotlib and add label to each section
- How to add multiple annotations to a barplot
- How to plot and annotate a grouped bar chart
Plot with
seaborn v0.11.1
- Using
matplotlib
before version 3.4.2 - Note that using
.annotate
and.patches
is much more verbose than with.bar_label
.
# plot fig, ax = plt.subplots(figsize=(12, 6)) sns.barplot(x='name', y='values', data=dfl, hue='cats', ax=ax) ax.set_xticklabels(chart.get_xticklabels(), rotation=0) ax.set_yscale('log') for p in ax.patches: ax.annotate(f"{p.get_height():.0f}", (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', xytext =(0, 7), textcoords='offset points')
这篇关于如何在seaborn/matplotlib中绘制和注释分组条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!