Updated On : Mar-30,2023 Time Investment : ~15 mins

How to Add Watermarks to Matplotlib Charts?

Data visualization is an essential aspect of data analysis that helps us to understand and communicate data effectively. Matplotlib is a powerful Python library widely used for creating high-quality data visualizations. With Matplotlib, you can create a wide range of charts and graphs, including scatter plots, line charts, bar charts, and histograms, to name a few. However, when you are sharing your data visualizations publicly, it is essential to ensure that your work is appropriately attributed to you or your organization. Watermarks are an excellent way to do just that.

A watermark is a visible image or text that is superimposed on a chart or graphic, usually to indicate the source or ownership of the work. Watermarks can also be used to add a professional touch to your charts and make them more visually appealing. In the context of data visualization, watermarks can be used to add logos, copyright notices, or other branding elements to charts and graphs.

Adding watermarks to Matplotlib charts might seem like a daunting task, especially if you are new to the library or Python programming. However, with a few simple steps, you can add watermarks to your Matplotlib charts and customize them to fit your needs.

What Can You Learn From This Article?

In this article, we will explain different methods to add watermarks to Matplotlib charts and discuss the various customization options available. We will cover how to add image watermarks and text watermarks. We will also discuss how to adjust the position, size, and transparency of the watermarks to ensure that they fit your needs.

The tutorial assumes that you have a basic understanding of Python programming and the Matplotlib library. If you are new to Python or Matplotlib, we recommend that you check out some introductory tutorials to get started. By the end of this tutorial, you will have a clear understanding of how to add watermarks to Matplotlib charts and how to customize them to fit your needs.

Video Tutorial

Please feel free to check below video tutorial if feel comfortable learning through videos.


First, we have imported matplotlib and printed the version that we have used in our tutorial.

import matplotlib

print("Matplotlib Version : {}".format(matplotlib.__version__))
Matplotlib Version : 3.5.3

Load Dataset

In this section, we have loaded an Apple OHLC dataset as pandas dataframe. We have downloaded the dataset from Yahoo finance as a CSV file.

It has information about apple stock prices from 2019-2020.

import pandas as pd

apple_df = pd.read_csv("~/datasets/AAPL.csv", index_col=0, parse_dates=True)

apple_df.head()
Open High Low Close Adj Close Volume
Date
2019-04-05 196.449997 197.100006 195.929993 197.000000 194.454758 18526600
2019-04-08 196.419998 200.229996 196.339996 200.100006 197.514709 25881700
2019-04-09 200.320007 202.850006 199.229996 199.500000 196.922470 35768200
2019-04-10 198.679993 200.740005 198.179993 200.619995 198.027985 21695300
2019-04-11 200.850006 201.000000 198.440002 198.949997 196.379578 20900800

Simple Line Chart

Below, we have created a simple line chart using our apple ohlc dataset. We'll be adding watermarks to this chart.

The code creates a new figure object with a size of 15 inches by 8 inches using the figsize parameter.

The third line plots the closing prices of Apple stock by passing in the index (which is the date) of the Apple DataFrame (apple_df) on the x-axis and the "Close" column of the same DataFrame on the y-axis. The line's color is set to "dodgerblue" and the line width is set to 2.0.

The fourth line sets the y-axis tick labels using the yticks method. It specifies a range of values from 160 to 340 with an interval of 20 and then creates a list of tick labels with a dollar sign appended to each value using a list comprehension.

The fifth and sixth line sets the label for the x-axis and y-axis respectively.

The seventh line sets the title of the chart to "Apple Close Prices 2019-2020" on the left side of the chart with a padding of 10 pixels. The font size is set to 20 and the font-weight is set to "bold".

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(15,8))

plt.plot(apple_df.index, apple_df.Close, color="dodgerblue", linewidth=2.0);

plt.yticks(range(160,341,20), ["{} $".format(val) for val in range(160,341,20)])

plt.xlabel("Date", fontdict=dict(fontsize=16, fontweight="bold"))
plt.ylabel("Close Price ($)", fontdict=dict(fontsize=16, fontweight="bold"))
plt.title("Apple Close Prices 2019-2020", loc="left", pad=10, fontdict=dict(fontsize=20, fontweight="bold"));

How to Add Watermarks to Matplotlib Charts?

1. Image as Watermark

In this section, we have explained how to add an image as a watermark to our chart.

The below code simply loads an image named cc.png in memory using image submodule of matplotlib. It's a small image.

from matplotlib import image

logo = image.imread("cc.png")

Below, we have added the image as a watermark in our chart. In order to add an image as a watermark, we have called figimage() method on figure object.

The first argument, logo, is the image to be added. The second and third arguments, 425 and 250, specify the position of the image on the figure. The position is given in pixels from the lower-left corner of the figure.

