Search code examples
pythonimagematplotlibwatermark

How do I put a watermark behind plotted data using matplotlib


I found this tutorial on how to do a watermark but I cannot figure out how to put it behind my plotted data. https://www.tutorialspoint.com/how-to-plot-a-watermark-image-in-matplotlib

Changing zorder has no impact because I think it is being drawn on the entire figure. I would like to have a subdued logo behind my data which is always centered in the figure so I don't really want to plot an image as a data point because then it would move as it is panned/zoomed.


Solution

  • Setting the zorder to a negative value works for me. However, you also need to make the facecolor of the axes transparent:

    import numpy as np
    import matplotlib.cbook as cbook
    import matplotlib.image as image
    import matplotlib.pyplot as plt
    
    with cbook.get_sample_data('logo2.png') as file:
       im = image.imread(file)
    
    fig, ax = plt.subplots()
    
    fig.figimage(im, 10, 10, zorder=-1, alpha=.5)
    ax.plot(np.sin(10 * np.linspace(0, 1)), '-o', ms=20,
             alpha=0.7, mfc='orange')
    ax.set_facecolor('none')
    
    plt.show()
    

    enter image description here