facetgrid(pairgrid)使用方法及绘制多变量(绘图进阶)
综述
学生党整理一些关于数据分析的知识:整理了facetgrid(pairgrid)使用方法及绘制多变量。包括了FacetGrid部分和PairGrid部分。
- FacetGrid部分:数据分类、绘制各种数据图、颜色浓度及长宽比设置、子图绘制顺序设置、调色板及其他参数设置及形状及坐标范围指定。
- PairGrid部分:指定绘图样式、增加分类、指定绘制量及颜色设置。
代码模块
调用库
import matplotlib.pyplot as plt
import seaborn as sns
使用seaborn库中load_dataset读取数据
tips = sns.load_dataset("tips")
tips.head(1)
FacetGrid
数据分类
显示数据集中time的两种情况
g = sns.FacetGrid(tips, col = 'time')
plt.show()
绘制各种数据图
绘制tip数据条形图
g = sns.FacetGrid(tips, col = 'time')
g.map(plt.hist,'tip')
plt.show()
绘制性别、抽烟散点图
g = sns.FacetGrid(tips, col = 'sex',hue = 'smoker')
g.map(plt.scatter,'total_bill','tip',alpha = .7)
g.add_legend()
plt.show()
颜色浓度及长宽比设置
g = sns.FacetGrid(tips, row = 'smoker', col = 'time', margin_titles= True)
g.map(sns.regplot,'size','total_bill',color=".5",fit_reg = True, x_jitter=.1)
#fit_reg = True 增加回归的线
plt.show()
长宽比设置
g = sns.FacetGrid(tips, col = 'day', size = 4, aspect = .5)
g.map(sns.barplot, 'sex', 'total_bill')
plt.show()
子图绘制顺序设置
from pandas import Categorical
ordered_days = tips.day.value_counts().index
print(ordered_days)
#CategoricalIndex(['Sat', 'Sun', 'Thur', 'Fri'], categories=['Thur', 'Fri', 'Sat', 'Sun'], ordered=False, dtype='category')
#自己指定绘图顺序
ordered_days = Categorical(['Thur','Fri','Sat','Sun'])
g = sns.FacetGrid(tips, row='day', row_order= ordered_days,
size=1.7, aspect=4)
g.map(sns.boxplot,'total_bill')
plt.show()
调色板及其他参数设置
pal = dict(Lunch = 'seagreen',Dinner = 'gray')
# 调色板 palette = pal——字典
g = sns.FacetGrid(tips,hue = 'time', palette=pal, size = 5)
g.map(plt.scatter,'total_bill', 'tip',s = 50, alpha = .7, linewidth = .5, edgecolor = 'white')
# map()中 s = 点大小
g.add_legend()
plt.show()
形状及坐标范围指定
形状
g = sns.FacetGrid(tips, hue='sex',palette='Set1',size = 5, hue_kws={"marker":["^","v"]})
g.map(plt.scatter,'total_bill', 'tip',s = 100, linewidth = .5, edgecolor = 'white')
plt.show()
with sns.axes_style("white"):#风格
g = sns.FacetGrid(tips, row = 'sex', col = 'smoker', margin_titles=True,size = 2.5)
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=.02,hspace=.02)#子图和子图之间的间隔设置
plt.show()
PairGrid
iris = sns.load_dataset('iris')
g = sns.PairGrid(iris)
g.map(plt.scatter,s = 20, edgecolor = 'black')
plt.show()
指定绘图样式
g = sns.PairGrid(iris)
g.map_diag(plt.hist,edgecolor = 'black')#对角线上图片
g.map_offdiag(plt.scatter,s = 20, edgecolor = 'black')#非对角线上图片
plt.show()
增加分类
g = sns.PairGrid(iris,hue= 'species')
g.map_diag(plt.hist,edgecolor = 'black')#对角线上图片
g.map_offdiag(plt.scatter,s = 20, edgecolor = 'black')#非对角线上图片
g.add_legend()
plt.show()
指定绘制量
g = sns.PairGrid(iris, vars=['sepal_length','sepal_width'],hue = 'species')
g.map(plt.scatter,s = 20, edgecolor = 'black')
plt.show()
颜色设置
g = sns.PairGrid(tips, hue = 'size', palette='GnBu_d')
g.map(plt.scatter, s = 50, edgecolor = 'white')
g.add_legend()
plt.show()
小结
绘图知识整理部分主要针对绘图方法,具体在数据分析过程中使用哪种图片在之后具体的数据分析案例中详细说明。