Open In App

Matplotlib.axes.Axes.set_axisbelow() in Python

Last Updated : 19 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:



    Like Article
    Suggest improvement
    Share your thoughts in the comments

    Similar Reads