seaborn添加数据标签

In the course of my data exploration adventures, I find myself looking at such plots (below), which is great for observing trend but it makes it difficult to make out where and what each data point is.

在进行数据探索的过程中,我发现自己正在查看此类图(如下),这对于观察趋势非常有用,但是很难确定每个数据点的位置和位置。

How many passengers are there in 1956?
1956年有多少乘客?

The purpose of this piece of writing is to provide a quick guide in labelling common data exploration seaborn graphs. All the code used can be found here.

本文的目的是提供一个快速指南,以标记常见的数据探索海洋图。 所有使用的代码都可以在这里找到。

建立 (Set-Up)

Seaborn’s flights dataset will be used for the purposes of demonstration.

Seaborn的航班数据集将用于演示。

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as plt%matplotlib inline# load datasetflights = sns.load_dataset(‘flights’)flights.head()
First 5 rows of the the data in flights
排期中数据的前5行

For increased ease and convenience in creating some plots, some additional data frames can be created.

为了增加创建某些绘图的便利性和便利性,可以创建一些其他数据框。

# set up flights by year dataframeyear_flights = flights.groupby(‘year’).sum().reset_index()year_flights
Total number of passengers for each year
每年的乘客总数
# set up average number of passengers by month dataframemonth_flights = flights.groupby(‘month’).agg({‘passengers’: ‘mean’}).reset_index()month_flights
Total number of passengers for each month
每个月的乘客总数

线图 (Line Plot)

Plotting a graph of passengers per year:

绘制每年的乘客图:

# plot line graphsns.set(rc={‘figure.figsize’:(10,5)})ax = sns.lineplot(x=’year’, y=’passengers’, data=year_flights, marker=’*’, color=’#965786')ax.set(title=’Total Number of Passengers Yearly’)# label points on the plotfor x, y in zip(year_flights[‘year’], year_flights[‘passengers’]): # the position of the data label relative to the data point can be adjusted by adding/subtracting a value from the x &/ y coordinates plt.text(x = x, # x-coordinate position of data label y = y-150, # y-coordinate position of data label, adjusted to be 150 below the data point s = ‘{:.0f}’.format(y), # data label, formatted to ignore decimals color = ‘purple’) # set colour of line
Line plot showing the total number of passengers yearly.
折线图显示了每年的乘客总数。

At times, it would be preferable for the data label to be more visible, which can be achieved by adding a background colour to the data labels:

有时,最好使数据标签更可见,这可以通过向数据标签添加背景色来实现:

# add set_backgroundcolor(‘color’) after plt.text(‘…’)plt.text(x, y-150, ‘{:.0f}’.format(y), color=’white’).set_backgroundcolor(‘#965786’)
Line plot showing the total number of passengers yearly.
折线图显示了每年的乘客总数。

直方图 (Histogram)

Plotting a histogram of the frequency of passengers on each flight:

绘制每次航班上乘客频率的直方图:

# plot histogram ax = sns.distplot(flights[‘passengers’], color=’#9d94ba’, bins=10, kde=False)ax.set(title=’Distribution of Passengers’)# label each bar in histogramfor p in ax.patches: height = p.get_height() # get the height of each bar # adding text to each bar ax.text(x = p.get_x()+(p.get_width()/2), # x-coordinate position of data label, padded to be in the middle of the bar y = height+0.2, # y-coordinate position of data label, padded 0.2 above bar s = ‘{:.0f}’.format(height), # data label, formatted to ignore decimals ha = ‘center’) # sets horizontal alignment (ha) to center
Histogram showing the number of passengers on each flight.
直方图显示每次航班上的乘客人数。

An additional information that might be beneficial to reflect in the graph as well is the mean line of the dataset:

可能也有益于在图中反映的其他信息是数据集的平均线:

# plot histogram # …# adding a vertical line for the average passengers per flightplt.axvline(flights[‘passengers’].mean(), color=’purple’, label=’mean’)# adding data label to mean lineplt.text(x = flights[‘passengers’].mean()+3, # x-coordinate position of data label, adjusted to be 3 right of the data point y = max([h.get_height() for h in ax.patches]), # y-coordinate position of data label, to take max height  s = ‘mean: {:.0f}’.format(flights[‘passengers’].mean()), # data label color = ‘purple’) # colour of the vertical mean line# label each bar in histogram# …
Histogram showing the number of passengers on each flight and a line indicating the mean.
直方图显示每次航班上的乘客人数,线表示平均值。

条形图 (Bar Plot)

Vertical Bar Plot

垂直条形图

Plotting the total number of passengers for each year:

绘制每年的乘客总数:

