简体   繁体   English

自定义sharex刻度标签

[英]Customize sharex tick label

My plot contains 13 x 4 subplots. 我的地块包含13 x 4个子图。 x-labels are shared. x标签是共享的。 So, only the bottom row that has x-tick labels, which is what I want. 因此,只有最下面的行具有x-tick标签,这就是我想要的。 The df uses datetime index. df使用datetime索引。

Now, I want to rotate the x-tick labels. 现在,我要旋转x-tick标签。 But, my code works on the last subplot only. 但是,我的代码仅适用于最后一个子图。

Here is the code: 这是代码:

x = df.index
fig, axs = plt.subplots(nrows=13, ncols=4, sharex=True, sharey=False, figsize=(20, 35))

axs[0, 0].plot(x, df['y1'])
axs[0, 1].plot(x, df['y2'])
.
.
.
axs[12, 3].plot(x, df['y52'])

plt.tick_params(axis="x", rotation=45)

plt.show()

You will have to loop over the last row and then individually rotate the tick labels. 您将必须在最后一行上循环,然后分别旋转刻度线标签。 You can access the last row of subplots as axs[-1, :] . 您可以以axs[-1, :]访问子图的最后一行。 I am showing a sample answer for a smaller number of figures 我正在显示少量数字的示例答案

import matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows=3, ncols=4, sharex=True, sharey=False, figsize=(8, 5))

axs[0, 0].plot([1,2,3])
axs[0, 1].plot([1,2,3])
axs[2, 3].plot([1,2,3])

for ax in axs[-1, :]:
    ax.tick_params(axis="x", rotation=45)

plt.tight_layout()
plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM