多个子图的自定义 xticks?
·
回答问题
使用 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()
产生这个输出:

请注意,左侧的 xticks not 旋转。我怎样才能做到这一点?
如果我删除sharex=True,左边的子图没有自定义 xticks。但是,我不能将 xticks 赋予单个轴。这会导致错误:
AttributeError: 'AxesSubplot' object has no attribute 'xticks'
Answers
如果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()

更多推荐

所有评论(0)