带有补丁的剪切轴

时间:2018-12-23 22:11:25

标签: python matplotlib clipping

我在理解如何用补丁夹轴时遇到一些麻烦。我希望蓝色矩形位于轴的背景中。但是我的剪辑电话没有任何作用。

import matplotlib.pyplot as plt
from matplotlib import patches
import numpy as np

fig = plt.figure()

X, Y = np.mgrid[-1:1:.1, -1:1:.1]
Z = X+Y

ax1 = fig.add_subplot(111)
ax1.contourf(X, Y, Z)

frame = patches.Rectangle(
        (-.1,-.1), 1.2, 1.2, transform=ax1.transAxes, alpha=.5, fc='b', fill=True, linewidth=3, color='k'
    )
ax1.set_clip_path(frame) # has no effect
fig.patches.append(frame)

fig.show()

enter image description here

我该如何正确设置?该文档非常缺乏。

1 个答案:

答案 0 :(得分:2)

您需要做的就是提供zorder并将其置于后台。具体而言,在当前情况下,zorder=0是您的Rectangle补丁程序。

zorder作为参数,它决定将什么堆叠在什么上面。 zorder=0只会将补丁发送到堆栈中最低的位置,这意味着该图的最后一层。

frame = patches.Rectangle(
        (-.1,-.1), 1.2, 1.2, transform=ax1.transAxes, alpha=.5, fc='b', fill=True, linewidth=3, color='k'
    , zorder=0) # <--- zorder specified here
ax1.set_clip_path(frame) # has no effect
fig.patches.append(frame)

enter image description here