美文网首页数据可视化
数据科学 : seaborn 入门

数据科学 : seaborn 入门

作者: CV_lsc | 来源:发表于2017-09-07 20:59 被阅读474次

    seaborn是基于matplotlib开发的可视化库,比matplotlib更加容易使用,而且图例的风格更加现代化。今天介绍一下它,讲解的形式还是代码加图,希望各位能认真看。

    %matplotlib inline    我是使用的jupter notebook 所以加这个
    import numpy as np  
    import matplotlib.pyplot as plt             国际惯例 缩写
    import seaborn as sns
    import pandas as pd
    sns.set()
    
    seaborn.displot() 用于绘制单变量分布情况
    x = np.random.normal(size=100)
    sns.distplot(x)
    

    非常简洁的代码就可以得到下面的图 直方图+核密度估计
    如何你不想看到直方图 只要把代码改成 sns.distplot(x,hist=False),还有很多功能可以查看displot函数说明。

    displot0
    seaborn.jointmtplot() 用于绘制二元分布情况
    mean, cov = [0, 1], [(1, .5), (.5, 1)]
    data = np.random.multivariate_normal(mean, cov, 200)
    df = pd.DataFrame(data, columns=["x", "y"])
    sns.jointplot(x="x", y="y", data=df)
    
    scatter

    可以通过kind设置 “scatter” , “reg” , “resid” , “kde” ,“hex” 例如"hex"

    sns.jointplot(x='x', y='y',data=df, kind="hex")
    
    hex
    seaborn.piarplot() 用于绘制数据集中特征两两之间关系
    iris = sns.load_dataset("iris")
    sns.pairplot(iris, hue='species', size=3)
    

    绘制的是iris鸢尾花卉数据集的特征两两之间的关系,4个特征,三种花。图如下:

    iris

    seaborn 还有很多的有意思的函数,能绘制很漂亮的图,大家可以查看官网的教程http://seaborn.pydata.org/tutorial.html,当然凡事有利有弊,简洁的代码带来的就是缺少灵活性,所以希望大家灵活运用多种可视化工具,完成符你要求的图案。

    相关文章

      网友评论

        本文标题:数据科学 : seaborn 入门

        本文链接:https://www.haomeiwen.com/subject/urwjjxtx.html