@Author:Runsen
@Date:2019年07月07日
作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件。导致翘课严重,专业排名中下。.在大学60%的时间,都在CSDN。
本专栏数据分析全系列:将使用Excel,Powerbi,Python,R,Sql,SPSS,stata以及Tableau,后面还会补充BI。这个从2020年期末考试,由于大三上大学挂了大学以来的两科化工原理和热力学。从此,决定逃离CSDN,但是发现我已经深深爱上了CSDN,这个我从大一奋斗到现在的地方。
于是将每天一直写,不断地回顾和反省,大学三年搞数据的从小白变老手的经验,想看就给钱,就是这么直接。这些是一月份我决定写书,结果别人看不起我写的。
没看上篇的先看上篇
Seaborn
Seaborn是基于matplotlib的图形可视化python包。它提供了一种高度交互式界面,便于用户能够做出各种有吸引力的统计图表。
# 条形图
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
# 加载数据集泰坦尼克号
titanic=sns.load_dataset('titanic')
titanic.head()
survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
# 绘制箱型图
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_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
90 | 5.5 | 2.6 | 4.4 | 1.2 | versicolor |
129 | 7.2 | 3.0 | 5.8 | 1.6 | virginica |
130 | 7.4 | 2.8 | 6.1 | 1.9 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
45 | 4.8 | 3.0 | 1.4 | 0.3 | setosa |
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)