Matplotlib Tick Placement Customization

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

In this lab, we will learn how to set the behavior of tick auto-placement in Matplotlib. By default, Matplotlib will choose the number of ticks and tick positions so that there is a reasonable number of ticks on the axis and they are located at "round" numbers. However, there may be no ticks on the edges of the plot. We will learn how to switch the axes.autolimit_mode to 'round_numbers' to keep ticks at round numbers and also have ticks at the edges.

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/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) 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`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") 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 matplotlib/importing_matplotlib -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} matplotlib/figures_axes -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} matplotlib/scatter_plots -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} matplotlib/matplotlib_config -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} python/lists -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} python/tuples -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} python/importing_modules -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} python/standard_libraries -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} python/math_random -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} python/numerical_computing -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} python/data_visualization -.-> lab-48559{{"`Matplotlib Tick Placement Customization`"}} end

Scatter plot without round_numbers autolimit_mode

In this step, we will create a scatter plot without round_numbers autolimit_mode and observe the behavior of tick auto-placement.

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

fig, ax = plt.subplots()
dots = np.linspace(0.3, 1.2, 10)
X, Y = np.meshgrid(dots, dots)
x, y = X.ravel(), Y.ravel()
ax.scatter(x, y, c=x+y)
plt.show()

Scatter plot with round_numbers autolimit_mode

In this step, we will switch axes.autolimit_mode to 'round_numbers' and create a scatter plot to keep ticks at round numbers and also have ticks at the edges.

plt.rcParams['axes.autolimit_mode'] = 'round_numbers'

fig, ax = plt.subplots()
ax.scatter(x, y, c=x+y)
plt.show()

Scatter plot with additional margin

In this step, we will set an additional margin around the data using .Axes.set_xmargin / .Axes.set_ymargin while the round numbers autolimit_mode is still respected.

fig, ax = plt.subplots()
ax.scatter(x, y, c=x+y)
ax.set_xmargin(0.8)
plt.show()

Summary

In this lab, we learned how to set the behavior of tick auto-placement in Matplotlib by switching axes.autolimit_mode to 'round_numbers'. We also learned how to set an additional margin around the data while the round numbers autolimit_mode is still respected. These techniques can be used to customize the tick positions on the axis and improve the readability of the plot.