Minor ticks with only major tick labels are shown

前端 未结 1 1911
野的像风
野的像风 2021-02-04 20:51

I would like to have minor ticks on a axis but show only major tick labels. For instance, minor ticks are [19, 20, 21, ... 40, 41] and major tick labels are [20, 25, 30, 35, 40]

相关标签:
1条回答
  • 2021-02-04 21:32

    I don't really understand why is it difficult to use MultipleLocator in your example.

    By adding these lines in your code

    from matplotlib.ticker import MultipleLocator, FormatStrFormatter
    
    majorLocator   = MultipleLocator(5)
    majorFormatter = FormatStrFormatter('%d')
    minorLocator   = MultipleLocator(1)
    
    ax.xaxis.set_major_locator(majorLocator)
    ax.xaxis.set_major_formatter(majorFormatter)
    ax.xaxis.set_minor_locator(minorLocator)
    

    You'll get this image, which I understood it is what you want (isn't it?): enter image description here


    In case you don't want the ticks to show below your range of data, define your ticks manually using the FixedLocator:

    from matplotlib.ticker import FixedLocator
    
    majorLocator   = FixedLocator(np.linspace(20,40,5))
    minorLocator   = FixedLocator(np.linspace(19,41,23))
    

    And you'll get this image: enter image description here

    0 讨论(0)
提交回复
热议问题