Creating Plots with Different Scales

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to create plots with different scales in Python using Matplotlib. Specifically, you will learn how to create two plots on the same axes with different left and right scales.

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/AdvancedPlottingGroup(["`Advanced Plotting`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) 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/AdvancedPlottingGroup -.-> matplotlib/secondary_axis("`Secondary Axis`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-49014{{"`Creating Plots with Different Scales`"}} matplotlib/importing_matplotlib -.-> lab-49014{{"`Creating Plots with Different Scales`"}} matplotlib/figures_axes -.-> lab-49014{{"`Creating Plots with Different Scales`"}} matplotlib/line_plots -.-> lab-49014{{"`Creating Plots with Different Scales`"}} matplotlib/secondary_axis -.-> lab-49014{{"`Creating Plots with Different Scales`"}} python/tuples -.-> lab-49014{{"`Creating Plots with Different Scales`"}} python/importing_modules -.-> lab-49014{{"`Creating Plots with Different Scales`"}} python/standard_libraries -.-> lab-49014{{"`Creating Plots with Different Scales`"}} python/numerical_computing -.-> lab-49014{{"`Creating Plots with Different Scales`"}} python/data_visualization -.-> lab-49014{{"`Creating Plots with Different Scales`"}} end

Import Required Libraries

Before we begin, let's import the required libraries. We will be using Matplotlib and NumPy for this tutorial.

import matplotlib.pyplot as plt
import numpy as np

Create Some Mock Data

Next, we will create some mock data to use for our plots. We will be using numpy.arange to create an array of values ranging from 0.01 to 10.0 with a step of 0.01. We will then use numpy.exp and numpy.sin to create two sets of data.

## Create some mock data
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

Create the Plot

Now that we have our data, we can create our plot. We will begin by creating an axes object using matplotlib.pyplot.subplots(). We will then plot our first set of data on this axes object and set the label color to red.

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

Next, we will instantiate a second axes object that shares the same x-axis as the first axes object using the ax1.twinx() method. We will then plot our second set of data on this new axes object and set the label color to blue.

ax2 = ax1.twinx()

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

Finally, we will adjust the layout of our plot using the fig.tight_layout() method and display it using matplotlib.pyplot.show().

fig.tight_layout()
plt.show()

Review the Plot

Take a moment to review the plot that was created. Notice how the two sets of data have different scales on the y-axis. The first set of data is plotted in red and has a scale on the left side of the plot, while the second set of data is plotted in blue and has a scale on the right side of the plot.

Summary

Congratulations! You have learned how to create plots with different scales in Python using Matplotlib. Specifically, you have learned how to create two plots on the same axes with different left and right scales by using two different axes that share the same x-axis. You can use separate matplotlib.ticker formatters and locators as desired since the two axes are independent.

Other Python Tutorials you may like