0

In python 3, i am trying to add watermark with multiple scale axis in the following pandas data frame

index,as_of_date,Total_10bn,close
0,2020-08-05,620.55975367473,332.11
1,2020-08-12,621.9414641848599,337.44
2,2020-08-19,628.88298116372,337.23
3,2020-08-26,627.26943375402,347.57
4,2020-09-02,630.01703674403,357.7
5,2020-09-09,630.70673674269,339.79
6,2020-09-16,637.50390815142,338.82

I can make the multiple scale works

df_soma_spy=pd.read_csv('df_soma_spy.csv')

print(df_soma_spy)

# create figure and axis objects with subplots()

fig,ax = plt.subplots()

plt.xticks(rotation=90)

ax.plot(df_soma_spy.as_of_date, df_soma_spy.Total_10bn, color="red") ## , marker="o"

# set x-axis label

ax.set_xlabel("Date", fontsize=12)

# set y-axis label

ax.set_ylabel("Fed SOMA ($10bn)",color="red",fontsize=14)

plt.grid(True, axis='both', which='both')

# twin object for two different y-axis on the sample plot

ax2=ax.twinx()

# make a plot with different y-axis using second axis object

ax2.plot(df_soma_spy.as_of_date, df_soma_spy["close"], color="black") ## , marker="o"

ax2.set_ylabel("$SPY Price",color="black",fontsize=14)


plt.title('Federal Reserves SOMA Total vs $SPY')

plt.show()

# save the plot as a file
fig.savefig('soma_spy.png',
            format='jpeg',
            dpi=300,
            bbox_inches='tight')

enter image description here

Now I am trying to add a logo behind the picture. But no matter how I try, it will mess up one of the axis.

For example

import matplotlib.image as image

im = image.imread('xxx.png')

myaximage = ax.imshow(im, aspect='auto', extent=(0.1,0.1,0.1,0.1), alpha=0.5, zorder=-1)

In this case, the logo doesn't show up and the red axis is totally messed up.

enter image description here

There are some other solutions but none of them seems to work.

Scale image in matplotlib without changing the axis

Matplotlib automate placement of watermark

Scale image in matplotlib without changing the axis

Any thoughts? Thank you!

1 Answer 1

1

Instead of ax.imshow(), you can use fig.figimage() as shown below and described here. Just insert the following two lines in your code:

logo = image.imread(fname='logo.png')
fig.figimage(logo,alpha= 0.1)

Using the partial data you provided, here is the saved image:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

perfect and simple solution! I am trying to move the water mark to upper left. Playing with several parameters but none of them seem to work. Any tip? Thank you! matplotlib.org/3.1.1/api/_as_gen/…
wow you are a neuron surgeon @pakpe ! so talented!
Thanks. You can play around with the figimage arguments, but they are cumbersome and the offset only shows on the displayed image and not the saved image. Probably the best way to achieve a precise result is to pre-process your image. For instance, use Photoshop to position the water mark on the desired location on a canvas that is the same size and resolution as your saved figure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.