零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

简介

Seaborn是一个统计图形绘制库

它默认具有非常漂亮的样式

它对pandas dataframe对象支持的非常好

安装

conda install seaborn

pip install seaborn

在线文档:https://seaborn.pydata.org/

seaborn可以绘制出多种图形例如:

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

Distribution Plots

本文介绍如何使用Seaborn绘制数据分布图,可以使用的函数包括:

  • distplot
  • jointplot
  • pairplot
  • rugplot
  • kdeplot

首先打开Jupyter Notebook

导入数据

import seaborn as sns

%matplotlib inline

数据

Seaborn本身包含一些数据集,例如tips

tips = sns.load_dataset('tips')

tips.head()

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

distplot

distplot显示单变量观测值的分布

sns.distplot(tips['total_bill'])

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

去掉kde只显示直方图

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

jointplot

可以在二维数据上将两个distplot组合显示,可选项包括:

  • “scatter”
  • “reg”
  • “resid”
  • “kde”
  • “hex”

sns.jointplot(x='total_bill',y='tip',data=tips,kind='scatter')

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

sns.jointplot(x='total_bill',y='tip',data=tips,kind='hex')

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

sns.jointplot(x='total_bill',y='tip',data=tips,kind='reg')

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

pairplot

pairplot在整个数据集中绘制成对关系(对于数值列),并支持颜色色调参数(用于分类列)。

sns.pairplot(tips)

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

sns.pairplot(tips,hue='sex',palette='coolwarm')

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

rugplot

rugplot实际上是一个非常简单的概念,它只是为单变量分布上的每个点画一个破折号,是建立在kde基础上的。

sns.rugplot(tips['total_bill'])

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

kdeplot

kdeplot是Kernel Density Estimation plots的缩写,这些KDE图替换以高斯分布为中心的每一个观察值。例如

# Don't worry about understanding this code!

# It's just for the diagram below

import numpy as np

import matplotlib.pyplot as plt

from scipy import stats

#Create dataset

dataset = np.random.randn(25)

# Create another rugplot

sns.rugplot(dataset);

# Set up the x-axis for the plot

x_min = dataset.min() - 2

x_max = dataset.max() + 2

# 100 equally spaced points from x_min to x_max

x_axis = np.linspace(x_min,x_max,100)

# Set up the bandwidth, for info on this:

url = 'http://en.wikipedia.org/wiki/Kernel_density_estimation#Practical_estimation_of_the_bandwidth'

bandwidth = ((4*dataset.std()**5)/(3*len(dataset)))**.2

# Create an empty kernel list

kernel_list = []

# Plot each basis function

for data_point in dataset:

# Create a kernel for each point and append to list

kernel = stats.norm(data_point,bandwidth).pdf(x_axis)

kernel_list.append(kernel)

#Scale for plotting

kernel = kernel / kernel.max()

kernel = kernel * .4

plt.plot(x_axis,kernel,color = 'grey',alpha=0.5)

plt.ylim(0,1)

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

# To get the kde plot we can sum these basis functions.

# Plot the sum of the basis function

sum_of_kde = np.sum(kernel_list,axis=0)

# Plot figure

fig = plt.plot(x_axis,sum_of_kde,color='indianred')

# Add the initial rugplot

sns.rugplot(dataset,c = 'indianred')

# Get rid of y-tick marks

plt.yticks([])

# Set title

plt.suptitle("Sum of the Basis Functions")

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

sns.kdeplot(tips['total_bill'])

sns.rugplot(tips['total_bill'])

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

sns.kdeplot(tips['tip'])

sns.rugplot(tips['tip'])

零基础入门到精通:Python大数据与机器学习-数据可视化:Seaborn

后续文章内容:

  • Distribution Plots
  • Categorical Data Plots
  • Matrix Plots
  • Grids
  • Regression Plots
  • Style and Color
  • Seaborn练习题及答案


分享到:


相關文章: