Matplotlib Figure Image

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

In this lab, you will learn how to use Matplotlib's figimage function to place images directly in a figure, without the need for Axes objects. This can be useful when you want to include images that are not part of your plot, such as a logo or watermark.

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/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`") 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-48728{{"`Matplotlib Figure Image`"}} matplotlib/figures_axes -.-> lab-48728{{"`Matplotlib Figure Image`"}} python/lists -.-> lab-48728{{"`Matplotlib Figure Image`"}} python/tuples -.-> lab-48728{{"`Matplotlib Figure Image`"}} python/importing_modules -.-> lab-48728{{"`Matplotlib Figure Image`"}} python/numerical_computing -.-> lab-48728{{"`Matplotlib Figure Image`"}} python/data_visualization -.-> lab-48728{{"`Matplotlib Figure Image`"}} end

Importing the necessary libraries

First, we need to import the necessary libraries, which are matplotlib.pyplot and numpy. We will use numpy to create an array of random values that we will use as our image.

import matplotlib.pyplot as plt
import numpy as np

Creating the figure and image

Next, we create the figure and the image that we want to place in it. In this example, we create a 100x100 array of random values and set the values in the right half of the image to 1. We then create two separate instances of the image, each with a different position and opacity.

fig = plt.figure()
Z = np.arange(10000).reshape((100, 100))
Z[:, 50:] = 1

im1 = fig.figimage(Z, xo=50, yo=0, origin='lower')
im2 = fig.figimage(Z, xo=100, yo=100, alpha=.8, origin='lower')

Showing the figure

Finally, we display the figure with the images using the show() function.

plt.show()

Summary

In this lab, we learned how to use Matplotlib's figimage function to place images directly in a figure. This can be useful when you want to include images that are not part of your plot. By following the steps outlined in this lab, you should now have a better understanding of how to create and display images in Matplotlib figures.

Other Matplotlib Tutorials you may like