Seaborn

Seaborn

Matplotlib是Python主要的绘图库。但是不建议你直接使用它,原因与不推荐你使用NumPy是一样的。虽然Matplotlib很强大,它本身就很复杂,你的图经过大量的调整才能变精致。因此,作为替代推荐一开始使用Seaborn。Seaborn 是由斯坦福大学提供的一个python库,简短地描述下seaborn的优点。具体来说,它可以:

  • 默认情况下就能创建赏心悦目的图表。(只有一点,默认不是jet colormap)
  • 创建具有统计意义的图
  • 能理解pandas的DataFrame类型,所以它们一起可以很好地工作。

Dataset

  • 数据集births.csv是由National Survey of Family Growth收集了2002年到2003年期间的怀孕,家庭生活等等信息。特征如下:

prglngth – the length of the pregnancy in weeks.
birthord – which child this was for the pregnant mother.
birthwgt_lb1 – the pounds portion of the birth weight.
birthwgt_oz1 – the ounces portion of the birth weight.
agepreg – the mother’s age at the end of the pregnancy, in years.

  • sns.plt.show()替换掉plt.show(),在matplotlib画直方图的方法是:births.hist(‘prglngth’),而在Seaborn中的方法是:distplot(births[‘prglngth’], kde=False),当然你也可以用sns.plt.show来显示用matplotlib画的直方图。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

births = pd.read_csv('births.csv')
# kde参数设置为True时表示纵轴显示的是密度
sns.distplot(births['prglngth'], kde=False)
sns.plt.show()

births.hist('prglngth')
plt.show()
  • 这是Seaborn的直方图,粒度较细。

这里写图片描述

  • 这是matplotlib的直方图

这里写图片描述

  • 将kde = True

    这里写图片描述

Pandas 和Seaborn都是利用Matplotlib 进行可视化,但是他们支架又有若干区别:

  • 当可视化时只需要调整少量参数时用Pandas比较合适,因为它主要是用来进行数据探索而不是可视化,如果非要用Pandas 进行自定义可视化,实际上就是用底层的Matplotlib进行绘图,那么需要对Matplotlib 有很深的了解。但是如果画图时需要个性化修改一些图的属性就用Seaborn,因为Seaborn可以利用一些简单的API来直接可视化我们的数据,不用我们进行参数的理解。
  • -

当用pandas进行可视化的时候,如果想要个性化某些参数,那么就需要对Matplotlib 有很深的了解,

Boxplots: Boxplot()

  • 用sns画一个X轴代表第一几个孩子纵轴代表孕妇的年龄的一个盒图。
births = pd.read_csv('births.csv')
sns.boxplot(x='birthord', y='agepreg', data=births)
sns.plt.show()

这里写图片描述

Pair Plot: Pairplot()

对于有n个属性的DataFrame,可以产生n*n个属性的比较图,也可以自己指定要比较的属性个数,然后进行两两比较。
当比较的属性是:var1 vs var2,那么就是var1 是横轴,var2是纵轴,画的是散点图。
当比较的属性是:var1 vs var1,那么画的是var1的直方图。

sns.pairplot(births[['agepreg','prglngth','birthord']])
sns.plt.show()

这里写图片描述

1. 目录 1. 目录 2 2. 绘图函数Plotting functions 4 2.1. 可视化的统计关系Visualizing statistical relationships 4 2.1.1. 用散点图联系变量Relating variables with scatter plots 4 2.1.2. 强调线条图的连续性Emphasizing continuity with line plots 10 2.1.3. 显示与切面的多个关系Showing multiple relationships with facets 21 2.2. 分类数据绘图Plotting with categorical data 24 2.2.1. 分类散点图Categorical scatterplots 26 2.2.2. 分类观测值分布Distributions of observations within categories 31 2.2.3. 分类统计估计Statistical estimation within categories 37 2.2.4. 对“wide-form”数据作图Plotting “wide-form” data 41 2.2.5. 显示与facet的多个关系Showing multiple relationships with facets 43 2.3. 可视化数据集的分布Visualizing the distribution of a dataset 44 2.3.1. 绘制单变量分布Plotting univariate distributions 45 2.3.2. 绘制二元分布Plotting bivariate distributions 51 2.3.3. 在数据集中可视化成对关系Visualizing pairwise relationships in a dataset 55 2.4. 可视化线性关系Visualizing linear relationships 57 2.4.1. 函数绘制线性模型Functions to draw linear regression models 58 2.4.2. 拟合不同种类的模型Fitting different kinds of models 61 2.4.3. 在其他变量上的情况Conditioning on other variables 68 2.4.4. 控制图表的大小和形状Controlling the size and shape of the plot 71 2.4.5. 在其他上下文中绘制回归图Plotting a regression in other contexts 73 3. 多图网格Multi-plot grids 76 3.1. 构建结构化的多图网格Building structured multi-plot grids 76 3.2. 有条件的小倍数Conditional small multiples 77 3.3. 使用定制函数Using custom functions 86 3.4. 绘制成对的数据关系Plotting pairwise data relationships 90 4. 绘图美学Plot aesthetics 99 4.1. 控制图表美学Controlling figure aesthetics 99 4.1.1. Seaborn图表风格Seaborn figure styles 101 4.1.2. 删除轴上的小凸起Removing axes spines 104 4.1.3. 临时设置图表样式Temporarily setting figure style 105 4.1.4. 覆盖Seaborn样式的元素Overriding elements of the seaborn styles 106 4.1.5. 缩放图表元素Scaling plot elements 108 4.2. 选择调色板Choosing color palettes 111 4.2.1. 创建颜色调色板Building color palettes 111 4.2.2. 定性调色板Qualitative color palettes 112 4.2.3. 连续调色板Sequential color palettes 116 4.2.4. 不同颜色的调色板Diverging color palettes 122 4.2.5. 设置默认调色板Setting the default color palette 124 5. 教程中的数据集 125
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值