Annotated Matplotlib Plots in Python

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 a plot with annotations using Matplotlib in Python. You will learn how to connect two points with an arrow, add an ellipse to the plot, and customize the arrow style and ellipse properties.

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-48542{{"`Annotated Matplotlib Plots in Python`"}} matplotlib/figures_axes -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} matplotlib/line_plots -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} matplotlib/text_annotations -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} python/variables_data_types -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} python/lists -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} python/tuples -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} python/importing_modules -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} python/data_collections -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} python/data_visualization -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} python/build_in_functions -.-> lab-48542{{"`Annotated Matplotlib Plots in Python`"}} end

Set up the plot

First, we need to set up the plot with two subplots. We will use the subplots function to create a 2x2 grid of subplots, and then we will define the x and y coordinates of two points.

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

fig, axs = plt.subplots(2, 2)
x1, y1 = 0.3, 0.3
x2, y2 = 0.7, 0.7

Connect two points with an arrow

In this step, we will connect the two points with an arrow. We will use the annotate function to create the arrow, and we will customize the arrow style and color.

ax = axs.flat[0]
ax.plot([x1, x2], [y1, y2], ".")
ax.annotate("",
            xy=(x1, y1), xycoords='data',
            xytext=(x2, y2), textcoords='data',
            arrowprops=dict(arrowstyle="-",
                            color="0.5",
                            connectionstyle="arc3,rad=0.3",
                            ),
            )

Add an ellipse to the plot

In this step, we will add an ellipse to the plot. We will use the Ellipse function to create the ellipse, and we will customize the ellipse properties such as the position, width, height, and angle.

ax = axs.flat[1]
ax.plot([x1, x2], [y1, y2], ".")
el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.2)
ax.add_artist(el)

Customize the arrow to connect to the ellipse

In this step, we will customize the arrow to connect to the ellipse. We will use the arrowprops parameter to specify that the arrow should connect to the ellipse, and we will also customize the arrow style and color.

ax = axs.flat[2]
ax.plot([x1, x2], [y1, y2], ".")
el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.2)
ax.add_artist(el)
ax.annotate("",
            xy=(x1, y1), xycoords='data',
            xytext=(x2, y2), textcoords='data',
            arrowprops=dict(arrowstyle="-",
                            color="0.5",
                            patchB=el,
                            connectionstyle="arc3,rad=0.3",
                            ),
            )

Customize the arrow to shrink to the ellipse

In this step, we will customize the arrow to shrink to the ellipse. We will use the shrinkB parameter to specify the amount by which the arrow should shrink towards the ellipse.

ax = axs.flat[3]
ax.plot([x1, x2], [y1, y2], ".")
el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.2)
ax.add_artist(el)
ax.annotate("",
            xy=(x1, y1), xycoords='data',
            xytext=(x2, y2), textcoords='data',
            arrowprops=dict(arrowstyle="fancy",
                            color="0.5",
                            patchB=el,
                            shrinkB=5,
                            connectionstyle="arc3,rad=0.3",
                            ),
            )

Summary

In this tutorial, you learned how to create a plot with annotations using Matplotlib in Python. You learned how to connect two points with an arrow, add an ellipse to the plot, and customize the arrow style and ellipse properties. These skills will be useful in creating informative and visually appealing plots for data visualization.

Other Python Tutorials you may like