Matplotlib 自动放置水印 [英] Matplotlib automate placement of watermark

查看:70
本文介绍了Matplotlib 自动放置水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数来自动在图形的右下方放置水印.到目前为止,这是我的功能.

I'm trying to write a function to automate the placement of watermark in the lower right of my figures. Here is my function so far.

from PIL import Image
import matplotlib.pyplot as plt

def watermark(fig, ax):

    """ Place watermark in bottom right of figure. """

    # Get the pixel dimensions of the figure
    width, height = fig.get_size_inches()*fig.dpi

    # Import logo and scale accordingly
    img = Image.open('logo.png')
    wm_width = int(width/4) # make the watermark 1/4 of the figure size
    scaling = (wm_width / float(img.size[0]))
    wm_height = int(float(img.size[1])*float(scaling))
    img = img.resize((wm_width, wm_height), Image.ANTIALIAS)

    # Place the watermark in the lower right of the figure
    xpos = ax.transAxes.transform((0.7,0))[0]
    ypos = ax.transAxes.transform((0.7,0))[1]
    plt.figimage(img, xpos, ypos, alpha=.25, zorder=1)

问题是,当我向任一轴添加标签时,水印的位置会发生变化.例如.添加 ax.set_xlabel('x-label',rotation = 45)会大大改变水印位置.这似乎是因为水印的位置是相对于整个图形(例如绘图和轴区域)而言的,但是函数 get_size_inches() 仅计算绘图区域(例如不包括轴区域).

The problem is that when I add a label to either axis the position of the water mark changes. E.g. adding ax.set_xlabel('x-label', rotation=45) changes the watermark position significantly. This seems to be the case because the placement of the watermark is relative to the whole figure (e.g. the plotting and axis area), however the function get_size_inches() only calculates the plotting area (e.g. not including the axis area).

总有办法获得整个图形的像素尺寸(例如,包括轴面积)或其他简便的解决方法.

Is there anyway to get the pixel dimensions of the entire figure (e.g. including axis area) or another easy workaround.

谢谢.

推荐答案

您可能想使用 AnchoredOffsetbox 在其中放置 OffsetImage .好处是您可以使用 loc = 4 参数将偏移"框放置在轴的右下角(就像在图例中一样).

You may want to use an AnchoredOffsetbox in which you place an OffsetImage. The advantage would be that you can use the loc=4 argument to place the Offsetbox in the lower right corner of the axes (just like in the case of a legend).

from PIL import Image
import matplotlib.pyplot as plt
from matplotlib.offsetbox import ( OffsetImage,AnchoredOffsetbox)


def watermark2(ax):
    img = Image.open('house.png')
    width, height = ax.figure.get_size_inches()*fig.dpi
    wm_width = int(width/4) # make the watermark 1/4 of the figure size
    scaling = (wm_width / float(img.size[0]))
    wm_height = int(float(img.size[1])*float(scaling))
    img = img.resize((wm_width, wm_height), Image.ANTIALIAS)

    imagebox = OffsetImage(img, zoom=1, alpha=0.2)
    imagebox.image.axes = ax

    ao = AnchoredOffsetbox(4, pad=0.01, borderpad=0, child=imagebox)
    ao.patch.set_alpha(0)
    ax.add_artist(ao)

fig, ax = plt.subplots()
ax.plot([1,2,3,4], [1,3,4.5,5])

watermark2(ax)
ax.set_xlabel("some xlabel")
plt.show()

这篇关于Matplotlib 自动放置水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