Matplotlib Secondary Axis

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Matplotlib is a popular data visualization library in Python. Sometimes, we need to plot two different scales of data on the same graph, this is when the concept of a secondary axis comes into play. In this lab, we will learn how to create a secondary axis in Matplotlib.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") matplotlib/PlotCustomizationGroup -.-> matplotlib/axis_ticks("`Axis Ticks Customization`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/date_time("`Date and Time`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} matplotlib/importing_matplotlib -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} matplotlib/figures_axes -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} matplotlib/line_plots -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} matplotlib/legend_config -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} matplotlib/axis_ticks -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/variables_data_types -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/numeric_types -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/for_loops -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/list_comprehensions -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/lists -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/tuples -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/sets -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/function_definition -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/importing_modules -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/using_packages -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/standard_libraries -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/math_random -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/date_time -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/data_collections -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/numerical_computing -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/data_visualization -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} python/build_in_functions -.-> lab-48920{{"`Matplotlib Secondary Axis`"}} end

Import the necessary libraries

We will start by importing the necessary libraries, which are matplotlib, numpy, and datetime.

import datetime
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates
from matplotlib.ticker import AutoMinorLocator

Plot the data

We will create a simple sine wave to demonstrate the use of a secondary axis. We will plot the sine wave using degrees as the x-axis.

fig, ax = plt.subplots(layout='constrained')
x = np.arange(0, 360, 1)
y = np.sin(2 * x * np.pi / 180)
ax.plot(x, y)
ax.set_xlabel('angle [degrees]')
ax.set_ylabel('signal')
ax.set_title('Sine wave')

Create the Secondary Axis

We will now create the secondary axis and convert the x-axis from degrees to radians. We will use deg2rad as the forward function and rad2deg as the inverse function.

def deg2rad(x):
    return x * np.pi / 180

def rad2deg(x):
    return x * 180 / np.pi

secax = ax.secondary_xaxis('top', functions=(deg2rad, rad2deg))
secax.set_xlabel('angle [rad]')

Plot another example

We will now plot another example of converting from wavenumber to wavelength in a log-log scale. We will use a random spectrum for this example.

fig, ax = plt.subplots(layout='constrained')
x = np.arange(0.02, 1, 0.02)
np.random.seed(19680801)
y = np.random.randn(len(x)) ** 2
ax.loglog(x, y)
ax.set_xlabel('f [Hz]')
ax.set_ylabel('PSD')
ax.set_title('Random spectrum')

Create the Secondary X-Axis

We will create the secondary x-axis and convert from frequency to period. We will use one_over as the forward function and inverse as the inverse function.

def one_over(x):
    """Vectorized 1/x, treating x==0 manually"""
    x = np.array(x, float)
    near_zero = np.isclose(x, 0)
    x[near_zero] = np.inf
    x[~near_zero] = 1 / x[~near_zero]
    return x

## the function "1/x" is its own inverse
inverse = one_over

secax = ax.secondary_xaxis('top', functions=(one_over, inverse))
secax.set_xlabel('period [s]')

Create the Secondary Y-Axis

We will create a third example of relating the axes in a transform that is ad-hoc from the data, and is derived empirically. In this case, we will set the forward and inverse transforms functions to be linear interpolations from the one data set to the other.

fig, ax = plt.subplots(layout='constrained')
xdata = np.arange(1, 11, 0.4)
ydata = np.random.randn(len(xdata))
ax.plot(xdata, ydata, label='Plotted data')

xold = np.arange(0, 11, 0.2)
## fake data set relating x coordinate to another data-derived coordinate.
## xnew must be monotonic, so we sort...
xnew = np.sort(10 * np.exp(-xold / 4) + np.random.randn(len(xold)) / 3)

ax.plot(xold[3:], xnew[3:], label='Transform data')
ax.set_xlabel('X [m]')
ax.legend()

def forward(x):
    return np.interp(x, xold, xnew)

def inverse(x):
    return np.interp(x, xnew, xold)

secax = ax.secondary_xaxis('top', functions=(forward, inverse))
secax.xaxis.set_minor_locator(AutoMinorLocator())
secax.set_xlabel('$X_{other}$')

Create Multiple Axes

We will now create a final example that translates np.datetime64 to yearday on the x-axis and from Celsius to Fahrenheit on the y-axis. We will also add a third y-axis and place it using a float for the location argument.

dates = [datetime.datetime(2018, 1, 1) + datetime.timedelta(hours=k * 6)
         for k in range(240)]
temperature = np.random.randn(len(dates)) * 4 + 6.7
fig, ax = plt.subplots(layout='constrained')

ax.plot(dates, temperature)
ax.set_ylabel(r'$T\ [^oC]$')
plt.xticks(rotation=70)

def date2yday(x):
    """Convert matplotlib datenum to days since 2018-01-01."""
    y = x - mdates.date2num(datetime.datetime(2018, 1, 1))
    return y

def yday2date(x):
    """Return a matplotlib datenum for *x* days after 2018-01-01."""
    y = x + mdates.date2num(datetime.datetime(2018, 1, 1))
    return y

secax_x = ax.secondary_xaxis('top', functions=(date2yday, yday2date))
secax_x.set_xlabel('yday [2018]')

def celsius_to_fahrenheit(x):
    return x * 1.8 + 32

def fahrenheit_to_celsius(x):
    return (x - 32) / 1.8

secax_y = ax.secondary_yaxis(
    'right', functions=(celsius_to_fahrenheit, fahrenheit_to_celsius))
secax_y.set_ylabel(r'$T\ [^oF]$')

def celsius_to_anomaly(x):
    return (x - np.mean(temperature))

def anomaly_to_celsius(x):
    return (x + np.mean(temperature))

## use of a float for the position:
secax_y2 = ax.secondary_yaxis(
    1.2, functions=(celsius_to_anomaly, anomaly_to_celsius))
secax_y2.set_ylabel(r'$T - \overline{T}\ [^oC]$')

Summary

In this lab, we learned how to create a secondary axis in Matplotlib. We used various examples to demonstrate the concept of a secondary axis and how to create it.

Other Python Tutorials you may like