gpt4 book ai didi

python - matplotlib 中的交替刻度标签

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:48 26 4
gpt4 key购买 nike

我正在寻找一种在两个级别上交替我的 x 轴刻度标签的方法。我现在的标签是:

A B C D

我想要的是

A   C
B D

但我似乎无法在 google/docs/SO 上找到方法。

非常感谢!

最佳答案

您可以使用小刻度(通过 set_minor_locatorset_minor_formatter 定义)。然后你可以转移例如使用 tick_params 的所有主要刻度:

import pylab as pl
from matplotlib.ticker import FixedLocator, FormatStrFormatter

pl.plot(range(100))

pl.axes().xaxis.set_major_locator(FixedLocator(range(0, 100, 10)))
pl.axes().xaxis.set_minor_locator(FixedLocator(range(5, 100, 10)))
pl.axes().xaxis.set_minor_formatter(FormatStrFormatter("%d"))
pl.axes().tick_params(which='major', pad=20, axis='x')

enter image description here

但要小心 FixedLocator:为了避免生成两次主要刻度(一次作为主要刻度,一次作为次要刻度),我选择了固定刻度位置而不是使用 MultipleLocator。您需要根据您的数据和窗口大小调整这些刻度线位置。

(从 here 借来的想法。)

关于python - matplotlib 中的交替刻度标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25414578/

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