多个子图的自定义xticks?

9
使用Matplotlib,我有两个子图,我希望它们具有相同的自定义字符串xticks。
下面是我尝试过的最小示例:
import matplotlib.pyplot as plt
f, axs = plt.subplots(ncols=2, sharex=True)
plt.xticks(range(6), [str(x)+"foo" for x in range(6)], rotation='45')
for i in range(2):
    ax = axs[i]
    ax.plot(range(6), range(6))
f.show()

生成以下输出:

output of code example

请注意,左侧的xticks未旋转。我该怎么做呢?

如果我删除 sharex=True,则左侧子图没有自定义xticks。 然而,我不能给单个轴设置xticks。这将导致错误:

AttributeError: 'AxesSubplot' object has no attribute 'xticks'
1个回答

12

如果sharex不是主要的,请使用此代码:

import matplotlib.pyplot as plt
f, axs = plt.subplots(ncols=2)
for i in range(2):
    ax = axs[i]
    ax.set_xticks(range(6))
    ax.set_xticklabels([str(x)+"foo" for x in range(6)], rotation=45)
    ax.plot(range(6), range(6))
plt.show()

在此输入图片描述


我可以没有ShareX。谢谢! - qznc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接