想要创建一个以变量名作为列,部门作为行,并且每个小图表都是y = value和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,如下所示:
g = sns.FacetGrid(df, row="department", col="variable", sharey='row')
我想更改Y轴标签以包含部门名称,例如“部门A”等。但是根据我使用的数据,特定部门可能会有所不同。所以我想用我从g中拉出的东西来参数化Y轴标签的设置,这告诉我映射到特定行的特定变量。我一直在挖掘,当我确定它存在时,我找不到它。
答案 0 :(得分:1)
您可以使用set_ylabel()
或set_xlabel
来更改特定构面上的标签。因此,在您提供的示例中,department = a | variable = x
构面将为[0,0],下一个标签为[1,0]。
因此,我们可以尝试使用row_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])