基于facet在Seaborn FacetGrid中自定义标题 [英] Customizing titles in Seaborn FacetGrid based on facet
问题描述
想做一个分面网格,以变量名为列,部门为行,每个小图都是y=value和x=date的散点图
want to make a facet grid with variable names as the columns, and departments as the rows, and each small chart is a scatter chart of y=value and x=date
我的数据有点像这样:
import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt from datetime import datetime import matplotlib.dates as mdates import random
datelist = pd.date_range(start="march 1 2020", end="may 20 2020", freq="w").tolist()
varlist = ["x", "y", "z", "x", "y", "z", "x", "y", "z", "x", "y", "z"]
deptlist = ["a", "a", "b", "a", "a", "b", "a", "a", "b", "a", "a", "b"]
vallist = random.sample(range(10, 30), 12)
df = pd.DataFrame({'date': datelist, 'value': vallist, 'variable': varlist, 'department': deptlist})
我想像这样创建一个 Seaborn Facetgrid:
I want to create a Seaborn Facetgrid like this:
g = sns.FacetGrid(df, row="department", col="variable", sharey='row')
我想更改 Y 轴标签以包含部门名称,例如部门 A"等.但根据我使用的数据,特定部门可能会有所不同.所以我想用我从 g 中提取的东西来参数化 Y 轴标签的设置,它告诉我映射到某个行的特定变量.我一直在四处挖掘,虽然我确定它存在,但我找不到它.
I would like to change the Y axis labels to contain the name of the department, so, "Department A" etc. But based on the data I am using, the particular departments might differ. So I would like to parameterize the setting of the Y Axis Label with something I pull out of g which tells me the particular variable that got mapped to a certain Row. I've been digging around and while I am sure it exists I can't find it.
推荐答案
您可以使用 set_ylabel()
或 set_xlabel
来更改特定方面的标签.因此,在您提供的示例中, department = a |variable = x
facet 为 [0,0],下一个标签为 [1,0].
You can use set_ylabel()
or set_xlabel
to change the labels at the specific facet. So in the example you have provide, the department = a | variable = x
facet will be [0,0] and the next label at [1,0].
所以我们可以试试这个,使用 row_order 输入来确保它们的顺序相同:
So we can try this, using a row_order input to ensure we have them in the same order:
rowOrder = ['a','b']
g = sns.FacetGrid(df, row="department", col="variable", sharey='row',row_order=rowOrder)
g = g.map(plt.scatter, "value", "value")
for i in range(len(rowOrder)):
g.axes[i,0].set_ylabel(rowOrder[i])
这篇关于基于facet在Seaborn FacetGrid中自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!