seaborn绘制精美的散点图、柱状图、直方图和核密度图

公众号:尤而小屋

作者:Peter

编辑:Peter

大家好,我是Peter~

seaborn绘制精美图形又更新了:

本文给大家介绍如何使用seaborn绘制4类图形:

  • 散点图scatterplot
  • 柱状图barplot
  • 直方图histplot
  • 核密度估计图kdeplot

1 导入库

In [1]:

python 复制代码
import pandas as pd
import numpy as np

import matplotlib
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"]=["SimHei"] # 设置字体
plt.rcParams["axes.unicode_minus"]=False # 解决"-"负号的乱码问题

import seaborn as sns
%matplotlib inline 

import warnings
warnings.filterwarnings("ignore")

2 散点图scatterplot

使用内置消费数据集:

In [2]:

ini 复制代码
tips = sns.load_dataset("tips")
tips.head()

Out[2]:

total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

绘制基础散点图:

In [3]:

ini 复制代码
sns.scatterplot(data=tips, x="total_bill",y="tip")

plt.show()

使用hue参数进行分组:

1、如果hue参数是分类型,每个组是不同的颜色:

In [4]:

ini 复制代码
sns.scatterplot(data=tips, x="total_bill",y="tip",hue="day")

plt.show()

2、如果给定的hue是数值型,那么将会是渐变型的颜色:

In [5]:

ini 复制代码
sns.scatterplot(data=tips, x="total_bill",y="tip",hue="size")

plt.show()

此时如果也想显示不同的颜色,可以再配合使用palette参数:

In [6]:

ini 复制代码
sns.scatterplot(data=tips, 
                x="total_bill",
                y="tip",
                hue="size", # 分组字段
                palette="deep"  # 使用的调色板
               )

plt.show()

使用style参数将散点标记为不同marker:

1、当hue和style参数是同一个字段:

In [7]:

ini 复制代码
# hue和style相同

sns.scatterplot(data=tips, x="total_bill",y="tip",hue="time",style="time")

plt.show()

2、当hue和style是不同的字段:

In [8]:

ini 复制代码
# hue和style不同

sns.scatterplot(data=tips, x="total_bill",y="tip",hue="day",style="time")

plt.show()

size参数表示散点的大小:

In [9]:

ini 复制代码
sns.scatterplot(data=tips, x="total_bill",y="tip",hue="size",size="size")

plt.show()

通过参数sizes指定点的大小范围:

In [10]:

ini 复制代码
sns.scatterplot(data=tips, x="total_bill",y="tip",
                hue="size",
                size="size", 
                sizes=(20,200),  # 指定范围
                legend="full"  # 保证每个值出现在图例中
               )

plt.show()

不同markers标记的指定:

In [11]:

ini 复制代码
markers = {"Lunch": "s", "Dinner": "X"}

sns.scatterplot(data=tips, x="total_bill", y="tip", style="time", markers=markers)

plt.show()

通过marker指定为某个标记,而不是默认的圆点:

In [12]:

ini 复制代码
sns.scatterplot(data=tips, x="total_bill", y="tip",s=100, marker="+") # 十字星

plt.show()

使用五角星为标记符:

In [13]:

ini 复制代码
sns.scatterplot(data=tips, x="total_bill", y="tip",s=100, marker="*") # 五角星

plt.show()

通过relplot函数的kind参数指定为scatter也可以实现散点图的绘制:

In [14]:

ini 复制代码
sns.relplot(data=tips,
            x="total_bill",
            y="tip",
            kind="scatter", # 指定为散点图
            col="time",
            hue="day",
            style="day"
           )

plt.show()

3 柱状图barplot

In [15]:

ini 复制代码
penguins = sns.load_dataset("penguins")
penguins.head()

Out[15]:

species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female
2 Adelie Torgersen 40.3 18.0 195.0 3250.0 Female
3 Adelie Torgersen NaN NaN NaN NaN NaN
4 Adelie Torgersen 36.7 19.3 193.0 3450.0 Female

In [16]:

复制代码
penguins.columns

