Annotate With Units

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial will guide you through the process of creating text and arrow annotations using a centimeter-scale plot in Python 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 matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlotCustomizationGroup -.-> matplotlib/text_annotations("`Text Annotations`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48546{{"`Annotate With Units`"}} matplotlib/figures_axes -.-> lab-48546{{"`Annotate With Units`"}} matplotlib/text_annotations -.-> lab-48546{{"`Annotate With Units`"}} python/variables_data_types -.-> lab-48546{{"`Annotate With Units`"}} python/lists -.-> lab-48546{{"`Annotate With Units`"}} python/tuples -.-> lab-48546{{"`Annotate With Units`"}} python/importing_modules -.-> lab-48546{{"`Annotate With Units`"}} python/using_packages -.-> lab-48546{{"`Annotate With Units`"}} python/data_collections -.-> lab-48546{{"`Annotate With Units`"}} python/data_visualization -.-> lab-48546{{"`Annotate With Units`"}} python/build_in_functions -.-> lab-48546{{"`Annotate With Units`"}} end

Import necessary libraries and define units

In this step, we will import the necessary libraries and define the unit of measurement that we will be using for our plot.

from basic_units import cm
import matplotlib.pyplot as plt

Create a plot

In this step, we will create a plot using the subplots() function and store it in the fig and ax variables.

fig, ax = plt.subplots()

Add text annotation

In this step, we will add a text annotation to the plot using the annotate() function. We will provide the position of the annotation and the text to be displayed.

ax.annotate("Note 01", [0.5*cm, 0.5*cm])

Add arrow annotation with unitized xy and text

In this step, we will add an arrow annotation to the plot using the annotate() function. We will provide the position of the arrow, the text to be displayed, and the arrow properties. We will also specify the units of measurement for the position and text.

ax.annotate('local max', xy=(3*cm, 1*cm), xycoords='data',
            xytext=(0.8*cm, 0.95*cm), textcoords='data',
            arrowprops=dict(facecolor='black', shrink=0.05),
            horizontalalignment='right', verticalalignment='top')

Add arrow annotation with mixed units

In this step, we will add another arrow annotation to the plot using the annotate() function. We will provide the position of the arrow, the text to be displayed, and the arrow properties. We will also mix units of measurement for the position and use the axes fraction for the text.

ax.annotate('local max', xy=(3*cm, 1*cm), xycoords='data',
            xytext=(0.8, 0.95), textcoords='axes fraction',
            arrowprops=dict(facecolor='black', shrink=0.05),
            horizontalalignment='right', verticalalignment='top')

Set plot limits and display the plot

In this step, we will set the limits of the plot and display it using the set_xlim(), set_ylim(), and show() functions.

ax.set_xlim(0*cm, 4*cm)
ax.set_ylim(0*cm, 4*cm)
plt.show()

Summary

This tutorial demonstrated how to create text and arrow annotations using a centimeter-scale plot in Python Matplotlib. We imported necessary libraries, defined units, created a plot, added text and arrow annotations, set plot limits, and displayed the plot.

Other Python Tutorials you may like