Customizing titles in Seaborn FacetGrid based on facet

旧巷老猫 提交于 2020-06-01 05:43:05

问题


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

My data is sort of like this:

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})

I want to create a Seaborn Facetgrid like this:

g = sns.FacetGrid(df, row="department", col="variable", sharey='row')

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.


回答1:


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].

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])



来源:https://stackoverflow.com/questions/61541562/customizing-titles-in-seaborn-facetgrid-based-on-facet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!