Formatting Tick Labels with Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial lab will guide you through the process of formatting tick labels using the Python Matplotlib library. It will cover the default tick formatter and various configurations possible via ~.axes.Axes.ticklabel_format.

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/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) 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/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} matplotlib/line_plots -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} matplotlib/matplotlib_config -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} python/booleans -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} python/for_loops -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} python/lists -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} python/tuples -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} python/dictionaries -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} python/importing_modules -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} python/numerical_computing -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} python/data_visualization -.-> lab-48910{{"`Formatting Tick Labels with Matplotlib`"}} end

Import Required Libraries

In order to use the Python Matplotlib library, we need to import it into our Python environment. Additionally, we will be using the NumPy library to generate data for our example plots.

import matplotlib.pyplot as plt
import numpy as np

Generate Data for Example Plots

We will generate data for three plots to demonstrate the different configurations possible with ~.axes.Axes.ticklabel_format.

x = np.arange(0, 1, .01)

## Plot 1
plot1_x = x * 1e5 + 1e10
plot1_y = x * 1e-10 + 1e-5

## Plot 2
plot2_x = x * 1e5
plot2_y = x * 1e-4

## Plot 3
plot3_x = -x * 1e5 - 1e10
plot3_y = -x * 1e-5 - 1e-10

Create Subplots for Example Plots

We will create a 3 x 3 grid of subplots to display our example plots.

fig, axs = plt.subplots(
    3, 3, figsize=(9, 9), layout="constrained", gridspec_kw={"hspace": 0.1})

Plot Data on Subplots

We will plot our generated data on the subplots we created in Step 3.

for col in axs.T:
    col[0].plot(plot1_x, plot1_y)
    col[1].plot(plot2_x, plot2_y)
    col[2].plot(plot3_x, plot3_y)

Configure Tick Label Formatting

We will configure the tick label formatting for our subplots. The first subplot will use the default settings, the second subplot will use fancy formatting of mathematical expressions, and the third subplot will not use offset notation.

## Subplot 1 (default settings)
axs[0, 0].set_title("default settings")

## Subplot 2 (useMathText=True)
for ax in axs[:, 1]:
    ax.ticklabel_format(useMathText=True)
axs[0, 1].set_title("useMathText=True")

## Subplot 3 (useOffset=False)
for ax in axs[:, 2]:
    ax.ticklabel_format(useOffset=False)
axs[0, 2].set_title("useOffset=False")

Display Example Plots

We will display the example plots with the configured tick label formatting.

plt.rcParams.update({"axes.titleweight": "bold", "axes.titley": 1.1})
plt.show()

Summary

In this tutorial lab, we learned how to format tick labels using the Python Matplotlib library. We generated data for three example plots and configured the tick label formatting for each plot. We displayed the example plots to visualize the different tick label formatting configurations.

Other Python Tutorials you may like