Add Watermark to Matplotlib Plot

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to add a text watermark effect to a matplotlib plot.

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`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) 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`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") 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/grid_config("`Grid Configuration`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") 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-49030{{"`Add Watermark to Matplotlib Plot`"}} python/with_statement -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} matplotlib/importing_matplotlib -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} matplotlib/figures_axes -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} matplotlib/line_plots -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} matplotlib/grid_config -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} python/for_loops -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} python/tuples -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} python/importing_modules -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} python/standard_libraries -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} python/math_random -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} python/numerical_computing -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} python/data_visualization -.-> lab-49030{{"`Add Watermark to Matplotlib Plot`"}} end

Import Necessary Libraries

Before we start, we need to import the necessary libraries. In this lab, we will be using numpy and matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Generate Data

Let's generate some random data to plot.

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

fig, ax = plt.subplots()
ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange')
ax.grid()

Add Text Watermark

To add a text watermark, we can use the text() method of the Figure object. We need to provide the position, text, and other properties like font size, color, and transparency.

ax.text(0.5, 0.5, 'created with matplotlib', transform=ax.transAxes,
        fontsize=40, color='gray', alpha=0.5,
        ha='center', va='center', rotation=30)

Display the Plot

Finally, we can display the plot using the show() method.

plt.show()

Summary

In this lab, we learned how to add a text watermark effect to a matplotlib plot. We imported the necessary libraries, generated random data, added the text watermark using the text() method, and displayed the plot using the show() method.

Other Python Tutorials you may like