Out[16]:

css 复制代码
Index(['species', 'island', 'bill_length_mm', 'bill_depth_mm',       'flipper_length_mm', 'body_mass_g', 'sex'],
      dtype='object')

基础柱状图:

In [17]:

ini 复制代码
sns.barplot(penguins, x="island", y="bill_length_mm")

plt.show()

绘制水平柱状图,只需要将x和y的数据进行对调:

In [18]:

ini 复制代码
sns.barplot(penguins, x="bill_length_mm", y="island")

plt.show()

使用hue参数进行分组:

In [19]:

ini 复制代码
sns.barplot(penguins, x="island", y="bill_length_mm", hue="sex")

plt.show()

带上误差棒errorbar:

In [20]:

ini 复制代码
sns.barplot(penguins, x="island", y="bill_length_mm", errorbar="sd")

plt.show()

航班数据集flights:

In [21]:

ini 复制代码
flights = sns.load_dataset("flights")

flights.head()

Out[21]:

year month passengers
0 1949 Jan 112
1 1949 Feb 118
2 1949 Mar 132
3 1949 Apr 129
4 1949 May 121

生成透视宽表的形式:

In [22]:

ini 复制代码
# 生成宽表
flights_wide = flights.pivot(index="year", columns="month", values="passengers")
flights_wide

Out[22]:

month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
year
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
1954 204 188 235 227 234 264 302 293 259 229 203 229
1955 242 233 267 269 270 315 364 347 312 274 237 278
1956 284 277 317 313 318 374 413 405 355 306 271 306
1957 315 301 356 348 355 422 465 467 404 347 305 336
1958 340 318 362 348 363 435 491 505 404 359 310 337
1959 360 342 406 396 420 472 548 559 463 407 362 405
1960 417 391 419 461 472 535 622 606 508 461 390 432

对宽表整体数据的绘图:

In [23]:

scss 复制代码
sns.barplot(flights_wide)

plt.show()

取消误差棒,同时在x轴上进行求和:

In [24]:

ini 复制代码
sns.barplot(flights,
            x="year",
            y="passengers",
            estimator="sum",  # 求和的方式
            errorbar=None  # 不显示误差棒
           )

plt.show()

给柱状图添加数据信息:

In [25]:

ini 复制代码
ax = sns.barplot(flights, x="year", y="passengers", estimator="sum", errorbar=None)

# 添加数据信息
ax.bar_label(ax.containers[0], fontsize=10);

绘制水平柱状图:

In [26]:

ini 复制代码
sns.barplot(flights,
            x="passengers",
            y="year",
            estimator="sum",  # 求和的方式
            errorbar=None,  # 不显示误差棒
            orient="h"  # h-水平  v-垂直
           )

plt.show()

4 直方图histplot

直方图(Histogram)是一种统计报告图,主要用于表示连续变量(定量变量)的概率分布。这种图由一系列高度不等的纵向条纹或线段组成,通常用横轴表示数据类型,纵轴表示分布情况。

In [27]:

scss 复制代码
penguins.head()

Out[27]:

species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female
2 Adelie Torgersen 40.3 18.0 195.0 3250.0 Female
3 Adelie Torgersen NaN NaN NaN NaN NaN
4 Adelie Torgersen 36.7 19.3 193.0 3450.0 Female

In [28]:

复制代码
penguins.columns

Out[28]:

css 复制代码
Index(['species', 'island', 'bill_length_mm', 'bill_depth_mm',       'flipper_length_mm', 'body_mass_g', 'sex'],
      dtype='object')

垂直方向上的直方图:

In [29]:

ini 复制代码
sns.histplot(data=penguins, x="bill_length_mm")

plt.show()

水平方向上的直方图:

In [30]:

ini 复制代码
sns.histplot(data=penguins, y="bill_length_mm")

plt.show()

设置箱体宽度binwidth和箱体个数bins:

In [31]:

ini 复制代码
sns.histplot(data=penguins, 
             x="bill_length_mm",
             binwidth=0.5,  # 箱体宽度
             bins=30  # 箱体个数
            )

