数据可视化之seaborn(3)

mac2025-06-11  36

接之前的第二篇,在绘图之前我们先导入需要的包做好准备工作。

import seaborn as sns import matplotlib.pyplot as plt %matplotlib inline import numpy as np import pandas as pd plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus'] = False sns.set_style=('darkgrid',{'font.sans-serif':['SimHei','Arial']}) import warnings#去除部分警告信息 warnings.filterwarnings('ignore')

准备工作做好我们就开始绘图吧。

线图(seaborn.lineplot)

lineplot(x=None, y=None, hue=None, size=None, style=None, data=None, palette=None, hue_order=None, hue_norm=None, sizes=None, size_order=None, size_norm=None, dashes=True, markers=None,style_order=None, units=None, estimator='mean', ci=95, n_boot=1000, sort=True, err_style='band', err_kws=None, legend='brief', ax=None, **kwargs) x,y,hue data:数据集。 style:绘图的风格。 size:绘图的大小。 palette:调色板。 markers:绘图的形状。 ci:允许的误差范围(空值误差的百分比,0-100之间),可为‘sd’,则采用标准差(默认95)。 dashes:是否绘制破折号。 n_boot(int):计算置信区间要使用的迭代次数。 alpha:透明度。 x_jitter,y_jitter:设置点的抖动程度。 data={ 'apples':[3,2,0,1], 'oranges':[0,1,2,4], 'bananas':[1,2,1,0] } df=pd.DataFrame(data,index=['June','Bob','Lily','Robert']) sns.lineplot(data=df)

分面网格关联图

mpg_df = sns.load_dataset("mpg",data_home='C:\\seaborn-data') g = sns.relplot(x = "displacement", y = "mpg",col = "cylinders", data = mpg_df, col_wrap = 3)#col_wrap:每一行绘制的图表数

密度图(kde图)

seaborn.kdeplot(data,data2=None,shade=False)

data:第一个变量数据,也可以是Series、一维数组或列表。 data2:第二个变量数据,与data相同。 shade:是否绘制阴影效果。 #通过bw(bandwidth)参数指定kde拟合的精度,类似于直方图中的ins的效架 #bw越小,曲线越精细 sns.kdeplot(tips['total_bill'],shade=True) sns.kdeplot(tips['total_bill'],bw=0.5,label='bw:0.5') sns.kdeplot(tips['total_bill'],bw=6,label='bw:6')

n = 1024 x = np.random.normal(0,1,n) y = np.random.normal(0,1,n) g = sns.kdeplot(x,y,shade=True)

连接图(seaborn.jointplot)

jointplot(x, y, data=None, kind='scatter', stat_func=None, color=None, height=6, ratio=5, space=0.2, dropna=True, xlim=None, ylim=None, joint_kws=None, marginal_kws=None, annot_kws=None, **kwargs) x, y : strings or vectors Data or names of variables in ``data``. data : DataFrame, optional DataFrame when ``x`` and ``y`` are variable names. #与上一段中的说明相对应,代表数据框,默认为None kind : { "scatter" | "reg" | "resid" | "kde" | "hex" }, optional Kind of plot to draw. #用于控制展示成对变量相关情况的主图中的样式 stat_func : callable or None, optional *Deprecated* #用于计算统计量关系的函数 color : matplotlib color, optional Color used for the plot elements. #控制图像中对象的色彩 height : numeric, optional Size of the figure (it will be square). #控制图像为正方形时的边长 ratio : numeric, optional Ratio of joint axes height to marginal axes height. #调节联合图与边缘图的相对比例,越大则边缘图越矮,默认为5 space : numeric, optional Space between the joint and marginal axes. #右图像距离中间图像的距离 dropna : bool, optional If True, remove observations that are missing from ``x`` and ``y``. #是否去除x,y的缺失值 {x, y}lim : two-tuples, optional Axis limits to set before plotting. #设置x轴与y轴显示范围 n = 1024 x = np.random.normal(0,1,n) y = np.random.normal(0,1,n) g = sns.jointplot(x,y,kind="hex")

热力图(seaborn.heatplot)

是以矩阵形式表示数据的一种方式,数据值在途中表示为颜色。

seaborn.heatplot(data,vmin=None,vmax=None,cmap=None,annot=None)

data:二维数据集。 vmin和vmax:图例中最大值和最小值的显示值。 cmap:设置颜色面板。 annot:设置热力图注解,如果True则在单元格中显示数据。 df=pd.DataFrame(np.random.rand(10,10),columns=list('abcdefghij')) sns.heatmap(df,vmin=0,vmax=0.5,cmap='Blues',annot=True)

dist图(seaborn.distplot)

单变量的直方图与密度图的结合体

distplot( a, bins=None, hist=True, kde=True, rug=False, fit=None, hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None, color=None, vertical=False, norm_hist=False, axlabel=None, label=None, ax=None) a:单变量数据,他可以是Series、一维数组或列表。 bins:直方图中柱体的个数。 hist:是否绘制直方图。 kde:是否绘制密度图。 rug:bool型变量,控制是否绘制对应rugplot的部分,默认为False。 fit:传入scipy.stats中的分布类型,用于在观察变量上抽取相关统计特征来强行拟合指定的分布,下文的例子中会有具体说明,默认为None,即不进行拟合。 hist_kws,kde_kws,rug_kws:这几个变量都接受字典形式的输入,键值对分别对应各自原生函数中的参数名称与参数值。 color:用于控制除了fit部分拟合出的曲线之外的所有对象的色彩。 vertical:bool型,控制是否颠倒x-y轴,默认为False,即不颠倒。 norm_hist:bool型变量,用于控制直方图高度代表的意义,为True直方图高度表示对应的密度,为False时代表的是对应的直方区间内记录值个数,默认为False。 label:控制图像中的图例标签显示内容 。 tips = sns.load_dataset("tips",data_home='C:\\seaborn-data') sns.distplot(tips['total_bill'])

线性回归图

regplot(

x, y, data=None, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker='o', scatter_kws=None, line_kws=None, ax=None)

具体值的介绍可以参考此网址:https://www.cnblogs.com/nadech/p/8496593.html

df = sns.load_dataset("tips",data_home='C:\\seaborn-data') sns.regplot(x='total_bill',y='tip',data=df)

lmplot

lmplot的参数与regplot的参数几乎一致,可以参考上面网址。

df = sns.load_dataset("tips",data_home='C:\\seaborn-data') sns.lmplot(x='total_bill',y='tip',data=df)

seaborn的各种图的介绍就结束了,具体的了解和学习可以参考官网,如有错误之处还望指出。

最新回复(0)