The last argument, alpha=0.5, specifies the transparency of the image. An alpha value of 1.0 means the image is fully opaque, while an alpha value of 0.0 means it is fully transparent. In this case, the alpha value is set to 0.5, which means the image will be partially transparent, allowing some of the plot to show through.

Overall, the code adds an image to a Matplotlib figure, positioned at (425, 250) with a transparency level of 0.5.

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(15,8))

plt.plot(apple_df.index, apple_df.Close, color="dodgerblue", linewidth=2.0);

fig.figimage(logo,425,250, alpha=0.5)

plt.yticks(range(160,341,20), ["{} $".format(val) for val in range(160,341,20)])

plt.xlabel("Date", fontdict=dict(fontsize=16, fontweight="bold"))
plt.ylabel("Close Price ($)", fontdict=dict(fontsize=16, fontweight="bold"))
plt.title("Apple Close Prices 2019-2020", loc="left", pad=10, fontdict=dict(fontsize=20, fontweight="bold"));

How to Add Watermarks to Matplotlib Charts?

Below, we have simply added a theme named fivethirtyeight to chart. If you want to learn how to add themes to your matplotlib charts then feel free to check our simple tutorial on the concept.

import matplotlib.pyplot as plt

with plt.style.context("fivethirtyeight"):
    fig = plt.figure(figsize=(15,8))

    plt.plot(apple_df.index, apple_df.Close, color="dodgerblue", linewidth=2.0);

    fig.figimage(logo,425,250, alpha=0.5)

    plt.yticks(range(160,341,20), ["{} $".format(val) for val in range(160,341,20)])

    plt.xlabel("Date", fontdict=dict(fontsize=16, fontweight="bold"))
    plt.ylabel("Close Price ($)", fontdict=dict(fontsize=16, fontweight="bold"))
    plt.title("Apple Close Prices 2019-2020", loc="left", pad=10, fontdict=dict(fontsize=20, fontweight="bold"));

How to Add Watermarks to Matplotlib Charts?

Below, we have created one more example demonstrating how to add an image watermark to a chart. The code is exactly the same as earlier with the only change being that we have used a different image for the watermark.

import matplotlib.pyplot as plt

logo = image.imread("cc2.jpg")

with plt.style.context("fivethirtyeight"):
    fig = plt.figure(figsize=(15,8))

    plt.plot(apple_df.index, apple_df.Close, color="dodgerblue", linewidth=2.0);

    fig.figimage(logo,425,250, alpha=0.5)

    plt.yticks(range(160,341,20), ["{} $".format(val) for val in range(160,341,20)])

    plt.xlabel("Date", fontdict=dict(fontsize=16, fontweight="bold"))
    plt.ylabel("Close Price ($)", fontdict=dict(fontsize=16, fontweight="bold"))
    plt.title("Apple Close Prices 2019-2020", loc="left", pad=10, fontdict=dict(fontsize=20, fontweight="bold"));

How to Add Watermarks to Matplotlib Charts?

2. Text as Watermark

In this section, we have explained how to add text as a watermark to the chart.

In order to add a text watermark, we have used text() method available from pyplot API of matplotlib.

The first argument date(2019,7,5) to text() method specifies the date location for the text label, which is July 5th, 2019. The second argument, 200, specifies the y-axis coordinate for the label. The third argument is the actual text string that will be displayed, which is "CoderzColumn".

The fontsize parameter sets the size of the text to 50 points. The fontweight parameter sets the weight of the font to "bold".

The alpha parameter specifies the transparency level of the text label, which is set to 0.3.

The rotation parameter rotates the text label by 30 degrees clockwise. This is optional and may be removed if not needed.

Overall, this one line of code adds a text label to a line plot with the text "CoderzColumn" displayed at a specific location on the plot, with a specified font size, weight, transparency level, and rotation. The rest of the code is same as in our previous examples.

import matplotlib.pyplot as plt
from datetime import date

with plt.style.context("fivethirtyeight"):
    fig = plt.figure(figsize=(15,8))

    plt.plot(apple_df.index, apple_df.Close, color="dodgerblue", linewidth=2.0);

    plt.text(date(2019,7,5), 200, "CoderzColumn", fontsize=50,  fontweight="bold", alpha=0.3, rotation=30)

    plt.yticks(range(160,341,20), ["{} $".format(val) for val in range(160,341,20)])

    plt.xlabel("Date", fontdict=dict(fontsize=16, fontweight="bold"))
    plt.ylabel("Close Price ($)", fontdict=dict(fontsize=16, fontweight="bold"))
    plt.title("Apple Close Prices 2019-2020", loc="left", pad=10, fontdict=dict(fontsize=20, fontweight="bold"));

How to Add Watermarks to Matplotlib Charts?

Sunny Solanki  Sunny Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.