18-12-11-可视化库Seaborn学习笔记(六:FacetGrid)

参数:

 

data :DataFrame

整洁(“长形式”)数据框,其中每列是变量,每行是观察。

row,col,hue:strings

定义数据子集的变量,将在网格中的不同构面上绘制。请参阅*_order参数以控制此变量的级别顺序。

col_wrap:int,可选

以此宽度“包裹”列变量,以便列面跨越多行。与row小平面不相容。

share{x,y}:bool,'col'或'row'可选

如果为true,则facet将跨行和/或x轴跨行共享y轴。

height :标量,可选

每个刻面的高度(以英寸为单位)。另见:aspect

aspect:标量,可选

扫描二维码关注公众号,回复: 4542325 查看本文章

每个刻面的纵横比,因此给出每个刻面的宽度,单位为英寸。aspect * height

palette :调色板名称,列表或字典,可选

用于hue变量的不同级别的颜色。应该是可以解释的东西color_palette(),或者是将色调级别映射到matplotlib颜色的字典。

{row,col,hue} _order:lists,optional

订购分面变量的级别。默认情况下,这将是级别出现的顺序,data或者,如果变量是pandas分类,则为类别顺序。

hue_kws:param字典 - >值列表映射

插入到绘图调用中以使其他绘图属性的其他关键字参数在色调变量的级别(例如散点图中的标记)之间变化。

legend_out:bool,可选

如果True,图形尺寸将被扩展,并且图例将被绘制在中心右侧的图形之外。

despine:布尔值,可选

从图中移除顶部和右侧脊柱。

margin_titles:bool,可选

如果True,行变量的标题被绘制到最后一列的右侧。此选项是实验性的,可能无法在所有情况下使用。

{x,y} lim:元组,可选

每个面上每个轴的限制(仅当share {x,y}为True时才相关)。

subplot_kws:dict,可选

传递给matplotlib subplot(s)方法的关键字参数字典。

gridspec_kws:dict,可选

关键字参数字典传递给matplotlib的gridspec 模块(via plt.subplots)。需要matplotlib> = 1.4,如果col_wrap不是,则忽略None

详情-建议参考:Seaborn学习(一)------- 构建结构化多绘图网格(FacetGrid()、map())详解

1、数据准备

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt

sns.set(style="ticks")
np.random.seed(sum(map(ord, "axis_grids")))

tips = sns.load_dataset("tips")
print(tips.head())

#  结果
'''
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4
'''
g = sns.FacetGrid(tips, col="time") # 准备画框

g = sns.FacetGrid(tips, col="time")
g.map(plt.hist, "tip");    # plt.hist:指定为条形图; "tip":X轴名称

g = sns.FacetGrid(tips, col="sex", hue="smoker")
g.map(plt.scatter, "total_bill", "tip", alpha=.7) #alpha透明度
g.add_legend(); #     hue="smoker" 的分类标签

g = sns.FacetGrid(tips, row="smoker", col="time", margin_titles=True)
g.map(sns.regplot, "size", "total_bill", color=".1", fit_reg=False, x_jitter=.1);

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)
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");

pal = dict(Lunch="seagreen", Dinner="gray")
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")
g.add_legend();

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")
g.add_legend();

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);
#g.fig.subplots_adjust(left  = 0.125,right = 0.5,bottom = 0.1,top = 0.9, wspace=.02, hspace=.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();

g = sns.PairGrid(iris, vars=["sepal_length", "sepal_width"], 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();

猜你喜欢

转载自blog.csdn.net/tzyyy1/article/details/84961741