python - 用补丁剪裁轴
问题描述
我在理解如何用补丁剪辑轴时遇到了一些麻烦。我想让蓝色矩形位于轴的背景中。但我的剪辑电话什么也没做。
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()
我需要如何正确设置它?文档非常缺乏。
解决方案
您需要做的就是提供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)
推荐阅读
- python-3.x - 有没有更好的方法来验证龙卷风上的查询字符串参数?
- python - Numpy cannot always convert a list of arrays to an object-array of arrays
- r - Problems with getMgmtDisc (from edgar): "no filing information found" or "object ´words.count´ not found"
- c# - How to get Microsoft Edge instance from C#.Net
- android - How to pick any part of image when set as wallpaper for android in Flutter app?
- c# - c# Converting part of form into a child
- javascript - 比较十六进制字符串
- node.js - 如何解决节点 js 中的以下要求?
- excel - Finding header/title of nearest number from a row
- flutter - 如何在颤动中改变动画方向