Search code examples
pythonpython-3.xmatplotlibaxes

How can I get length of axis' major ticks in matplotlib?


I know that I can set length of ticks with length parameter of AxesSubplot.tick_params() method.

How may I determine the actual length of major ticks of certain axis?

Please assume the length may be altered.


Solution

  • The default length of the ticks is determined by the xtick.major.size or ytick.major.size, or the xtick.minor.size or ytick.minor.size rcParams.

    You may find out programmatically via

    import matplotlib.pyplot as plt
    print(plt.rcParams["xtick.major.size"])
    

    This prints in the usual case 3.5 (size in points).

    You may equally use those the rcParams to set the length

    import matplotlib.pyplot as plt
    plt.rcParams["xtick.major.size"] = 6
    

    This will work in all cases where you create the axes yourself and have not (yet) changed the length e.g. via the tick_params method.

    Otherwise, you can find out the tick length via

    ax.xaxis.majorTicks[0].tick1line.get_markersize()
    

    where ax is the axes in question.