【问题标题】:Alternating tick labels in matplotlibmatplotlib 中的交替刻度标签
【发布时间】:2014-08-20 21:16:56
【问题描述】:

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

A B C D

我想要的是

A   C
  B   D

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

非常感谢!

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以使用次要刻度(通过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')
    

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

    (从here借来的想法。)

    【讨论】:

    • 这成功了。希望我可以按要求的方式订购主要刻度,但这有效:)。
    猜你喜欢
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2013-12-30
    • 2012-03-14
    相关资源
    最近更新 更多