python数据分析作图细节

seaborn

官方链接:http://seaborn.pydata.org/index.html
Seaborn是一种基于matplotlib的图形可视化python libraty。它提供了一种高度交互式界面,便于用户能够做出各种有吸引力的统计图表。
Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图,而使用matplotlib就能制作具有更多特色的图。应该把Seaborn视为matplotlib的补充,而不是替代物。同时它能高度兼容numpy与pandas数据结构以及scipy与statsmodels等统计模式。掌握seaborn能很大程度帮助我们更高效的观察数据与图表,并且更加深入了解它们。

barplot条形图

seaborn.barplot - seaborn 0.7.1 documentation链接:http://seaborn.pydata.org/generated/seaborn.barplot.html?highlight=barplot#seaborn.barplot

seaborn的barplot()利用矩阵条的高度反映数值变量的集中趋势,以及使用errorbar功能(差棒图)来估计变量之间的差值统计。请谨记bar plot展示的是某种变量分布的平均值,当需要精确观察每类变量的分布趋势,boxplot与violinplot往往是更好的选择。

具体用法如下:

seaborn.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None,ci=95, n_boot=1000, units=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, ax=None, estimator=<function mean>**kwargs)
  • x, y, hue : names of variables in data or vector data, optional #设置x,y以及颜色控制的变量
  • data : DataFrame, array, or list of arrays, optional #设置输入的数据集
  • order, hue_order : lists of strings, optional #控制变量绘图的顺序
  • estimator : callable that maps vector -> scalar, optional #设置对每类变量的计算函数,默认为平均值,可修改为max、median、max等
  • ax : matplotlib Axes, optional #设置子图位置,将在下节介绍绘图基础
  • orient : “v” | “h”, optional #控制绘图的方向,水平或者竖直
  • capsize : float, optional #设置误差棒帽条的宽度
import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips") #载入自带数据集
#x轴为分类变量day,y轴为数值变量total_bill,利用颜色再对sex分类
ax = sns.barplot(x="day", y="total_bill", hue="sex", data=tips) 

在这里插入图片描述

countplot计数图

seaborn.countplot - seaborn 0.7.1 documentation链接:http://seaborn.pydata.org/generated/seaborn.countplot.html?highlight=countplot#seaborn.countplot

countplot故名思意,计数图,可将它认为一种应用到分类变量的直方图,也可认为它是用以比较类别间计数差,调用count函数的barplot。

seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)
  • x, y, hue : names of variables in data or vector data, optional
    order, hue_order : lists of strings, optional #设置顺序
    orient : “v” | “h”, optional #设置水平或者垂直显示
    ax : matplotlib Axes, optional #设置子图位置
>>> import seaborn as sns
>>> sns.set(style="darkgrid")
>>> titanic = sns.load_dataset("titanic") #titanic经典数据集,带有登船人员的信息
#源数据集class代表三等舱位,who代表人员分类,男女小孩,对每一类人数计数
>>> ax = sns.countplot(x="class", hue="who", data=titanic) 

在这里插入图片描述

distplot

seaborn.distplot - seaborn 0.7.1 documentation链接:
http://seaborn.pydata.org/generated/seaborn.distplot.html?highlight=hist
seaborn的displot()集合了matplotlib的hist()与核函数估计kdeplot的功能,增加了rugplot分布观测条显示与利用scipy库fit拟合参数分布的新颖用途。具体用法如下:

seaborn.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, 1d-array, or list.
    bins : argument for matplotlib hist(), or None, optional #设置矩形图数
    hist : bool, optional #控制是否显示条形图
    kde : bool, optional #控制是否显示核密度估计图
    rug : bool, optional #控制是否显示观测的小细条(边际毛毯)
    fit : random variable object, optional #控制拟合的参数分布图形
    vertical : bool, optional #显示正交控制

Histograms直方图

直方图(Histogram)又称质量分布图。是一种统计报告图,由一系列高度不等的纵向条纹或线段表示数据分布的情况。 一般用横轴表示数据类型,纵轴表示分布情况。

%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt #导入

import seaborn as sns
sns.set(color_codes=True)#导入seaborn包设定颜色

np.random.seed(sum(map(ord, "distributions")))

x = np.random.normal(size=100)
sns.distplot(x, kde=False, rug=True);#kde=False关闭核密度分布,rug表示在x轴上每个观测上生成的小细条(边际毛毯)

在这里插入图片描述
当绘制直方图时,你最需要确定的参数是矩形条的数目以及如何放置它们。利用bins可以方便设置矩形条的数量。如下所示:

sns.distplot(x, bins=20, kde=False, rug=True);#设置了20个矩形条

在这里插入图片描述

Kernel density estimaton核密度估计

核密度估计是在概率论中用来估计未知的密度函数,属于非参数检验方法之一。.由于核密度估计方法不利用有关数据分布的先验知识,对数据分布不附加任何假定,是一种从数据样本本身出发研究数据分布特征的方法,因而,在统计学理论和应用领域均受到高度的重视。
seaborn.kdeplot - seaborn 0.7.1 documentation链接:http://seaborn.pydata.org/generated/seaborn.kdeplot.html?highlight=kdeplot#seaborn.kdeplot

distplot()

sns.distplot(x, hist=False, rug=True);#关闭直方图,开启rug细条

在这里插入图片描述

kdeplot()

sns.kdeplot(x, shade=True);#shade控制阴影

在这里插入图片描述

Fitting parametric distributions拟合参数分布

可以利用distplot() 把数据拟合成参数分布的图形并且观察它们之间的差距,再运用fit来进行参数控制。

x = np.random.gamma(6, size=200)#生成gamma分布的数据
sns.distplot(x, kde=False, fit=stats.gamma);#fit拟合

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值