我在理解如何用补丁夹轴时遇到一些麻烦。我希望蓝色矩形位于轴的背景中。但是我的剪辑电话没有任何作用。
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()
我该如何正确设置?该文档非常缺乏。
答案 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)