Simple Anchored Artists

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to use the anchored helper classes found in Matplotlib's offsetbox and mpl_toolkits.axes_grid1. You will create a figure that contains text boxes, a circle, and a size bar using these classes.

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`"]) 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`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") 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-48927{{"`Simple Anchored Artists`"}} matplotlib/figures_axes -.-> lab-48927{{"`Simple Anchored Artists`"}} python/variables_data_types -.-> lab-48927{{"`Simple Anchored Artists`"}} python/booleans -.-> lab-48927{{"`Simple Anchored Artists`"}} python/tuples -.-> lab-48927{{"`Simple Anchored Artists`"}} python/sets -.-> lab-48927{{"`Simple Anchored Artists`"}} python/importing_modules -.-> lab-48927{{"`Simple Anchored Artists`"}} python/using_packages -.-> lab-48927{{"`Simple Anchored Artists`"}} python/data_collections -.-> lab-48927{{"`Simple Anchored Artists`"}} python/data_visualization -.-> lab-48927{{"`Simple Anchored Artists`"}} python/build_in_functions -.-> lab-48927{{"`Simple Anchored Artists`"}} end

Import Libraries

To get started, you will need to import Matplotlib and the necessary modules.

import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText
from matplotlib.patches import Circle
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDrawingArea, AnchoredSizeBar

Create the Figure

Create a figure and axis object using Matplotlib's subplots() function.

fig, ax = plt.subplots()
ax.set_aspect(1.)

Add Text Boxes

Add two text boxes to the figure, anchored by different corners to the upper-left corner of the figure.

at = AnchoredText("Figure 1a",
                  loc='upper left', prop=dict(size=8), frameon=True,
                  )
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)

at2 = AnchoredText("Figure 1(b)",
                   loc='lower left', prop=dict(size=8), frameon=True,
                   bbox_to_anchor=(0., 1.),
                   bbox_transform=ax.transAxes
                   )
at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at2)

Draw a Circle

Draw a circle in axis coordinates.

ada = AnchoredDrawingArea(20, 20, 0, 0,
                          loc='upper right', pad=0., frameon=False)
p = Circle((10, 10), 10)
ada.da.add_artist(p)
ax.add_artist(ada)

Add a Size Bar

Draw a horizontal bar with a length of 0.1 in data coordinates, with a fixed label underneath.

asb = AnchoredSizeBar(ax.transData,
                      0.1,
                      r"1$^{\prime}$",
                      loc='lower center',
                      pad=0.1, borderpad=0.5, sep=5,
                      frameon=False)
ax.add_artist(asb)

Display the Figure

Display the figure using Matplotlib's show() function.

plt.show()

Summary

In this lab, you learned how to use the anchored helper classes found in Matplotlib's offsetbox and mpl_toolkits.axes_grid1 to create a figure with text boxes, a circle, and a size bar. You can use these classes to add informative annotations and graphics to your figures.

Other Python Tutorials you may like