Major and Minor Ticks in Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

In a Matplotlib plot, ticks are used to mark the position of data points on the axis. Major ticks are the larger ticks that denote the values of the data points and minor ticks are the smaller ticks that are placed between the major ticks. This tutorial shows how to use major and minor ticks in Matplotlib.

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`"]) 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`") 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-48816{{"`Major and Minor Ticks in Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48816{{"`Major and Minor Ticks in Matplotlib`"}} matplotlib/figures_axes -.-> lab-48816{{"`Major and Minor Ticks in Matplotlib`"}} matplotlib/line_plots -.-> lab-48816{{"`Major and Minor Ticks in Matplotlib`"}} python/tuples -.-> lab-48816{{"`Major and Minor Ticks in Matplotlib`"}} python/dictionaries -.-> lab-48816{{"`Major and Minor Ticks in Matplotlib`"}} python/importing_modules -.-> lab-48816{{"`Major and Minor Ticks in Matplotlib`"}} python/numerical_computing -.-> lab-48816{{"`Major and Minor Ticks in Matplotlib`"}} python/data_visualization -.-> lab-48816{{"`Major and Minor Ticks in Matplotlib`"}} end

Import the necessary libraries and create data

import matplotlib.pyplot as plt
import numpy as np

## Create data
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)

First, we import the necessary libraries, i.e., Matplotlib and NumPy. Then we create data to plot. In this example, we create a numpy array "t" and calculate another numpy array "s" using t.

Plot the data

fig, ax = plt.subplots()
ax.plot(t, s)

Next, we create a figure and axis object and plot the data on the axis.

Set the major and minor locators

## Set the major locator
ax.xaxis.set_major_locator(MultipleLocator(20))
## Set the major formatter
ax.xaxis.set_major_formatter('{x:.0f}')
## Set the minor locator
ax.xaxis.set_minor_locator(MultipleLocator(5))

Here, we set the major locator to place ticks at multiples of 20, set the major formatter to label the major ticks with ".0f" formatting, and set the minor locator to place ticks at multiples of 5.

Display the plot

plt.show()

Finally, we display the plot.

Automatic tick selection for major and minor ticks

## Create data
t = np.arange(0.0, 100.0, 0.01)
s = np.sin(2 * np.pi * t) * np.exp(-t * 0.01)

## Plot the data
fig, ax = plt.subplots()
ax.plot(t, s)

## Set the minor locator
ax.xaxis.set_minor_locator(AutoMinorLocator())

## Set the tick parameters
ax.tick_params(which='both', width=2)
ax.tick_params(which='major', length=7)
ax.tick_params(which='minor', length=4, color='r')

## Display the plot
plt.show()

In this step, we create new data and plot it. Then we set the minor locator to automatically select the number of minor ticks. After that, we set the tick parameters, i.e., the width and length of the ticks and their color, for both major and minor ticks. Finally, we display the plot.

Summary

This tutorial showed how to use major and minor ticks in Matplotlib. We saw how to set the major and minor locators and formatters and how to automatically select the number of minor ticks. We also saw how to set the tick parameters, i.e., the width and length of the ticks and their color.

Other Python Tutorials you may like