本文是对seaborn.FacetGrid摘抄和记录,仅供自己学习记录。 [参考链接](http://seaborn.pydata.org/generated/seaborn.FacetGrid.html#seaborn.FacetGrid
- 参数介绍
- 1.例子
- 1.1 使用提示数据集初始化2x2的构面网格:
- 1.2 在每个子图绘制一个单变量图:
- 1.3 将其他关键字参数传递给映射函数:
- 1.4 在每个方面上绘制一个双变量函数:
- 1.5 将变量之一分配给绘图元素的颜色:
- 1.6 更改facet的高度和长宽比:
- 1.7 指定绘图元素的顺序:
- 1.8 使用其他调色板:
- 1.9 使用字典将色调级别映射到颜色:
- 1.10 另外,对色阶使用不同的标记:
- 1.11 将具有多个级别的列变量“包装”到行中:
- 1.12 定义一个自定义双变量函数以映射到网格:
- 1.13 定义一个使用DataFrame对象并接受列名称作为位置变量的自定义函数:
- 1.14 绘制后使用不同的轴标签:
- 1.15 设置在子图之间共享的其他属性:
- 1.16 对构面标题使用其他模板:
- 1.17 收紧各个方面:
- 2.方法
- 3.属性
参考链接
参数介绍
class seaborn.FacetGrid(data, row=None, col=None, hue=None, col_wrap=None, sharex=True, sharey=True, height=3, aspect=1, palette=None, row_order=None, col_order=None, hue_order=None, hue_kws=None, dropna=True, legend_out=True, despine=True, margin_titles=False, xlim=None, ylim=None, subplot_kws=None, gridspec_kws=None, size=None)
1.例子
1.1 使用提示数据集初始化2x2的构面网格:
import seaborn as sns; sns.set(style="ticks", color_codes=True)
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="time", row="smoker")
1.2 在每个子图绘制一个单变量图:
import matplotlib.pyplot as plt
g = sns.FacetGrid(tips, col="time", row="smoker")
g = g.map(plt.hist, "total_bill")
(请注意,不必重新捕获返回的变量;它是同一对象,但是在示例中这样做会使处理doctest的烦恼有所减少)。
1.3 将其他关键字参数传递给映射函数:
import numpy as np
bins = np.arange(0, 65, 5)
g = sns.FacetGrid(tips, col="time", row="smoker")
g = g.map(plt.hist, "total_bill", bins=bins, color="r")
1.4 在每个方面上绘制一个双变量函数:
g = sns.FacetGrid(tips, col="time", row="smoker")
g = g.map(plt.scatter, "total_bill", "tip", edgecolor="w")
1.5 将变量之一分配给绘图元素的颜色:
g = sns.FacetGrid(tips, col="time", hue="smoker")
g = (g.map(plt.scatter, "total_bill", "tip", edgecolor="w")
.add_legend())
1.6 更改facet的高度和长宽比:
g = sns.FacetGrid(tips, col="day", height=4, aspect=.5)
g = g.map(plt.hist, "total_bill", bins=bins)
1.7 指定绘图元素的顺序:
g = sns.FacetGrid(tips, col="smoker", col_order=["Yes", "No"])
g = g.map(plt.hist, "total_bill", bins=bins, color="m")
1.8 使用其他调色板:
kws = dict(s=50, linewidth=.5, edgecolor="w")
g = sns.FacetGrid(tips, col="sex", hue="time", palette="Set1",
hue_order=["Dinner", "Lunch"])
g = (g.map(plt.scatter, "total_bill", "tip", **kws)
.add_legend())
1.9 使用字典将色调级别映射到颜色:
pal = dict(Lunch="seagreen", Dinner="gray")
g = sns.FacetGrid(tips, col="sex", hue="time", palette=pal,
hue_order=["Dinner", "Lunch"])
g = (g.map(plt.scatter, "total_bill", "tip", **kws)
.add_legend())
1.10 另外,对色阶使用不同的标记:
g = sns.FacetGrid(tips, col="sex", hue="time", palette=pal,
hue_order=["Dinner", "Lunch"],
hue_kws=dict(marker=["^", "v"]))
g = (g.map(plt.scatter, "total_bill", "tip", **kws)
.add_legend())
1.11 将具有多个级别的列变量“包装”到行中:
att = sns.load_dataset("attention")
g = sns.FacetGrid(att, col="subject", col_wrap=5, height=1.5)
g = g.map(plt.plot, "solutions", "score", marker=".")
1.12 定义一个自定义双变量函数以映射到网格:
from scipy import stats
def qqplot(x, y, **kwargs):
_, xr = stats.probplot(x, fit=False)
_, yr = stats.probplot(y, fit=False)
sns.scatterplot(xr, yr, **kwargs)
g = sns.FacetGrid(tips, col="smoker", hue="sex")
g = (g.map(qqplot, "total_bill", "tip", **kws)
.add_legend())
1.13 定义一个使用DataFrame对象并接受列名称作为位置变量的自定义函数:
import pandas as pd
df = pd.DataFrame(
data=np.random.randn(90, 4),
columns=pd.Series(list("ABCD"), name="walk"),
index=pd.date_range("2015-01-01", "2015-03-31",
name="date"))
df = df.cumsum(axis=0).stack().reset_index(name="val")
def dateplot(x, y, **kwargs):
ax = plt.gca()
data = kwargs.pop("data")
data.plot(x=x, y=y, ax=ax, grid=False, **kwargs)
g = sns.FacetGrid(df, col="walk", col_wrap=2, height=3.5)
g = g.map_dataframe(dateplot, "date", "val")
1.14 绘制后使用不同的轴标签:
g = sns.FacetGrid(tips, col="smoker", row="sex")
g = (g.map(plt.scatter, "total_bill", "tip", color="g", **kws)
.set_axis_labels("Total bill (US Dollars)", "Tip"))
1.15 设置在子图之间共享的其他属性:
g = sns.FacetGrid(tips, col="smoker", row="sex")
g = (g.map(plt.scatter, "total_bill", "tip", color="r", **kws)
.set(xlim=(0, 60), ylim=(0, 12),
xticks=[10, 30, 50], yticks=[2, 6, 10]))
1.16 对构面标题使用其他模板:
g = sns.FacetGrid(tips, col="size", col_wrap=3)
g = (g.map(plt.hist, "tip", bins=np.arange(0, 13), color="c")
.set_titles("{col_name} diners"))
1.17 收紧各个方面:
g = sns.FacetGrid(tips, col="smoker", row="sex",
margin_titles=True)
g = (g.map(plt.scatter, "total_bill", "tip", color="m", **kws)
.set(xlim=(0, 60), ylim=(0, 12),
xticks=[10, 30, 50], yticks=[2, 6, 10])
.fig.subplots_adjust(wspace=.05, hspace=.05))
2.方法
项目 | 说明 |
---|---|
** _init _**(self, data[, row, col, hue, …]) | 初始化matplotlib图形和FacetGrid对象。 |
add_legend(self[, legend_data, title, …]) | 绘制图例,也许将其放置在轴外并调整图的大小。 |
despine(self, **kwargs) | 从刻面上除去轴刺。 |
facet_axis(self, row_i, col_j) | 使这些索引所标识的轴处于活动状态并返回。 |
facet_data(self) | 每个方面的名称索引和数据子集的生成器。 |
map(self, func, *args, **kwargs) | 将绘图函数应用于数据的每个方面。 |
map_dataframe(self, func, *args, **kwargs) | 像,.map但将args作为字符串传递,并将数据插入kwargs中。 |
savefig(self, *args, **kwargs) | 保存该图。 |
set(self, **kwargs) | 在每个子图轴上设置属性。 |
set_axis_labels(self[, x_var, y_var]) | 在网格的左列和底部行设置轴标签。 |
set_titles(self[, template, row_template, …]) | 在每个构面上方或网格边距上绘制标题。 |
set_xlabels(self[, label]) | 在网格的底行上标记x轴。 |
set_xticklabels(self[, labels, step]) | 设置网格的x轴刻度标签。 |
set_ylabels(self[, label]) | 在网格的左列上标记y轴。 |
set_yticklabels(self[, labels]) | 在网格的左列上设置y轴刻度标签。 |
3.属性
名称 | 说明 |
---|---|
ax | 轻松访问单轴. |