Open In App

Matplotlib.axes.Axes.secondary_yaxis() in Python

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.secondary_yaxis() Function

The Axes.secondary_yaxis() function in axes module of matplotlib library is also used to add a second y-axis to this axes.

Syntax: Axes.secondary_yaxis(self, location, *, functions=None, **kwargs) 

Parameters: This method accept the following parameters that are described below:

  • location : This parameter is the position to put the secondary axis.
  • functions : This parameter is used to specify the transform function and its inverse.

Returns: This method returns the following:

  • ax : This return the axes._secondary_axes.SecondaryAxis.

Note: This function works in Matplotlib version >= 3.1 

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

Example 1: 

Python3




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
     
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
  
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
  
secax = ax.secondary_yaxis('right')
secax.set_ylabel('Secondary-Y-Axis')
ax.set_title('matplotlib.axes.Axes.secondary_yaxis() Example',
             fontsize = 14, fontweight ='bold')
plt.show()


Output:

  

Example 2: 

Python3




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
import datetime
import matplotlib.dates as mdates
from matplotlib.transforms import Transform
from matplotlib.ticker import (
    AutoLocator, AutoMinorLocator)
 
fig, ax = plt.subplots(constrained_layout = True)
x = np.arange(0, 500, 2)
y = np.sin(3 * x * np.pi / 180)
ax.plot(y, x)
ax.set_ylabel('Degree')
ax.set_xlabel('Frequency')
 
 
def val1(y):
    return y * np.pi / 180
 
 
def val2(y):
    return y * 180 / np.pi
 
secax = ax.secondary_yaxis('right', functions =(val1, val2))
secax.set_ylabel('Radian')
ax.set_title('matplotlib.axes.Axes.secondary_yaxis() Example',
             fontsize = 14, fontweight ='bold')
plt.show()


Output:

 



Last Updated : 28 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads