Plotting Exponential Decay with Matplotlib Semilog

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial will guide you through how to assign a log scale for the x-axis using matplotlib.axes.Axes.semilogx in Python Matplotlib. A logarithmic scale is useful when the data you want to plot spans several orders of magnitude. In this tutorial, we will use an example of plotting exponential decay as a function of time.

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 matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedPlottingGroup(["`Advanced Plotting`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/log_scale("`Logarithmic Scale`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48921{{"`Plotting Exponential Decay with Matplotlib Semilog`"}} matplotlib/figures_axes -.-> lab-48921{{"`Plotting Exponential Decay with Matplotlib Semilog`"}} matplotlib/log_scale -.-> lab-48921{{"`Plotting Exponential Decay with Matplotlib Semilog`"}} matplotlib/grid_config -.-> lab-48921{{"`Plotting Exponential Decay with Matplotlib Semilog`"}} python/tuples -.-> lab-48921{{"`Plotting Exponential Decay with Matplotlib Semilog`"}} python/importing_modules -.-> lab-48921{{"`Plotting Exponential Decay with Matplotlib Semilog`"}} python/numerical_computing -.-> lab-48921{{"`Plotting Exponential Decay with Matplotlib Semilog`"}} python/data_visualization -.-> lab-48921{{"`Plotting Exponential Decay with Matplotlib Semilog`"}} end

Import the necessary libraries

We will use numpy and matplotlib libraries in this tutorial.

import matplotlib.pyplot as plt
import numpy as np

Generate data

We will generate data for exponential decay function np.exp(-t / 5.0) using numpy library.

dt = 0.01
t = np.arange(dt, 20.0, dt)

Create a plot and set x-axis to logarithmic scale

We create a figure and axes object using subplots() method. We then plot the exponential decay function using semilogx() method and set the x-axis to a logarithmic scale using set_xscale() method. We also add a grid to the plot using grid() method.

fig, ax = plt.subplots()

ax.semilogx(t, np.exp(-t / 5.0))
ax.set_xscale('log')
ax.grid()

Show the plot

We use show() method to display the plot.

plt.show()

Summary

In this tutorial, we learned how to assign a logarithmic scale to the x-axis using matplotlib.axes.Axes.semilogx method. We also learned how to generate data for an exponential decay function and add a grid to the plot.

Other Python Tutorials you may like