我正在尝试向当前图添加徽标,该图已经具有现有图元素。我在plot_pic()函数中定义了背景。然后绘制它,我想在顶部表面添加徽标。我尝试将zorder = 10放置,但不起作用。 Jupyter Notebook中的代码是:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.offsetbox import  OffsetImage
%matplotlib inline

from matplotlib.patches import Circle, Rectangle, Arc, Ellipse


def plot_pic(ax=None, color='black', lw=2, scale = 15):
    # get the current ax if ax is None
    if ax is None:
        ax = plt.gca()


    big_box =  Rectangle((-34 * scale, 0), width = 68 * scale, height = 105 / 2 * scale, linewidth=lw, color=color, fill=False)

    middle_box =  Rectangle((-(7.32 * scale / 2+ 5.5 * scale +11 * scale),0), width = (5.5 * scale * 2 + 11 * scale * 2 + 7.32 * scale), height = 16.5 * scale, linewidth = lw, color = color, fc = "white")

    small_box = Rectangle((-(7.32 * scale/ 2 + 5.5 * scale), 0), width = 7.32 * scale + 5.5 * scale * 2, height = 5.5 * scale, linewidth = lw, color = color, fill = False)

    arc = Circle((0, 11 * scale), radius = 9.15 * scale, color = color, lw = lw, fill = False, zorder = 0)

    # List of elements to be plotted
    pic_elements = [big_box, middle_box, small_box, arc]


    # Add the elements onto the axes
    for element in pic_elements:
        ax.add_patch(element)

    return ax

fig = plt.figure()
fig = plt.figure(figsize=(10, 10))
ax = plt.subplot()
logo=mpimg.imread('rbl_logo.png')
# You have to add your own logo, this is in my own folder
addLogo = OffsetImage(logo, zoom=0.6, zorder = 10)
addLogo.set_offset((200,-10)) # pass the position in a tuple
ax.add_artist(addLogo)
plt.xlim(-600,600)
plt.ylim(-100,1000)
plot_pic()


结果是,plot_pic()层覆盖了我想要显示的徽标的一部分,而我只想将徽标放置在覆盖下面所有元素的最上面。

python - 如何使用matplotlib将图像添加到所有现有图层的顶层表面-LMLPHP

反正有这样做吗?非常感谢你。

最佳答案

问题在于,使用关键字参数设置zorder会在OffsetBox内设置图像的zorder,这将无效。为了设置盒子本身的zorder,您需要在外部进行设置:

addLogo = OffsetImage(logo, zoom=0.6)
addLogo.set_zorder(10)

09-09 17:02