Matplotlib Anchored Direction Arrow

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create anchored direction arrows in Matplotlib. Anchored direction arrows are arrows that point in a specific direction and are anchored to a plot. These arrows are useful for indicating specific directions or orientations in a plot. We will learn how to create simple arrows as well as high contrast and rotated arrows.

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/ControlFlowGroup(["`Control Flow`"]) 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/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") 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/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} matplotlib/importing_matplotlib -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} matplotlib/figures_axes -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} matplotlib/heatmaps -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/for_loops -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/tuples -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/dictionaries -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/importing_modules -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/using_packages -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/standard_libraries -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/math_random -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/numerical_computing -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} python/data_visualization -.-> lab-48658{{"`Matplotlib Anchored Direction Arrow`"}} end

Import necessary libraries

First, we need to import necessary libraries such as Matplotlib, NumPy, Matplotlib font manager, and AnchoredDirectionArrows from mpl_toolkits.axes_grid1. We will use these libraries to create anchored direction arrows.

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.font_manager as fm
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDirectionArrows

Create a plot

Next, we will create a simple plot using NumPy. This plot will serve as a background for the anchored direction arrows.

## Fixing random state for reproducibility
np.random.seed(19680801)

fig, ax = plt.subplots()
ax.imshow(np.random.random((10, 10)))

Create a simple arrow

Now, we will create a simple anchored direction arrow using the AnchoredDirectionArrows class. This arrow will indicate the X and Y directions in the plot.

## Simple example
simple_arrow = AnchoredDirectionArrows(ax.transAxes, 'X', 'Y')
ax.add_artist(simple_arrow)

Create a high contrast arrow

Next, we will create a high contrast anchored direction arrow. This arrow will have a white outline and a black fill.

high_contrast_part_1 = AnchoredDirectionArrows(
                            ax.transAxes,
                            '111', r'11$\overline{2}$',
                            loc='upper right',
                            arrow_props={'ec': 'w', 'fc': 'none', 'alpha': 1,
                                         'lw': 2}
                            )
ax.add_artist(high_contrast_part_1)

high_contrast_part_2 = AnchoredDirectionArrows(
                            ax.transAxes,
                            '111', r'11$\overline{2}$',
                            loc='upper right',
                            arrow_props={'ec': 'none', 'fc': 'k'},
                            text_props={'ec': 'w', 'fc': 'k', 'lw': 0.4}
                            )
ax.add_artist(high_contrast_part_2)

Create a rotated arrow

In this step, we will create a rotated anchored direction arrow. This arrow will be rotated by 30 degrees and will have a serif font.

fontprops = fm.FontProperties(family='serif')

rotated_arrow = AnchoredDirectionArrows(
                    ax.transAxes,
                    '30', '120',
                    loc='center',
                    color='w',
                    angle=30,
                    fontproperties=fontprops
                    )
ax.add_artist(rotated_arrow)

Alter the arrow directions

In this step, we will create three anchored direction arrows that point in different directions. These arrows will have different lengths and aspect ratios.

a1 = AnchoredDirectionArrows(
        ax.transAxes, 'A', 'B', loc='lower center',
        length=-0.15,
        sep_x=0.03, sep_y=0.03,
        color='r'
    )
ax.add_artist(a1)

a2 = AnchoredDirectionArrows(
        ax.transAxes, 'A', ' B', loc='lower left',
        aspect_ratio=-1,
        sep_x=0.01, sep_y=-0.02,
        color='orange'
        )
ax.add_artist(a2)


a3 = AnchoredDirectionArrows(
        ax.transAxes, ' A', 'B', loc='lower right',
        length=-0.15,
        aspect_ratio=-1,
        sep_y=-0.1, sep_x=0.04,
        color='cyan'
        )
ax.add_artist(a3)

Display the plot

Finally, we will display the plot with all the anchored direction arrows.

plt.show()

Summary

In this lab, we learned how to create anchored direction arrows in Matplotlib. We created simple arrows as well as high contrast and rotated arrows. We also learned how to alter the arrow directions and aspect ratios. Anchored direction arrows are useful for indicating specific directions or orientations in a plot.

Other Python Tutorials you may like