plt.show()

直方图配合KDE图:

In [32]:

ini 复制代码
sns.histplot(data=penguins, x="bill_length_mm",kde=True)

plt.show()

使用hue参数进行分组:

In [33]:

ini 复制代码
sns.histplot(data=penguins, x="bill_length_mm",hue="sex")

plt.show()

hue和kde参数配合使用:

In [34]:

ini 复制代码
sns.histplot(data=penguins, x="bill_length_mm",hue="sex",kde=True)

plt.show()

参数multiple指定直方图叠加方式:

  • layer-默认
  • stack
  • fill
  • dodge

In [35]:

shell 复制代码
# # 默认是layer
# sns.histplot(data=penguins, 
#              x="bill_length_mm",
#              hue="sex",
#              multiple="layer"   
#             )

# plt.show()

In [36]:

ini 复制代码
sns.histplot(data=penguins, 
             x="bill_length_mm",
             hue="sex",
             multiple="stack"   #  不同取值['layer', 'stack', 'fill', 'dodge']
            )
plt.show()

使用其他不同的取值:

In [37]:

ini 复制代码
sns.histplot(data=penguins, 
             x="bill_length_mm",
             hue="sex",
             multiple="fill"   #  不同取值['layer', 'stack', 'fill', 'dodge']
            )

plt.show()

In [38]:

python 复制代码
sns.histplot(data=penguins, 
             x="bill_length_mm",
             hue="sex",
             multiple="dodge"   #  不同取值['layer', 'stack', 'fill', 'dodge']
            )
plt.show()

关于shrink参数的使用:分组之间的间隔

In [39]:

ini 复制代码
sns.histplot(data=penguins, 
             x="bill_length_mm",
             hue="sex",
             multiple="dodge",
             shrink=0.5
            )
plt.show()

关于element参数的使用:

  • bars:默认
  • step
  • poly

In [40]:

ini 复制代码
sns.histplot(data=penguins, 
             x="bill_length_mm",
             hue="sex",
             element="step"  # ['bars', 'step', 'poly']
            )

plt.show()

In [41]:

python 复制代码
sns.histplot(data=penguins, 
             x="bill_length_mm",
             hue="sex",
             element="poly"  #  ['bars', 'step', 'poly']
            )

plt.show()

5 核密度估计图kdeplot

核密度估计(Kernel Density Estimation,简称KDE)是一种非参数的估计方法,用于估计一个随机变量的概率密度函数。

它通过对数据集中的每个点应用一个核函数(如高斯核),然后将所有这些核函数叠加起来,形成一个平滑的密度曲线。这种方法可以很好地捕捉数据的分布特征,尤其是在样本量较小或有异常值时,比传统的直方图和箱线图更能反映数据的真实分布。

基础核密度估计图:

In [42]:

scss 复制代码
tips.head()

Out[42]:

total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

In [43]:

ini 复制代码
sns.kdeplot(data=tips, x="tip")

plt.grid()
plt.show()

在水平方向上的核密度图:

In [44]:

ini 复制代码
sns.kdeplot(data=tips, y="tip")

plt.grid()
plt.show()

鸢尾花iris数据集:

In [45]:

ini 复制代码
iris = sns.load_dataset("iris")
iris.head()

Out[45]:

sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa

In [46]:

scss 复制代码
sns.kdeplot(data=iris)

plt.grid()
plt.show()

调整曲线的平滑度:

In [47]:

ini 复制代码
sns.kdeplot(data=tips, x="tip",bw_adjust=0.8)

plt.grid()
plt.show()

使用hue进行分组:

In [48]:

ini 复制代码
sns.kdeplot(data=tips, x="tip",hue="day")

plt.grid()
plt.show()

使用数值型的数据作为hue参数的取值:

In [49]:

ini 复制代码
sns.kdeplot(data=tips, x="tip",hue="size")

plt.grid()
plt.show()

可以看到颜色是同一个色系的渐变色。也可以改变颜色:

In [50]:

