https://blog.csdn.net/cymy001/article/details/78418432
有待补充
Python 中,数据可视化一般是通过较底层的 Matplotlib 库和较高层的 Seaborn库实现的,本文主要介绍一些常用的图的绘制方法。
9是多分类面板,能够绘制多个图,并且可通过kind参数绘制出更多的图。
1、一维数据
import seaborn as sns
import pandas as pd
import numpy as np
from scipy import stats,integrate
import matplotlib.pyplot as plt
sns.set(color_codes = True)
np.random.seed(sum(map(ord,"distributions")))
x = np.random.normal(size=100)
sns.distplot(x,kde=False)
sns.distplot(x,bins=20,kde=False) # 切分为20小块
x = np.random.gamma(6,size=200)
sns.distplot(x,kde=False,fit=stats.gamma)
2、二维数据的分布
mean,cov = [0,1],[(1,0.5),(0.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)
观测两个变量的分布关系,而且还输出皮尔森指数
如果数据很多的话,用hex图能更好的显示出分布
x,y = np.random.multivariate_normal(mean,cov,1000).T
with sns.axes_style('white'):
sns.jointplot(x=x,y = y,kind='hex',color='k')
可以看出结果分布较多是在中间位置,通过颜色深浅看出分布
3、回归
np.random.seed(sum(map(ord,"regression")))
tips = sns.load_dataset("tips")
tips.head()
regplot() 和 lmplot()都可以绘制回归关系
sns.regplot(x="total_bill",y="tip",data=tips)
sns.lmplot(x="total_bill",y="tip",data=tips)
目前看区别不大,但是后者功能更多,规范也多。
用离散数据作回归
sns.regplot(x="size",y="tip",data=tips)
看出显示并不好
原始数据集是类别值,都是整数,现在加了点浮动(参数x_jitter)
使得离散值,变得像连续值
sns.regplot(x="size",y="tip",data=tips,x_jitter=0.05)
sns.regplot(x="size",y="tip",data=tips,x_jitter=0.5)
4、对类别值的可视化展示
titanic = sns.load_dataset("titanic")
iris = sns.load_dataset("iris")
sns.stripplot(x='day',y="total_bill",data=tips)
像散点图,但是不怎么看得出来,因为都重叠了,影响观察数据量
参数 jitter=True相当于往左右偏移,使得不重叠
sns.stripplot(x='day',y="total_bill",data=tips,jitter=True)
sns.swarmplot(x='day',y="total_bill",data=tips)
画出来像一棵树
三个参数,加入更多的条件hue
sns.swarmplot(x='day',y="total_bill",hue='sex',data=tips)
sns.swarmplot(y='day',x="total_bill",hue='time',data=tips)
5、盒图
可观察离群点,竖着画对x,y类型没要求
sns.boxplot(x='day',y='total_bill',hue="time",data=tips)
可以横着画,但是x,y必须是数值型
sns.boxplot(y='size',x='total_bill',data=tips,orient="h")
6、小提琴图
sns.violinplot(x='day',y='total_bill',hue="time",data=tips)
sns.violinplot(x='day',y='total_bill',hue="sex",data=tips,split=True)
还可以合体
sns.violinplot(x='day',y='total_bill',data=tips,inner=None)
sns.swarmplot(x='day',y="total_bill",data=tips,color='w',alpha=0.5)
7、分类数据
通过柱状图观察一等、二等、三等舱的分布
sns.barplot(x='sex',y='survived',hue='class',data=titanic)
8、折线图
sns.pointplot(x='sex',y='survived',hue='class',data=titanic)
sns.pointplot(x='class',y='survived',hue='sex',data=titanic,
palette={"male":"g","female":"m"},
maker=["s","o"],linestyles=["-","--"])
factorplot()
sns.factorplot(x='day',y='total_bill',hue='smoker',data=tips)
sns.factorplot(x='day',y='total_bill',hue='smoker',data=tips,kind='bar')
可绘制多个图
sns.factorplot(x='day',y='total_bill',hue='smoker',
col='time',data=tips,kind='swarm')
sns.factorplot(x='time',y='total_bill',hue='smoker',
col='day',data=tips,kind='box',size=4,aspect=0.5)
参数kind:point默认,bar柱形图,count频次,box箱体,violin提琴,strip散点,swarm分散点
FacetGrid()
准备:
import seaborn as sns
import pandas as pd
import numpy as np
from scipy import stats,integrate
import matplotlib.pyplot as plt
sns.set(color_codes = True)
np.random.seed(sum(map(ord,"distributions")))
x = np.random.normal(size=100)
sns.distplot(x,kde=False)
tips = sns.load_dataset("tips")
titanic = sns.load_dataset("titanic")
iris = sns.load_dataset("iris")
1、操作流程
- 先sns.FacetGrid画出轮廓
- 然后用map填充内容
g = sns.FacetGrid(tips,col='time')
g.map(plt.hist,"tip")
g = sns.FacetGrid(tips,col='sex',hue='smoker')
g.map(plt.scatter,"total_bill","tip",alpha=0.7)
g.add_legend() # 加注释
g = sns.FacetGrid(tips,col='time',row='smoker',margin_titles=True)
g.map(sns.regplot,"size","total_bill",color="0.5",fit_reg=True,x_jitter=0.1)
g = sns.FacetGrid(tips,col='day',size=4,aspect=0.5)
g.map(sns.barplot,"sex","total_bill")
from pandas import Categorical
ordered_days = tips.day.value_counts().index
print(ordered_days)
ordered_days = Categorical(['Thur',"Fri","Sat","Sun"])
# FacetGrid传数据需要是pandas格式
g = sns.FacetGrid(tips,row='day',row_order=ordered_days,size=1.7,aspect=4)
g.map(sns.boxplot,"total_bill")
pal = dict(Lunch="seagreen",Dinner="red")# 两个变量自己指定颜色
g=sns.FacetGrid(tips,hue="time",palette=pal,size=5)
g.map(plt.scatter,"total_bill","tip",s=50, alpha=0.7, linewidth=0.5,edgecolor="white")
g.add_legend()
g=sns.FacetGrid(tips,hue="time",palette=pal,size=5,hue_kws={"marker":["^","v"]})
g.map(plt.scatter,"total_bill","tip",s=50, alpha=0.7, linewidth=0.5,edgecolor="white")
g.add_legend()
with sns.axes_style("white"):
g = sns.FacetGrid(tips,row="sex",col="smoker",margin_titles=True,size=3)
g.map(plt.scatter,"total_bill","tip",color="#334488",edgecolor="white",lw=5)
g.set_axis_labels("Total bill (US Dollars)","Tip")
g.set(xticks=[10,30,50],yticks=[2,6,10])# 轴的取值范围
g.fig.subplots_adjust(wspace=0.02,hspace=0.02)# 子图和子图之间的间隔
iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map(plt.scatter)
g = sns.PairGrid(iris)
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
g = sns.PairGrid(iris,hue="species")
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
g.add_legend()
# 指定想画出的特征
feature_=["sepal_length","sepal_width"]
g = sns.PairGrid(iris,vars=feature_, hue="species")
g.map(plt.scatter)
g = sns.PairGrid(tips, hue='size',palette="GnBu_d")#渐变色
g.map(plt.scatter,s=50,edgecolor="white")
g.add_legend()