十九、Seaborn数据可视化

@Author:Runsen

@Date:2019年07月07日

作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件。导致翘课严重,专业排名中下。.在大学60%的时间,都在CSDN。

本专栏数据分析全系列:将使用Excel,Powerbi,Python,R,Sql,SPSS,stata以及Tableau,后面还会补充BI。这个从2020年期末考试,由于大三上大学挂了大学以来的两科化工原理和热力学。从此,决定逃离CSDN,但是发现我已经深深爱上了CSDN,这个我从大一奋斗到现在的地方。

于是将每天一直写,不断地回顾和反省,大学三年搞数据的从小白变老手的经验,想看就给钱,就是这么直接。这些是一月份我决定写书,结果别人看不起我写的。

没看上篇的先看上篇

十八、Matplotlib数据可视化


Seaborn

Seaborn是基于matplotlib的图形可视化python包。它提供了一种高度交互式界面,便于用户能够做出各种有吸引力的统计图表。

# 条形图
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
# 加载数据集泰坦尼克号
titanic=sns.load_dataset('titanic')
titanic.head()
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
003male22.0107.2500SThirdmanTrueNaNSouthamptonnoFalse
111female38.01071.2833CFirstwomanFalseCCherbourgyesFalse
213female26.0007.9250SThirdwomanFalseNaNSouthamptonyesTrue
311female35.01053.1000SFirstwomanFalseCSouthamptonyesFalse
403male35.0008.0500SThirdmanTrueNaNSouthamptonnoTrue
# 绘制箱型图
sns.barplot(x='class',y='survived',data=titanic)

在这里插入图片描述

# 通过设置'hue'参数,对x轴的数据进行细分,细分的条件就是'hue'的参数值,
# 比如这里我们的x轴是'class'(仓位等级),我们将其按'sex'(性别)再进行细分。
sns.barplot(x='class',y='survived',hue='sex',data=titanic)

在这里插入图片描述
<matplotlib.axes._subplots.AxesSubplot at 0x1f68244a860>

barplot

sns.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean at 0x000001F6FB742840>,
ci=95, n_boot=1000, units=None, orient=None, color=None, palette=None, saturation=0.75, errcolor=’.26’, errwidth=None, capsize=None,
dodge=True, ax=None, **kwargs)

# 直方图 
import numpy as np
# 正态分布
x = np.random.normal(size=100)
sns.set(color_codes=True) 
#  sns.set(context='notebook', style='darkgrid', palette='deep', 
#  font='sans-serif', font_scale=1, color_codes=False, rc=None)
np.random.seed(sum(map(ord, "distributions")))
sns.distplot(x,kde=True) #distplot()函数会根据输入数据自动绘制直方图
<matplotlib.axes._subplots.AxesSubplot at 0x1f68220c2b0>

在这里插入图片描述

distplot

sns.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)

# 计数图 
sns.countplot(x='deck',data=titanic)
<matplotlib.axes._subplots.AxesSubplot at 0x1f68246bc88>

在这里插入图片描述

countplot

sns.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None,
palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)

# 横图
sns.countplot(y='deck',hue='who',data=titanic)
<matplotlib.axes._subplots.AxesSubplot at 0x1f6824e1588>

在这里插入图片描述

# 散点图
sns.jointplot(x='age',y = 'fare',data=titanic)
<seaborn.axisgrid.JointGrid at 0x1f6843f5b70>

在这里插入图片描述

import numpy as np
mean, cov = [0, 1], [(1, .5), (.5, 1)] #自定义均值与协方差
data = np.random.multivariate_normal(mean, cov, 200) #生成200个数据
df = pd.DataFrame(data, columns=["x", "y"]) #通过pandas读入数据
sns.jointplot(x='x',y = 'y',data=df)

在这里插入图片描述

regplot和 lmplot

iris = sns.load_dataset("iris") #载入鸢尾花数据集
sns.pairplot(iris) #绘制
<seaborn.axisgrid.PairGrid at 0x1f685be3160>

# 回归图 
# Seaborn 中利用 regplot() 和 lmplot() 来进行回归,确定线性关系
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
#导入数据集'iris'
iris=sns.load_dataset('iris')
#随机查看数据集的10行数据
iris.sample(5)
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
905.52.64.41.2versicolor
1297.23.05.81.6virginica
1307.42.86.11.9virginica
1495.93.05.11.8virginica
454.83.01.40.3setosa

regplot

sns.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)

sns.regplot(x='sepal_length',y='petal_length',data=iris)

在这里插入图片描述

sns.regplot(x='sepal_length',y='petal_length',data=iris,ci=None)

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小刘要努力。

顺便点一个赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值