Simple Matplotlib Annotation

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you through the process of adding annotations to your Matplotlib plots. Annotations help to highlight specific data points or provide additional information to the viewer. The annotations can include text, arrows, and shapes. You will learn how to add annotations to your plot, customize them, and position them.

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/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/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") 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/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-48929{{"`Simple Matplotlib Annotation`"}} matplotlib/figures_axes -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} matplotlib/line_plots -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} matplotlib/text_annotations -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} python/variables_data_types -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} python/lists -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} python/tuples -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} python/importing_modules -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} python/data_collections -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} python/data_visualization -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} python/build_in_functions -.-> lab-48929{{"`Simple Matplotlib Annotation`"}} end

Import Matplotlib

Before we can begin working with Matplotlib, we need to import it. The following code will import Matplotlib and allow us to use its plotting functions.

import matplotlib.pyplot as plt

Create a Plot

We will now create a plot to annotate. The following code will create a plot with two data points.

fig, ax = plt.subplots()
x = [1, 2]
y = [3, 4]
ax.plot(x, y, "o")

Add Text Annotation

We will now add a text annotation to the plot. The following code will add the text "Data Point 1" at the first data point.

ax.annotate("Data Point 1", xy=(1, 3), xytext=(1.5, 3.5),
            arrowprops=dict(facecolor="black", shrink=0.05))

Add Arrow Annotation

We will now add an arrow annotation to the plot. The following code will add an arrow from the first data point to the second data point.

ax.annotate("", xy=(1, 3), xytext=(2, 4),
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3"))

Add Shape Annotation

We will now add a shape annotation to the plot. The following code will add a rectangle around the second data point.

bbox = dict(boxstyle="round", fc="0.8")
ax.annotate("Data Point 2", xy=(2, 4), xytext=(2.5, 4.5),
            bbox=bbox,
            arrowprops=dict(facecolor="black", shrink=0.05))

Customize Annotations

We can customize the annotations by changing the font size, font color, and arrow style. The following code will change the font size, font color, and arrow style of the text annotation.

ax.annotate("Data Point 1", xy=(1, 3), xytext=(1.5, 3.5),
            arrowprops=dict(facecolor="black", shrink=0.05, arrowstyle="->"),
            fontsize=12, color="red")

Position Annotations

We can position the annotations using different coordinate systems. The following code will position the text annotation using data coordinates and the arrow annotation using figure coordinates.

ax.annotate("Data Point 1", xy=(1, 3), xytext=(1.5, 3.5),
            arrowprops=dict(facecolor="black", shrink=0.05),
            xycoords="data", textcoords="data")
ax.annotate("", xy=(1, 3), xytext=(0.5, 0.5),
            arrowprops=dict(facecolor="black", shrink=0.05),
            xycoords="data", textcoords="figure fraction")

Summary

In this lab, you learned how to add annotations to your Matplotlib plots. You learned how to add text, arrow, and shape annotations, customize them, and position them. Annotations help to highlight specific data points or provide additional information to the viewer.

Other Python Tutorials you may like