仅显示列值而不是 column_name = catplot 子图标题中的列值
Show only column value instead of column_name = column value in catplot subplots titles
我正在尝试生成不同的箱线图,这些箱线图描述了某些产品系列和行业组(“生产”、“贸易”、“商业”、“休闲与健康”)的变量分布。
我想知道:
- 如果有办法在子图标题中只显示“生产”、“贸易”、“商业”和“休闲与健康”,而不是每次都显示“industry_group = 生产”等等在
- 如果有办法将这些子图的标题从每个子图的上边框提高一点(在我发布的图像中你可以清楚地看到它们没有 space)
在我正在使用的代码下方,这里有一张图片向您展示我得到的结果 --> 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 ))
猫图图像
可以用set_titles()改变。 {} 指定要绘制子图的列或行。详见docstring,seaborn官方参考中的例子修改字幕。
Signature: g.set_titles(template=None, row_template=None, col_template=None, **kwargs)
Docstring:
Draw titles either above each facet or on the grid margins.
Parameters
template : string
Template for all titles with the formatting keys {col_var} and
{col_name} (if using a col
faceting variable) and/or {row_var}
and {row_name} (if using a row
faceting variable).
row_template:
Template for the row variable when titles are drawn on the grid
margins. Must have {row_var} and {row_name} formatting keys.
col_template:
Template for the row variable when titles are drawn on the grid
margins. Must have {col_var} and {col_name} formatting keys.
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}')
默认:
我正在尝试生成不同的箱线图,这些箱线图描述了某些产品系列和行业组(“生产”、“贸易”、“商业”、“休闲与健康”)的变量分布。
我想知道:
- 如果有办法在子图标题中只显示“生产”、“贸易”、“商业”和“休闲与健康”,而不是每次都显示“industry_group = 生产”等等在
- 如果有办法将这些子图的标题从每个子图的上边框提高一点(在我发布的图像中你可以清楚地看到它们没有 space)
在我正在使用的代码下方,这里有一张图片向您展示我得到的结果 --> 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 ))
猫图图像
可以用set_titles()改变。 {} 指定要绘制子图的列或行。详见docstring,seaborn官方参考中的例子修改字幕。
Signature: g.set_titles(template=None, row_template=None, col_template=None, **kwargs) Docstring: Draw titles either above each facet or on the grid margins.
Parameters template : string Template for all titles with the formatting keys {col_var} and {col_name} (if using a
col
faceting variable) and/or {row_var} and {row_name} (if using arow
faceting variable). row_template: Template for the row variable when titles are drawn on the grid margins. Must have {row_var} and {row_name} formatting keys. col_template: Template for the row variable when titles are drawn on the grid margins. Must have {col_var} and {col_name} formatting keys.
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}')
默认: