旋转子图中的 xticks(xticklabels 旋转)

Art*_*Sbr 0 python matplotlib xticks

我有一个包含四个子图的图形。我想将xticks所有suplots 旋转45 度。

对于这个问题,我相信这是可以做到的plt.setp()

# Create subplots
fig, ax = plt.subplots(2, 2, figsize=(10,5), sharex=True, sharey=True)
# Try to rotate the xticks of all axes
plt.setp(plt.xticks()[1], rotation=45) # Close attempt
# Show
plt.show()
Run Code Online (Sandbox Code Playgroud)

关闭

but*_*ife 7

您可以循环浏览每个子图,将其设置为当前轴,然后调用plt.xticks()每个子图。

fig, axes = plt.subplots(2, 2, figsize=(10,5), sharex=True, sharey=True)

for ax in axes.flatten():
    plt.sca(ax)
    plt.xticks(rotation = 45)
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述