# plot vertical barplotsns.set(rc={‘figure.figsize’:(10,5)})ax = sns.barplot(x=’year’, y=’passengers’, data=year_flights)ax.set(title=’Total Number of Passengers Yearly’) # title barplot# label each bar in barplotfor p in ax.patches: # get the height of each bar height = p.get_height() # adding text to each bar ax.text(x = p.get_x()+(p.get_width()/2), # x-coordinate position of data label, padded to be in the middle of the bar y = height+100, # y-coordinate position of data label, padded 100 above bar s = ‘{:.0f}’.format(height), # data label, formatted to ignore decimals ha = ‘center’) # sets horizontal alignment (ha) to center
Bar plot with vertical bars showing the total number of passengers yearly
竖线条形图,显示每年的乘客总数

Horizontal Bar Plot

水平条形图

Plotting the average number of passengers on flights each month:

绘制每月航班的平均乘客数:

# plot horizontal barplotsns.set(rc={‘figure.figsize’:(10,5)})ax = sns.barplot(x=’passengers’, y=’month’, data=month_flights, orient=’h’)ax.set(title=’Average Number of Flight Passengers Monthly’) # title barplot# label each bar in barplotfor p in ax.patches: height = p.get_height() # height of each horizontal bar is the same width = p.get_width() # width (average number of passengers) # adding text to each bar ax.text(x = width+3, # x-coordinate position of data label, padded 3 to right of bar y = p.get_y()+(height/2), # # y-coordinate position of data label, padded to be in the middle of the bar s = ‘{:.0f}’.format(width), # data label, formatted to ignore decimals va = ‘center’) # sets vertical alignment (va) to center
Bar plot with horizontal bars showing the average number of passengers for each month
带有水平条的条形图,显示每个月的平均乘客人数

使用注意事项 (Notes on Usage)

It might be beneficial to add data labels to some plots (especially bar plots), it would be good to experiment and test out different configurations (such as using labels only for certain meaningful points, instead of labelling everything) and not overdo the labelling, especially if there are many points. A clean and informative graph is usually more preferable than a cluttered one.

将数据标签添加到某些图(尤其是条形图)可能是有益的,尝试并测试不同的配置(例如仅对某些有意义的点使用标签,而不是对所有内容进行标签),并且不要过度标注,特别是如果有很多要点的话。 通常,干净整洁的图表比混乱的图表更可取。

# only labelling some points on graph# plot line graphsns.set(rc={‘figure.figsize’:(10,5)})ax = sns.lineplot(x=’year’, y=’passengers’, data=year_flights, marker=’*’, color=’#965786')# title the plotax.set(title=’Total Number of Passengers Yearly’)mean = year_flights[‘passengers’].mean()# label points on the plot only if they are higher than the meanfor x, y in zip(year_flights[‘year’], year_flights[‘passengers’]): if y > mean: plt.text(x = x, # x-coordinate position of data label y = y-150, # y-coordinate position of data label, adjusted to be 150 below the data point s = ‘{:.0f}’.format(y), # data label, formatted to ignore decimals color = ‘purple’) # set colour of line
Line plot showing the total number of passengers yearly.
折线图显示了每年的乘客总数。

翻译自: https://medium.com/swlh/quick-guide-to-labelling-data-for-common-seaborn-plots-736e10bf14a9

seaborn添加数据标签


http://www.taodudu.cc/news/show-5238368.html

相关文章:

  • python画图添加数据标签_matplotlib可视化之如何给图形添加数据标签?
  • 【数据治理】标签的分类、设计及实现方法
  • 怎么进行数据标签?
  • ubuntu nfs 服务器搭建
  • NFS服务器的搭建(文件共享)
  • Ubuntu NFS 服务器的搭建和使用
  • nfs服务器之间实现目录共享
  • linux中查看nfs日志,nfs服务器与日志服务器
  • 基于Kerberos认证的NFS服务器
  • Ubuntu 18.04搭建NFS服务器
  • Ubuntu nfs服务器共享文件夹
  • Windows系统下搭建NFS服务器
  • Ubuntu NFS 服务器和客户端安装、挂载
  • NFS介绍及服务器搭建
  • 卡特D型中文显示器PCB板图和原理图
  • 与小卡特一起学python 第1章 出发吧 课后练习题
  • LibreOJ #2478.「九省联考 2018」林克卡特树 树形dp+带权二分
  • 播放失败246106异常代码_卡特挖掘机故障代码(值得收藏)
  • [loj 2478][luogu P4843]「九省联考 2018」林克卡特树
  • [JZOJ5641] 林克卡特树【树形DP】【凸优化】
  • 小兔的棋盘(卡特蘭數)
  • JSOI2018 DAY 2 T2 林克卡特树
  • 八省联考林克卡特树【题解】
  • 解题:八省联考2018 林克卡特树
  • P4383 [八省联考2018]林克卡特树lct
  • [学习笔记]dp凸优化/wqs二分[八省联考2018]林克卡特树lct
  • [八省联考2018]林克卡特树lct
  • LuoguP4383 [八省联考2018]林克卡特树lct
  • 洛谷P4383 [八省联考2018]林克卡特树
  • BZOJ 5252 林克卡特树 —— 树形dp + wqs二分

seaborn添加数据标签_常见Seaborn图的数据标签快速指南相关推荐

  1. 【Excel】绘图案例_常见复合图:簇状图+堆积图+折线图

    [Excel]绘图案例_常见复合图:簇状图+堆积图+折线图 前言 最近有朋友让我帮忙用excel画图,老实说我很讨厌用excel画图,点来点去,复杂一些还不能复用,非常繁琐.当然,入门也很简单.需求时 ...

  2. 基于plotly数据可视化_如何使用Plotly进行数据可视化

    基于plotly数据可视化 The amount of data in the world is growing every second. From sending a text to clicki ...

  3. keras时间序列数据预测_使用Keras的时间序列数据中的异常检测

    keras时间序列数据预测 Anomaly Detection in time series data provides e-commerce companies, finances the insi ...

  4. python交互式数据可视化_基于Python实现交互式数据可视化的工具,你用过几种?...

    作者:Alark Joshi 翻译:陈雨琳 来源:数据派THU(ID:DatapiTHU) 我教授了一门关于数据可视化的数据科学硕士课程.我们的数据科学硕士项目是一个为期15个月的强化项目,这个项目已 ...

  5. python实现数据可视化_使用Matplotib python实现数据可视化

    python实现数据可视化 I Feel: 我觉得: In today's digital world data has become as important as air. Machines &a ...

  6. python怎么用大数据分析师_如何七周成为数据分析师18:Python的新手教程

    本文是<怎样 七周成为数据剖析 师>的第十八篇教程,假定 想要了解 写作初衷,能够 先行阅读七周指南.温馨提示:假定 您曾经 熟习 Python,大可不用 再看这篇文章,或只选择 部分 . ...

  7. python如何设置标签_[pyecharts1.8] 系列配置之标签设置

    本文档(以及pyecharts使用手册中的其他文档)将会持续更新. 有些内容标记为待更新的,有时间我会补充上.有问题的可以在评论处留言,或者可关注微信公众号「燃烧吧数据」(id:data-fired) ...

  8. 数据多重共线性_多重共线性对您的数据科学项目的影响比您所知道的要多

    数据多重共线性 Multicollinearity is likely far down on a mental list of things to check for, if it is on a ...

  9. python大数据免费_用python做大数据

    不学Python迟早会被淘汰?Python真有这么好的前景? 最近几年Python编程语言在国内引起不小的轰动,有超越Java之势,本来在美国这个编程语言就是最火的,应用的非常非常的广泛,而Pytho ...

最新文章

  1. ATS插件开发中内存泄露问题的解决方法探讨
  2. 在 Mac OSX 版的 LispBox 环境上安装配置 SBCL 详细过程
  3. 进程知识点,只需这一篇
  4. VC++读写INI文件示例
  5. jqgrid多选和禁止某行记录选择
  6. 获取本地ip(一个或多个都能取)
  7. 新买阿里云linux服务器如何设置账号密码xshell远程登陆
  8. /usr/include/openssl/des.h:91:9: error: unknown type name ‘DES_LONG’ DES_LONG deslong[2];
  9. Django(五):后台管理平台admin
  10. 微信小程序反编译解包教程
  11. 树莓派无线投屏服务器,树莓派 Raspberry Pi 设置无线上网
  12. 阿里巴巴Java开发规范手册(详尽版)——免费下载
  13. java .jar怎么打开_jar文件怎么打开,教您如何打开jar文件
  14. 阿里高管的思考方式真正厉害在哪?内部员工7000字深度干货
  15. SQL Server的3种恢复模式(Simple,Full,Bulk-logged)
  16. Tesseract 教程
  17. android7.1的SnapdragonCamera之CameraActivity逻辑流程分析整体
  18. 基于51单片机智能温控风扇设计PWM调速套件电子制作仿真
  19. webstorm提示TypeError: this.cliEngine is not a constructor
  20. python 列表迭代_Python | 以相反的顺序迭代列表

热门文章

  1. python程序员专用壁纸_Python开发专属壁纸下载与轮换程序
  2. WPS Office 保存时间太长
  3. [转载]如何解决failed to push some refs to git
  4. 车联网系统会和哪些主机厂系统对接?
  5. 【FBS外汇公司】通胀数据降低,美联储量化宽松预期升温
  6. 购物网站前端页面的制作
  7. hubuild 打包ios_iOS | 使用HBuilder进行本地打包步骤
  8. 关于量子计算机的中考试题,中考语文试题真题汇编现代文阅读理解训练百篇(附答案)...
  9. 三国志战略版:许攸阵容初尝试,官渡之战
  10. IOS 学习 gei post 同步 异步方法