Labeling Ticks Using Engineering Notation

PythonPythonBeginner
Practice Now

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

Introduction

In data visualization, it is essential to label the ticks on axes accurately. The EngFormatter in Matplotlib is a class that enables one to label the ticks on an axis using engineering notation. Engineering notation is a mathematical representation of numbers that uses powers of ten with a multiple of three. It is a concise way to express large or small numbers that are difficult to read or write in standard notation. In this lab, we will learn how to label ticks on an axis using engineering notation.

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`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedPlottingGroup(["`Advanced Plotting`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) 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`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") 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/log_scale("`Logarithmic Scale`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") 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/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/with_statement -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} matplotlib/importing_matplotlib -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} matplotlib/figures_axes -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} matplotlib/line_plots -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} matplotlib/log_scale -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/for_loops -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/lists -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/tuples -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/sets -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/importing_modules -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/using_packages -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/standard_libraries -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/math_random -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/numerical_computing -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} python/data_visualization -.-> lab-48711{{"`Labeling Ticks Using Engineering Notation`"}} end

Import Required Libraries

The first step is to import the required libraries. In this lab, we will use Matplotlib, NumPy, and EngFormatter.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import EngFormatter

Create Artificial Data

We need to create some artificial data to plot. In this lab, we will plot the log of the frequency (in Hz) against the log of the power (in Watts). We will use the numpy library to generate the data.

## Fixing random state for reproducibility
prng = np.random.RandomState(19680801)

## Create artificial data to plot.
## The x data span over several decades to demonstrate several SI prefixes.
xs = np.logspace(1, 9, 100)
ys = (0.8 + 0.4 * prng.uniform(size=100)) * np.log10(xs)**2

Create the Figure and Subplots

We need to create a figure and subplots to display the data. In this lab, we will create two subplots, side by side.

## Figure width is doubled (2*6.4) to display nicely 2 subplots side by side.
fig, (ax0, ax1) = plt.subplots(nrows=2, figsize=(7, 9.6))
for ax in (ax0, ax1):
    ax.set_xscale('log')

Label the Ticks Using Engineering Notation

We will now label the ticks on the x-axis using engineering notation. In the first subplot, we will use the default settings, and in the second subplot, we will use the options places and sep to specify the number of digits after the decimal point and the separator between the number and the prefix/unit.

## Demo of the default settings, with a user-defined unit label.
ax0.set_title('Full unit ticklabels, w/ default precision & space separator')
formatter0 = EngFormatter(unit='Hz')
ax0.xaxis.set_major_formatter(formatter0)
ax0.plot(xs, ys)
ax0.set_xlabel('Frequency')

## Demo of the options `places` (number of digit after decimal point) and
## `sep` (separator between the number and the prefix/unit).
ax1.set_title('SI-prefix only ticklabels, 1-digit precision & '
              'thin space separator')
formatter1 = EngFormatter(places=1, sep="\N{THIN SPACE}")  ## U+2009
ax1.xaxis.set_major_formatter(formatter1)
ax1.plot(xs, ys)
ax1.set_xlabel('Frequency [Hz]')

Display the Plot

We will now display the plot using the plt.show() function.

plt.tight_layout()
plt.show()

Summary

In this lab, we learned how to label ticks on an axis using engineering notation. We used the EngFormatter class in Matplotlib to label the ticks on the x-axis of a plot. We also learned how to create subplots and customize the tick labels using the EngFormatter options places and sep. Engineering notation is a concise and clear way to express large or small numbers that are difficult to read or write in standard notation.

Other Python Tutorials you may like