Turn off the upper/right axis tick marks

前端 未结 4 1457
天涯浪人
天涯浪人 2020-12-17 11:16

I want to make the ticks on the right and upper axis invisible and am not sure what the third line should be:

import matplotlib.pyplot as plt
plt.plot(X,Y)
#         


        
相关标签:
4条回答
  • 2020-12-17 11:19

    As pointed by @imsc, You can tweak the visibility of the tick marks by setting the position of the ticks to the bottom and left (if you don't want them on top and right) using the ax.xaxis.set_ticks_position and ax.yaxis.set_ticks_position methods.

    If you also want to set the axis itself invisible, check out the matplotlib.spines class. You just have to set the color of the spines to none using the ax.spines[spine].set_color method.

    If you have a lot of plots to display, and don't want to turn manually the axis/ticks off each time, below is a function that will do the job for you.

    Basically, you define for each axis the color you want (in this case none will render it invisible), and the function will also "turn off" the ticks marks for all the axis invisible. I have also added options to define the linewidth of the axis lines, as well as the fontsize of the ticklabels and the pad between the ticklabels and the ticks.

    def customaxis(ax, c_left='k', c_bottom='k', c_right='none', c_top='none',
                   lw=3, size=12, pad=8):
    
        for c_spine, spine in zip([c_left, c_bottom, c_right, c_top],
                                  ['left', 'bottom', 'right', 'top']):
            if c_spine != 'none':
                ax.spines[spine].set_color(c_spine)
                ax.spines[spine].set_linewidth(lw)
            else:
                ax.spines[spine].set_color('none')
        if (c_bottom == 'none') & (c_top == 'none'): # no bottom and no top
            ax.xaxis.set_ticks_position('none')
        elif (c_bottom != 'none') & (c_top != 'none'): # bottom and top
            ax.tick_params(axis='x', direction='out', width=lw, length=7,
                          color=c_bottom, labelsize=size, pad=pad)
        elif (c_bottom != 'none') & (c_top == 'none'): # bottom but not top
            ax.xaxis.set_ticks_position('bottom')
            ax.tick_params(axis='x', direction='out', width=lw, length=7,
                           color=c_bottom, labelsize=size, pad=pad)
        elif (c_bottom == 'none') & (c_top != 'none'): # no bottom but top
            ax.xaxis.set_ticks_position('top')
            ax.tick_params(axis='x', direction='out', width=lw, length=7,
                           color=c_top, labelsize=size, pad=pad)
        if (c_left == 'none') & (c_right == 'none'): # no left and no right
            ax.yaxis.set_ticks_position('none')
        elif (c_left != 'none') & (c_right != 'none'): # left and right
            ax.tick_params(axis='y', direction='out', width=lw, length=7,
                           color=c_left, labelsize=size, pad=pad)
        elif (c_left != 'none') & (c_right == 'none'): # left but not right
            ax.yaxis.set_ticks_position('left')
            ax.tick_params(axis='y', direction='out', width=lw, length=7,
                           color=c_left, labelsize=size, pad=pad)
        elif (c_left == 'none') & (c_right != 'none'): # no left but right
            ax.yaxis.set_ticks_position('right')
            ax.tick_params(axis='y', direction='out', width=lw, length=7,
                           color=c_right, labelsize=size, pad=pad)
    

    Below is an example of how to use it:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig, ([ax1, ax2], [ax3, ax4]) = plt.subplots(nrows=2, ncols=2)
    
    for ax in [ax1, ax2, ax3, ax4]:
        ax.plot(np.random.randn(10), lw=2)
    
    customaxis(ax1) #default: no right and top axis
    customaxis(ax2, c_left='none', c_bottom='none', c_right='k', c_top='k')
    customaxis(ax3, c_left='none', c_bottom='k', c_right='k', c_top='none')
    customaxis(ax4, c_left='k', c_bottom='none', c_right='none', c_top='k')
    
    plt.show()
    

    spines

    0 讨论(0)
  • 2020-12-17 11:22

    Have a look at http://matplotlib.sourceforge.net/examples/pylab_examples/spine_placement_demo.html

    import pylab as p
    t = p.arange(0.0, 2.0, 0.01)
    ax=p.subplot(111)
    s = p.sin(2*p.pi*t)
    ax.plot(t, s, color='r',linewidth=1.0)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    p.show()
    
    0 讨论(0)
  • 2020-12-17 11:41

    tick_params should do it.

    I don't have matplotlib installed on my current computer, so I can't test it, but this should work:

    import matplotlib.pyplot as plt
    
    ax = plt.subplot(111)
    plt.plot([0,1], [0,1])
    ax.tick_params(labeltop='off', labelright='off')
    plt.show()
    
    0 讨论(0)
  • 2020-12-17 11:44
    plt.tick_params(top='off', right='off', which='both') 
    

    I used above code with 'on' instead of 'off' ,for displaying tick marks on top and right along with default 'bottom'and 'left', in matplotlib 2.0

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