Annotate Figures with AnnotationBbox

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to use AnnotationBbox in Matplotlib to annotate figures using text, shapes, and images. AnnotationBbox is a more fine-grained control method than Axes.annotate. We will go through three different OffsetBoxes: TextArea, DrawingArea, and OffsetImage.

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/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`"]) 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/line_plots("`Line Plots`") 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/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/with_statement -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} matplotlib/importing_matplotlib -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} matplotlib/figures_axes -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} matplotlib/line_plots -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/variables_data_types -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/lists -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/tuples -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/importing_modules -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/using_packages -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/standard_libraries -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/data_collections -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/numerical_computing -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/data_visualization -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} python/build_in_functions -.-> lab-48659{{"`Annotate Figures with AnnotationBbox`"}} end

Plotting Points

To start, let's plot two points that we will annotate later.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

## Define a 1st position to annotate (display it with a marker)
xy1 = (0.5, 0.7)
ax.plot(xy1[0], xy1[1], ".r")

## Define a 2nd position to annotate (don't display with a marker this time)
xy2 = [0.3, 0.55]

## Fix the display limits to see everything
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)

plt.show()

Annotating with TextArea

Now let's annotate the first point using a TextArea.

from matplotlib.offsetbox import AnnotationBbox, TextArea

## Annotate the 1st position with a text box ('Test 1')
offsetbox = TextArea("Test 1")

ab = AnnotationBbox(offsetbox, xy1,
                    xybox=(-20, 40),
                    xycoords='data',
                    boxcoords="offset points",
                    arrowprops=dict(arrowstyle="->"),
                    bboxprops=dict(boxstyle="sawtooth"))

ax.add_artist(ab)

plt.show()

Annotating with DrawingArea

Next, let's annotate the second point with a circle patch using DrawingArea.

from matplotlib.offsetbox import DrawingArea
from matplotlib.patches import Circle

## Annotate the 2nd position with a circle patch
da = DrawingArea(20, 20, 0, 0)
p = Circle((10, 10), 10)
da.add_artist(p)

ab = AnnotationBbox(da, xy2,
                    xybox=(1., xy2[1]),
                    xycoords='data',
                    boxcoords=("axes fraction", "data"),
                    box_alignment=(0.2, 0.5),
                    arrowprops=dict(arrowstyle="->"),
                    bboxprops=dict(alpha=0.5))

ax.add_artist(ab)

plt.show()

Annotating with OffsetImage

Finally, let's annotate the second point with an OffsetImage using an image of Grace Hopper.

from matplotlib.cbook import get_sample_data
from matplotlib.offsetbox import OffsetImage

## Annotate the 2nd position with an image (a generated array of pixels)
arr = np.arange(100).reshape((10, 10))
im = OffsetImage(arr, zoom=2)
im.image.axes = ax

ab = AnnotationBbox(im, xy2,
                    xybox=(-50., 50.),
                    xycoords='data',
                    boxcoords="offset points",
                    pad=0.3,
                    arrowprops=dict(arrowstyle="->"))

ax.add_artist(ab)

## Annotate the 2nd position with another image (a Grace Hopper portrait)
with get_sample_data("grace_hopper.jpg") as file:
    arr_img = plt.imread(file)

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax

ab = AnnotationBbox(imagebox, xy2,
                    xybox=(120., -80.),
                    xycoords='data',
                    boxcoords="offset points",
                    pad=0.5,
                    arrowprops=dict(
                        arrowstyle="->",
                        connectionstyle="angle,angleA=0,angleB=90,rad=3")
                    )

ax.add_artist(ab)

plt.show()

Summary

In this lab, we have learned how to use AnnotationBbox in Matplotlib to annotate figures using text, shapes, and images. We have gone through three different OffsetBoxes: TextArea, DrawingArea, and OffsetImage. By using AnnotationBbox, we have more fine-grained control over annotations than using Axes.annotate.

Other Python Tutorials you may like