仅显示列值而不是 catplot 子图标题中的 column_name = 列值

Ste*_*ini 2 python seaborn catplot

我正在尝试生成不同的箱线图,描述某些产品系列和行业组(“生产”、“贸易”、“商业”、“休闲与福祉”)的变量分布。

我想知道:

  1. 如果有办法在子图标题中仅显示“生产”、“贸易”、“商业”和“休闲与福祉”,而不是每次都显示“industry_group = Production”等
  2. 如果有办法将这些子图标题从每个子图的上边框提高一点(在我发布的图像中,您可以清楚地看到它们没有空间)

在我正在使用的代码下方,这里有一张图像向您展示我得到的内容 --> Catplot Image

import matplotlib.pyplot as plt
import seaborn as sns

sns.set_context("talk")

boxplot = sns.catplot(
    x='family', 
    y='lag',
    hue="SF_type",
    col="industry_group",
    data=df1, 
    kind="box",
    orient="v",
    height=8, 
    aspect=2,
    col_wrap=1,
    order=fam_sort,
    palette='Set3',
    notch=True,
    legend=True,
    legend_out=True,
    showfliers=False,
    showmeans=True,
    meanprops={
        "marker":"o",
        "markerfacecolor":"white", 
        "markeredgecolor":"black",
        "markersize":"10"
    }
)

boxplot.fig.suptitle("Boxplots by Product Basket, Industry Group & SF Type", y=1.14)

#repeat xticks for each plot
for ax in boxplot.axes:
    plt.setp(ax.get_xticklabels(), visible=True, rotation=75)
    plt.subplots_adjust(hspace=1.2, top = 1.1)
    
#remove axis titles
for ax in boxplot.axes.flatten():
    ax.set_ylabel('')
    ax.set_xlabel('')
    limit_upper = max(df1.groupby(['family','SF_type','industry_group'])['lag'].quantile(0.75))
    plt.setp(ax.texts, text="")
    ax.set(ylim=(-10, limit_upper ))
Run Code Online (Sandbox Code Playgroud)

Catplot 图像 Catplot 图像

r-b*_*ers 6

可以使用 set_titles() 更改它。{} 指定要绘制子图的列或行。有关更多详细信息,请参阅文档字符串,以及官方seaborn参考中的示例来修改字幕。

签名: g.set_titles(template=None, row_template=None, col_template=None, **kwargs) 文档字符串:在每个面上方或网格边缘上绘制标题。

参数 template :带有格式化键 {col_var} 和 {col_name} (如果使用col分面变量)和/或 {row_var} 和 {row_name} (如果使用row分面变量)的所有标题的字符串模板。row_template:在网格边距上绘制标题时行变量的模板。必须具有 {row_var} 和 {row_name} 格式键。col_template:在网格边距上绘制标题时行变量的模板。必须具有 {col_var} 和 {col_name} 格式化键。

import seaborn as sns
sns.set_theme(style="ticks")

titanic = sns.load_dataset("titanic")

g = sns.catplot(x="embark_town", y="age",
                hue="sex", row="class",
                data=titanic[titanic.embark_town.notnull()],
                height=2, aspect=3, palette="Set3",
                kind="violin", dodge=True, cut=0, bw=.2)
g.set_titles(template='{row_name}')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

默认:

在此输入图像描述