matplotlib 旋转特定子图的xtick标签[重复]

aydmsdu9  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(122)

此问题已在此处有答案

Rotate tick labels in subplot(3个答案)
How to change rotation of tick labels of a subplot(1个答案)
7天前关闭
如何将子图222(图2)的X轴标签旋转90°并保持使用sns。boxplot?

fig = plt.figure(figsize=(12, 8))

# CHART 1
fig.add_subplot(221)
#sns.countplot(df_train['MSZoning'])
sns.boxplot(x='MSZoning', y='SalePrice', data=df_train)
                 
# CHART 2
fig.add_subplot(222)
#sns.boxplot(x='MSZoning', y='SalePrice', data=df_train)
sns.boxplot(x='Neighborhood', y='SalePrice', data=df_train)

# CHART 3
fig.add_subplot(223)
#sns.boxplot(x='Neighborhood', y='SalePrice', data=df_train)
sns.boxplot(x='OverallCond', y='SalePrice', data=df_train)

# CHART 4
fig.add_subplot(224)
#sns.violinplot(x = 'OverallQual', y = 'SalePrice', data=df_train, palette='Set3', bw=.2, cut=1, split=True, linewidth=1)
sns.boxplot(x='OverallQual', y='SalePrice', data=df_train)

绘图结果

已尝试:

ax.set_xticklabels(labels, rotation=45)
ax.tick_params(labelrotation=45)
oxosxuxt

oxosxuxt1#

一种方法是获取特定绘图的轴(在您的情况下为222),然后将tick_param()设置为将标签旋转90度。下面的示例供您参考...

df_train = sns.load_dataset("titanic")
fig = plt.figure(figsize=(12, 8))

# CHART 1
fig.add_subplot(221)
#sns.countplot(df_train['MSZoning'])
sns.boxplot(x='sex', y='age', data=df_train)
                 
# CHART 2
fig.add_subplot(222)
#sns.boxplot(x='MSZoning', y='SalePrice', data=df_train)
ax222=sns.boxplot(x='embark_town', y='age', data=df_train) ## Note I added ax222

# CHART 3
fig.add_subplot(223)
#sns.boxplot(x='Neighborhood', y='SalePrice', data=df_train)
sns.boxplot(x='alive', y='age', data=df_train)

# CHART 4
fig.add_subplot(224)
#sns.violinplot(x = 'OverallQual', y = 'SalePrice', data=df_train, palette='Set3', bw=.2, cut=1, split=True, linewidth=1)
sns.boxplot(x='class', y='age', data=df_train)

## Rotate ax222 by 90 degrees
ax222.tick_params(axis='x', labelrotation=90)

输出图

相关问题