简体   繁体   English

Python 3 matplotlib 添加多刻度轴水印

[英]Python 3 matplotlib add a watermark with multiple scale axis

In python 3, i am trying to add watermark with multiple scale axis in the following pandas data frame在 python 3 中,我试图在以下 pandas 数据帧中添加具有多个比例轴的水印

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')

在此处输入图像描述

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.在这种情况下,徽标不会出现,并且红色轴完全混乱。

在此处输入图像描述

There are some other solutions but none of them seems to work.还有其他一些解决方案,但它们似乎都不起作用。

Scale image in matplotlib without changing the axis 在 matplotlib 中缩放图像而不改变轴

Matplotlib automate placement of watermark Matplotlib 自动放置水印

Scale image in matplotlib without changing the axis 在 matplotlib 中缩放图像而不改变轴

Any thoughts?有什么想法吗? Thank you!谢谢!

Instead of ax.imshow(), you can use fig.figimage() as shown below and described here .您可以使用 fig.figimage() 代替 ax.imshow(),如下所示并在此处描述。 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:使用您提供的部分数据,这是保存的图像:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM