Matplotlib Connection Patch Visualization

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a powerful data visualization library in Python. It provides a variety of visualization tools to create stunning graphics and charts. In this lab, we will learn about ConnectionPatch, which is used to draw a line between points defined in different coordinate systems and/or axes.

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`"]) 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/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") 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/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} matplotlib/importing_matplotlib -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} matplotlib/figures_axes -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} matplotlib/line_plots -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} python/lists -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} python/tuples -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} python/importing_modules -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} python/using_packages -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} python/data_visualization -.-> lab-48616{{"`Matplotlib Connection Patch Visualization`"}} end

Import the necessary libraries

Before we start, let's import the necessary libraries.

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch

Create the plot

Next, let's create the plot with two subplots.

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))

Draw a simple arrow

Now, let's draw a simple arrow between two points in axes coordinates within a single axes.

xyA = (0.2, 0.2)
xyB = (0.8, 0.8)
coordsA = "data"
coordsB = "data"
con = ConnectionPatch(xyA, xyB, coordsA, coordsB,
                      arrowstyle="-|>", shrinkA=5, shrinkB=5,
                      mutation_scale=20, fc="w")
ax1.plot([xyA[0], xyB[0]], [xyA[1], xyB[1]], "o")
ax1.add_artist(con)

Draw an arrow between different axes

Let's draw an arrow between the same point in data coordinates, but in different axes.

xy = (0.3, 0.2)
con = ConnectionPatch(
    xyA=xy, coordsA=ax2.transData,
    xyB=xy, coordsB=ax1.transData,
    arrowstyle="->", shrinkB=5)
fig.add_artist(con)

Draw a line between the different points

Finally, let's draw a line between the different points, defined in different coordinate systems.

con = ConnectionPatch(
    ## in axes coordinates
    xyA=(0.6, 1.0), coordsA=ax2.transAxes,
    ## x in axes coordinates, y in data coordinates
    xyB=(0.0, 0.2), coordsB=ax2.get_yaxis_transform(),
    arrowstyle="-")
ax2.add_artist(con)

Summary

In this lab, we learned about ConnectionPatch, which is used to draw a line between points defined in different coordinate systems and/or axes. We also learned how to draw a simple arrow and a line between different points in a plot.

Other Python Tutorials you may like