Turn off the upper/right axis tick marks

前端 未结 4 1467
天涯浪人
天涯浪人 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

提交回复
热议问题