1

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

我想知道:

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

猫图图像 猫图图像

4

1 回答 1

1

它可以用 set_titles() 改变。{} 指定正在绘制子图的列或行。有关详细信息,请参阅 docstring,以及修改字幕的官方 seaborn 参考中的示例。

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

参数 template : string 具有格式键 {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}')

在此处输入图像描述

默认:

在此处输入图像描述

于 2021-05-27T03:29:33.983 回答