Tuesday, July 28, 2026
HomeLanguagesMatplotlib.axes.Axes.set_axisbelow() in Python

Matplotlib.axes.Axes.set_axisbelow() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.set_axisbelow() Function

The Axes.set_axisbelow() function in axes module of matplotlib library is used to set whether axis ticks and gridlines are above or below most artists.

Syntax: Axes.set_axisbelow(self, b)

Parameters: This method accept only one parameters.

  • b: This parameter contains a boolean value and it’s possible values are : True, False or “line”.
  • Returns:This method does not returns anything.

    Below examples illustrate the matplotlib.axes.Axes.set_axisbelow() function in matplotlib.axes:

    Example 1:




    # Implementation of matplotlib function
    import matplotlib.pyplot as plt
    import numpy as np
      
    # Random test data
    np.random.seed(19680801)
    all_data = [np.random.normal(0, std, size = 100) for std in range(1, 6)]
    labels = ['x1', 'x2', 'x3', 'x4', 'x5']
      
    fig, ax = plt.subplots()
      
    bplot = ax.boxplot(all_data,
                         vert = True,  
                         patch_artist = True,  
                         labels = labels) 
    colors = ['lightpink', 'lightblue', 'lightgreen',
              "lightgrey", "yellow"]
    for patch, color in zip(bplot['boxes'], colors):
        patch.set_facecolor(color)
      
    ax.yaxis.grid(True, color ="green", lw = 2)
    ax.set_axisbelow(True)
    ax.set_xlabel('Samples')
    ax.set_ylabel('Observed values')
    ax.set_title('matplotlib.axes.Axes.set_axisbelow() \
    Example\n', fontsize = 12, fontweight ='bold')
    plt.show()

    
    

    Output:

    Example 2:




    # Implementation of matplotlib function
    import matplotlib.pyplot as plt
    import numpy as np
      
    # Random test data
    np.random.seed(19680801)
    all_data = [np.random.normal(0, std, size = 100) for std in range(1, 6)]
    labels = ['x1', 'x2', 'x3', 'x4', 'x5']
      
    fig, ax = plt.subplots()
      
    bplot = ax.boxplot(all_data,
                         vert = True,  
                         patch_artist = True,  
                         labels = labels) 
    colors = ['lightpink', 'lightblue', 'lightgreen',
              "lightgrey", "yellow"]
    for patch, color in zip(bplot['boxes'], colors):
        patch.set_facecolor(color)
      
    ax.yaxis.grid(True, color ="green", lw = 2)
    ax.set_axisbelow(False)
    ax.set_xlabel('Samples')
    ax.set_ylabel('Observed values')
    ax.set_title('matplotlib.axes.Axes.set_axisbelow()\
     Example\n', fontsize = 12, fontweight ='bold')
    plt.show()

    
    

    Output:

    Dominic
    Dominichttp://wardslaus.com
    infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
    RELATED ARTICLES

    2 COMMENTS

    Most Popular

    Dominic
    32520 POSTS0 COMMENTS
    Milvus
    131 POSTS0 COMMENTS
    Nango Kala
    6903 POSTS0 COMMENTS
    Nicole Veronica
    12017 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    12115 POSTS0 COMMENTS
    Shaida Kate Naidoo
    7023 POSTS0 COMMENTS
    Ted Musemwa
    7265 POSTS0 COMMENTS
    Thapelo Manthata
    6980 POSTS0 COMMENTS
    Umr Jansen
    6973 POSTS0 COMMENTS