ini 复制代码
sns.kdeplot(data=tips, 
            x="tip",
            hue="size",
            fill=True,
            common_norm=False,
            palette="Spectral_r",  # 指定调色板
            alpha=0.8,  # 透明度
            linewidth=3  # 线宽
           )

plt.grid()
plt.show()

参数multiple的使用:指定图形的叠加方式

  • layer:默认
  • stack
  • fill

In [51]:

ini 复制代码
sns.kdeplot(data=tips, 
            x="tip", 
            hue="day", 
            multiple="layer"  # 默认是layer
           )

plt.show()

使用其他不同的参数:

In [52]:

ini 复制代码
sns.kdeplot(data=tips, 
            x="tip", 
            hue="day", 
            multiple="stack"  # 不同取值['layer', 'stack', 'fill']
           )

plt.show()

In [53]:

python 复制代码
sns.kdeplot(data=tips, 
            x="tip", 
            hue="day", 
            multiple="fill"  # 不同取值['layer', 'stack', 'fill']
           )

plt.show()

经验累积分布函数的使用:

In [54]:

ini 复制代码
sns.kdeplot(data=tips, 
            x="tip", 
            hue="day",
            cumulative=True,
            #common_grid=True,
            common_norm=False
           )

plt.grid()
plt.show()

绘制双变量的核密度估计图:

In [55]:

ini 复制代码
sns.kdeplot(data=tips, x="total_bill",y="tip")  # 同时指定x-y

plt.show()

指定hue参数进行分组:

In [56]:

ini 复制代码
sns.kdeplot(data=tips, x="total_bill",y="tip",hue="sex")  

plt.show()

指定fill参数填充区域:

In [57]:

ini 复制代码
sns.kdeplot(data=tips, x="total_bill",y="tip",hue="sex",fill=True)  

plt.show()

参数thresh表示等高线的阈值:

In [58]:

ini 复制代码
sns.kdeplot(data=tips,
            x="total_bill",
            y="tip",
            hue="sex",
            levels=5,  #  最多显示的levels数
            thresh=0.2  # 等高线的最小值,只有高于此值的等高线才会显示在图上;提高图的清晰度,排除噪声或低密度的区域
           )  

plt.show()

使用参数cmap代表不同的颜色:

In [59]:

ini 复制代码
sns.kdeplot(data=tips,
            x="total_bill",
            y="tip",
            hue="sex",
            levels=200,  #  最多显示的levels数
            thresh=0,  # 等高线的最小值,只有高于此值的等高线才会显示在图上;提高图的清晰度,排除噪声或低密度的区域
            cmap="mako"
           )  

plt.show()
相关推荐
胖哥真不错41 分钟前
基于MATLAB的朴素贝叶斯NB的数据分类预测方法应用
人工智能·机器学习·matlab·分类·数据挖掘·数据分类预测·朴素贝叶斯nb
不会计算机的g_c__b2 小时前
深入解析文本分类技术全景:从特征提取到深度学习架构
深度学习·分类·数据挖掘
V1ncent Chen3 小时前
Excel基础:格式化
数据分析·excel
wh_xia_jun17 小时前
基础分类模型及回归简介(一)
分类·数据挖掘·回归
淦暴尼19 小时前
基于spark的二手房数据分析可视化系统
大数据·分布式·数据分析·spark
云天徽上19 小时前
【数据可视化】基于pyecharts的航空安全深度剖析:坠毁航班数据集可视化分析
数据可视化
ClouGence1 天前
CloudCanal + Apache Paimon + StarRocks 实时构建湖仓一体架构
后端·数据挖掘·数据分析
SickeyLee1 天前
对比分析:给数据找个 “参照物”,让孤立数字变 “决策依据”
信息可视化·数据挖掘·数据分析
杨超越luckly1 天前
ArcGISPro应用指南:使用ArcGIS Pro创建与优化H3六边形网格
arcgis·信息可视化·数据可视化·shp·h3网格
李昊哲小课2 天前
K近邻算法的分类与回归应用场景
python·机器学习·分类·数据挖掘·回归·近邻算法·sklearn