gpt4 book ai didi

python - 多个子图的自定义 xticks?

转载 作者:太空狗 更新时间:2023-10-30 00:54:28 24 4
gpt4 key购买 nike

使用 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,左侧子图将没有自定义 xtick。但是,我不能将 xticks 赋予单个轴。它会导致错误:

AttributeError: 'AxesSubplot' object has no attribute 'xticks'

最佳答案

如果 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()

enter image description here

关于python - 多个子图的自定义 xticks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com