无法旋转 seaborn 图下所有子图的 xticklabels
Unable to rotate xticklabels of all subplots under seaborn figure
我有一个包含多个子图的图形。
由于某些原因,set_xticklabels
方法不适用于这些子图。
这是我正在使用的代码:
n_rows=len(customers)
fig,ax = plt.subplots(n_rows,2, figsize=(18,72))
fig.suptitle('Mytitile',fontsize=16)
fig.subplots_adjust(hspace=0.4, wspace=0.4)
#fig.tight_layout()
fig.subplots_adjust(top=0.97)
sns.set_style("ticks")
for i,client in enumerate(customers):
df = df1[df1['Client'] == client]
sns.lineplot(data = df, x='date', y='income',ax=ax[i,0]).set_title(client)
ax[i,0].yaxis.set_major_formatter(ticker.EngFormatter())
sns.lineplot(data = df, x='date', y='cost',ax=ax[i,1]).set_title(client)
ax[i,1].yaxis.set_major_formatter(ticker.EngFormatter())
我希望
ax[i,0].set_xticklabels(ax[i,0].get_xticklabels(), rotation=45,horizontalalignment='right')
可以解决问题,但事实并非如此。 (它只是完全消失了标签)
知道为什么以及如何解决它吗?
使用ax.tick_params
设置旋转会更简单,
ax[i, 0].tick_params('x', labelrotation=45)
'x'
、'y'
或 'both'
可以指定为(可选)第一个参数(默认为 'both'
),并将控制标签到哪个轴已应用旋转。
也许你应该试试
我有一个包含多个子图的图形。
由于某些原因,set_xticklabels
方法不适用于这些子图。
这是我正在使用的代码:
n_rows=len(customers)
fig,ax = plt.subplots(n_rows,2, figsize=(18,72))
fig.suptitle('Mytitile',fontsize=16)
fig.subplots_adjust(hspace=0.4, wspace=0.4)
#fig.tight_layout()
fig.subplots_adjust(top=0.97)
sns.set_style("ticks")
for i,client in enumerate(customers):
df = df1[df1['Client'] == client]
sns.lineplot(data = df, x='date', y='income',ax=ax[i,0]).set_title(client)
ax[i,0].yaxis.set_major_formatter(ticker.EngFormatter())
sns.lineplot(data = df, x='date', y='cost',ax=ax[i,1]).set_title(client)
ax[i,1].yaxis.set_major_formatter(ticker.EngFormatter())
我希望
ax[i,0].set_xticklabels(ax[i,0].get_xticklabels(), rotation=45,horizontalalignment='right')
可以解决问题,但事实并非如此。 (它只是完全消失了标签) 知道为什么以及如何解决它吗?
使用ax.tick_params
设置旋转会更简单,
ax[i, 0].tick_params('x', labelrotation=45)
'x'
、'y'
或 'both'
可以指定为(可选)第一个参数(默认为 'both'
),并将控制标签到哪个轴已应用旋转。
也许你应该试试