FacertGrid()的使用
查看数据的前五行
tips = sns.load_dataset("tips")
tips.head()

引入数据,布置横向画布
g = sns.FacetGrid(tips, col='time')

g = sns.FacetGrid(tips, col='time')
g.map(plt.hist, "tip") #以tip为横轴画柱状图

g = sns.FacetGrid(tips, col="sex", hue="smoker")
g.map(plt.scatter, "total_bill", "tip", alpha=.7) #绘制散点图,设置横纵轴,设置透明度
g.add_legend() #加上如下图标注的图例

g = sns.FacetGrid(tips, row="smoker", col="time", margin_titles=True) #设置行列布局方式
g.map(sns.regplot, "size", "total_bill", color=".1", fit_reg=True, x_jitter=.1) #fit_reg画出回归线,x_jitter为摆动程度

画出柱形图
g = sns.FacetGrid(tips, col="day", size=4, aspect=.5)
g.map(sns.barplot, "sex", "total_bill")

from pandas import Categorical
ordered_days = tips.day.value_counts().index
print(ordered_days)
查看day的排列顺序
CategoricalIndex(['Sat', 'Sun', 'Thur', 'Fri'], categories=['Thur', 'Fri', 'Sat', 'Sun'], ordered=False, dtype='category')
重新设置行的排列顺序
ordered_days = Categorical(["Thur", "Sun", "Fri", "Sat"])
g = sns.FacetGrid(tips, row="day", row_order=ordered_days, size=1.7, aspect=4)
g.map(sns.boxplot, "total_bill")

(盒图能够自动识别哪个变量是离散型,哪个是连续型,然后对连续型构造盒图。)
例如以下代码
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
from pandas import Categorical
import matplotlib.pyplot as plt tips = sns.load_dataset("tips") #seaborn内置数据集,DaraFram类型
print(tips.head())
ordered_days = Categorical(["Thur", "Sun", "Fri", "Sat"])
print(type(ordered_days))
print(ordered_days)
g = sns.FacetGrid(tips, row="day", row_order=ordered_days, size=1.7, aspect=4)
g.map(sns.boxplot, "total_bill", "sex") plt.show()
运行结果如下,函数识别出sex是离散型变量,所以对sex进行分类,然后在每一个类别上对连续型变量total_bill构造盒图。

还可以用FacertGrid的palette参数给hue的列的不同类设置不同颜色,代码如下
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
from pandas import Categorical
import matplotlib.pyplot as plt tips = sns.load_dataset("tips") #seaborn内置数据集,DaraFram类型
print(tips.head())
pal = dict(Lunch="seagreen", Dinner="gray")
g = sns.FacetGrid(data=tips, hue="time", palette=pal, size=5)
g.map(plt.scatter, "total_bill", "tip", s=50, alpha=0.7, linewidths=0.5, edgecolors="white")
g.add_legend()
plt.show()
运行结果如下

如果再设置marker参数,可指定用什么图标画散点,可以是三角形或圆形等
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
from pandas import Categorical
import matplotlib.pyplot as plt tips = sns.load_dataset("tips") #seaborn内置数据集,DaraFram类型
print(tips.head())
pal = dict(Lunch="seagreen", Dinner="gray")
g = sns.FacetGrid(data=tips, hue="time", palette=pal, size=5, hue_kws={"marker":['^', 'v']})
g.map(plt.scatter, "total_bill", "tip", s=50, alpha=0.7, linewidths=0.5, edgecolors="white")
g.add_legend()
plt.show()

还有一些小调整:set_axis_labels()函数可以自定义x和y轴名字,set(xticks, yticks)可以自定义x和y轴的刻度。fig.subplots_adjust()函数可以调整子图之间
的间隔和距离边框的大小。edgecolors可以设置散点周围的边缘颜色。
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
from pandas import Categorical
import matplotlib.pyplot as plt tips = sns.load_dataset("tips") #seaborn内置数据集,DaraFram类型
print(tips.head())
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", edgecolors="white", lw=0.5)
g.set_axis_labels("Total_bill", "Tip")
g.set(xticks=[10, 30, 50], yticks=[2, 6, 10])
g.fig.subplots_adjust(wspace=0.25, hspace=0.25)
plt.show()

可以用PairGrid对数据中的列进行两两配对绘制散点图,当然也可以指定要配对的列。
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
from pandas import Categorical
import matplotlib.pyplot as plt iris = sns.load_dataset("iris")
g = sns.PairGrid(data=iris, vars=["sepal_length", "sepal_width"], hue="species")
g.add_legend()
g.map_offdiag(plt.scatter)
g.map_diag(plt.hist)
plt.show()
函数PairGrid()中的vars参数指定要两两进行绘图的列,这些列是数据集的子列。map_offdiag和map_diag分别设置非对角的和对角的图使用的统计图类型。
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
from pandas import Categorical
import matplotlib.pyplot as plt iris = sns.load_dataset("iris")
g = sns.PairGrid(data=iris, vars=["sepal_length", "sepal_width"], hue="species")
g.add_legend()
g.map_offdiag(plt.scatter)
g.map_diag(plt.hist)
plt.show()

FacertGrid()的使用的更多相关文章
随机推荐
- DP | Luogu P1466 集合 Subset Sums
题面:P1466 集合 Subset Sums 题解: dpsum=N*(N+1)/2;模型转化为求选若干个数,填满sum/2的空间的方案数,就是背包啦显然如果sum%2!=0是没有答案的,就特判掉F ...
- 【vue-router的基础】history了解一下
概述 window.onpopstate是popstate事件在window对象上的事件处理程序. 每当处于激活状态的历史记录条目发生变化时,popstate事件就会在对应window对象上触发. 如 ...
- 第十二章 学习 shell脚本之前的基础知识
http://www.92csz.com/study/linux/12.htm [什么是shell] 简单点理解,就是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具.实际上,在shell和 ...
- JS收缩展开效果
// 收缩展开效果 $(document).ready(function () { $(".box h2").toggle(function () { $(this).next(& ...
- node 的CommonJS的引入
1.CommonJS 每个文件是一个模块,有自己的作用域 引入:
- linux操作目录命令之mkdir与rmdir
一.mkdir 创建目录(一个或多个目录) mkdir -m 777 -p path 1)-m 对新建目录设置权限 2)-p 可以是一个路径名称.此时若路径的某一级目录尚不存在,使有该选项后系统 ...
- php+html5实现无刷新上传,大文件分片上传,断点续传
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- LOJ #539. 「LibreOJ NOIP Round #1」旅游路线 倍增floyd + 思维
考试的时候是这么想的: 求出每一个点花掉 $i$ 的花费向其他点尽可能走的最长距离,然后二分这个花费,找到第一个大于 $d$ 的就输出$.$然而,我这个记忆化搜索 $TLE$ 的很惨$.$这里讲一下正 ...
- C++ 打印XPS文档
CoInitializeEx(, COINIT_MULTITHREADED); IXpsOMObjectFactory *xpsFactory; HRESULT hr = CoCreateInstan ...
- (WCF) There is already a listener on IP endpoint 0.0.0.0:9999.
有個nettcpbinding, service host總是不能起來,出現如題錯誤. 查了下,同樣的程序并沒有在進程裏面,但是看起來好像有其他的程序在占用這個Port C:\Program File ...