Adding Hyperlinks to Plots

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

Matplotlib is a Python data visualization library that enables users to create a wide range of static, animated, and interactive visualizations in Python. In this lab, you will learn how to add hyperlinks to your plots using Matplotlib. By the end of this lab, you will be able to add hyperlinks to scatter plots and images in Matplotlib.

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`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/BasicConceptsGroup -.-> matplotlib/saving_figures("`Saving Figures to File`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} matplotlib/figures_axes -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} matplotlib/saving_figures -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} matplotlib/scatter_plots -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} matplotlib/heatmaps -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} python/lists -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} python/tuples -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} python/importing_modules -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} python/numerical_computing -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} python/data_visualization -.-> lab-48775{{"`Adding Hyperlinks to Plots`"}} end

Import Required Libraries

Before we get started, we need to import the necessary libraries for this lab. We will be using matplotlib.pyplot, numpy, and matplotlib.cm.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

In this step, we will create a scatter plot and add hyperlinks to the markers. Here's the code to create the scatter plot:

fig = plt.figure()
s = plt.scatter([1, 2, 3], [4, 5, 6])

To add hyperlinks, we need to use the set_urls() method of the scatter plot object. This method takes a list of URLs as its argument. Here's the updated code:

s.set_urls(['https://www.bbc.com/news', 'https://www.google.com/', None])

The first two markers will have hyperlinks to https://www.bbc.com/news and https://www.google.com/, respectively. The third marker will not have a hyperlink. Finally, we can save the plot as an SVG file using fig.savefig():

fig.savefig('scatter.svg')

In this step, we will create an image and add a hyperlink to it. Here's the code to create the image:

fig = plt.figure()
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
                origin='lower', extent=[-3, 3, -3, 3])

To add a hyperlink to the image, we need to use the set_url() method of the image object. This method takes a URL as its argument. Here's the updated code:

im.set_url('https://www.google.com/')

The image will have a hyperlink to https://www.google.com/. Finally, we can save the plot as an SVG file using fig.savefig():

fig.savefig('image.svg')

Run the Code

Run the code in your Python environment. Two SVG files should be generated: scatter.svg and image.svg. Open these files and hover your mouse over the markers in the scatter plot and the image. You should see a tooltip with the hyperlink.

Summary

In this lab, you learned how to add hyperlinks to scatter plots and images in Matplotlib. You used the set_urls() method for scatter plots and the set_url() method for images. Adding hyperlinks to your plots can be useful when you want to provide additional information or resources to your audience.

Other Matplotlib Tutorials you may like