Seaborn Catplot set values over the bars(Seaborn Catplot 在条形图上设置值)
本文介绍了Seaborn Catplot 在条形图上设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我像这样在seaborn中绘制了一个catplot
将 seaborn 导入为 sns将熊猫导入为 pd数据= {'年':[2016、2013、2014、2015、2016、2013、2014、2015、2016、2013、2014、2015、2016、2013、2014、2015、2016、2013、2014、2015]geo_name': ['Michigan', 'Michigan', 'Michigan', 'Michigan', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Ann密歇根州阿伯市"、密歇根州安娜堡"、密歇根州安娜堡"、密歇根州安娜堡"、宾夕法尼亚州费城"、宾夕法尼亚州费城"、宾夕法尼亚州费城"、宾夕法尼亚州费城"、'密歇根州安娜堡市都会区', '密歇根州安娜堡市都会区', '密歇根州安娜堡市都会区', '密歇根州安娜堡市都会区'], 'geo': ['04000US26', '04000US26','04000us26','04000us26','05000us26161','05000us26161','05000us26161','16000us2603000','16000us2603000','16000us2603000','16000us4260000','16000us4260000','16000US4260000','16000US4260000','16000US42600','16000US42600','16000US42600','16000US42600','16000US42600','16000US42600','16000US4260000''16000US4260000'','16000US4260000','31000US11460','31000US11460','31000US11460','31000us11460','收入':[50803.0,48411.0,49087.0,49576.0,62484.0,59055.0,60805.0,61003.0,57697.0,59003.0,56835.0,55990.0,39770.0,37192.0,37460.0,38253.0,62484.0,59055.0,60805.0,61003.0],收入_MOE":[162.0,163.0,192.0,186.0,984.0,985.0,958.0,901.0,2046.0,1688.0,1320.0,1259.0,567.0,424.0、430.0、511.0、984.0、985.0、958.0、901.0]}df = pd.DataFrame(数据)g = sns.catplot(x='year', y='income', data=df, kind='bar', hue='geo_name', legend=True)g.fig.set_size_inches(15,8)g.fig.subplots_adjust(top=0.81,right=0.86)我得到如下所示的输出
我想在 K 表示的顶部添加每个条的值.例如在 2013 中,Michigan 的栏位于 48411 所以我想在上面添加值 48.4K酒吧.对于所有的酒吧也是如此.
解决方案
更新至matplotlib v3.4.2
- 使用
对于单个或多个地块
g = sns.catplot(x='year', y='income', data=df, kind='bar', col='geo_name',col_wrap=3,图例=真)g.fig.set_size_inches(15, 8)g.fig.subplots_adjust(top=0.9)g.fig.suptitle('带注释的条形计数')# 遍历轴对于 g.axes.ravel() 中的 ax:# 添加注释对于 ax.containers 中的 c:标签 = [f'{(v.get_height()/1000):.1f}K' for v in c]ax.bar_label(c, 标签=标签, label_type='edge')ax.margins(y=0.2)plt.show()I plotted a
catplotinseabornlike thisimport seaborn as sns import pandas as pd data = {'year': [2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015, 2016, 2013, 2014, 2015], 'geo_name': ['Michigan', 'Michigan', 'Michigan', 'Michigan', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Washtenaw County, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Ann Arbor, MI', 'Philadelphia, PA', 'Philadelphia, PA', 'Philadelphia, PA', 'Philadelphia, PA', 'Ann Arbor, MI Metro Area', 'Ann Arbor, MI Metro Area', 'Ann Arbor, MI Metro Area', 'Ann Arbor, MI Metro Area'], 'geo': ['04000US26', '04000US26', '04000US26', '04000US26', '05000US26161', '05000US26161', '05000US26161', '05000US26161', '16000US2603000', '16000US2603000', '16000US2603000', '16000US2603000', '16000US4260000', '16000US4260000', '16000US4260000', '16000US4260000', '31000US11460', '31000US11460', '31000US11460', '31000US11460'], 'income': [50803.0, 48411.0, 49087.0, 49576.0, 62484.0, 59055.0, 60805.0, 61003.0, 57697.0, 55003.0, 56835.0, 55990.0, 39770.0, 37192.0, 37460.0, 38253.0, 62484.0, 59055.0, 60805.0, 61003.0], 'income_moe': [162.0, 163.0, 192.0, 186.0, 984.0, 985.0, 958.0, 901.0, 2046.0, 1688.0, 1320.0, 1259.0, 567.0, 424.0, 430.0, 511.0, 984.0, 985.0, 958.0, 901.0]} df = pd.DataFrame(data) g = sns.catplot(x='year', y='income', data=df, kind='bar', hue='geo_name', legend=True) g.fig.set_size_inches(15,8) g.fig.subplots_adjust(top=0.81,right=0.86)I am getting an output like shown below
I want to add the values of each bar on its top in K representation. For example in
2013the bar forMichiganis at48411so I want to add the value48.4Kon top of that bar. Likewise for all the bars.解决方案Updated as of
matplotlib v3.4.2- Use
matplotlib.pyplot.bar_label - See the matplotlib: Bar Label Demo page for additional formatting options.
- Tested with
pandas v1.2.4, which is usingmatplotlibas the plot engine. - Use the
fmtparameter for simple formats, andlabelsparameter for customized string formatting. - See Adding value labels on a matplotlib bar chart for other plotting options related to the new method.
For single plot only
g = sns.catplot(x='year', y='income', data=df, kind='bar', hue='geo_name', legend=True) g.fig.set_size_inches(15, 8) g.fig.subplots_adjust(top=0.81, right=0.86) # extract the matplotlib axes_subplot objects from the FacetGrid ax = g.facet_axis(0, 0) # iterate through the axes containers for c in ax.containers: labels = [f'{(v.get_height() / 1000):.1f}K' for v in c] ax.bar_label(c, labels=labels, label_type='edge')For single or multiple plots
g = sns.catplot(x='year', y='income', data=df, kind='bar', col='geo_name', col_wrap=3, legend=True) g.fig.set_size_inches(15, 8) g.fig.subplots_adjust(top=0.9) g.fig.suptitle('Bar Count with Annotations') # iterate through axes for ax in g.axes.ravel(): # add annotations for c in ax.containers: labels = [f'{(v.get_height() / 1000):.1f}K' for v in c] ax.bar_label(c, labels=labels, label_type='edge') ax.margins(y=0.2) plt.show()这篇关于Seaborn Catplot 在条形图上设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
- Use
编程基础网
本文标题为:Seaborn Catplot 在条形图上设置值
基础教程推荐
猜你喜欢
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
