Create Custom Matplotlib Ticker Formatting

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to create a custom ticker in Python Matplotlib using the ticker module. The custom ticker will format the y-axis ticks in millions of dollars.

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`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) 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/bar_charts("`Bar Charts`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} python/with_statement -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} matplotlib/importing_matplotlib -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} matplotlib/figures_axes -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} matplotlib/bar_charts -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} python/lists -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} python/tuples -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} python/dictionaries -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} python/function_definition -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} python/importing_modules -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} python/data_visualization -.-> lab-48645{{"`Create Custom Matplotlib Ticker Formatting`"}} end

Import Required Libraries

First, we need to import the required libraries to create the custom ticker. We need the pyplot and ticker modules from Matplotlib.

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

Define Custom Ticker Function

Next, we need to define the custom ticker function. The custom ticker function takes two arguments - the value and tick position - and returns the formatted tick label. In this case, we will format the tick label as dollars in millions.

def millions(x, pos):
    """The two arguments are the value and tick position."""
    return f'${x*1e-6:1.1f}M'

Create the Plot

Now, we can create the plot with the custom ticker. We will create a bar chart with sample data and set the y-axis ticker to use our custom ticker function.

## Create a bar chart with sample data
fig, ax = plt.subplots()
money = [1.5e5, 2.5e6, 5.5e6, 2.0e7]
ax.bar(['Bill', 'Fred', 'Mary', 'Sue'], money)

## Set the y-axis ticker to use the custom ticker function
ax.yaxis.set_major_formatter(ticker.FuncFormatter(millions))

## Display the plot
plt.show()

Interpret the Output

The output of the code should be a bar chart with the y-axis labels formatted in millions of dollars. The tick labels will be formatted as $0.2M, $2.5M, $5.5M, and $20.0M respectively.

Summary

In this lab, you learned how to create a custom ticker in Python Matplotlib using the ticker module. You also learned how to format the tick labels in millions of dollars using a custom ticker function. This technique can be useful when working with large financial data sets.

Other Python Tutorials you may like