在matplotlib中仅绘制带边框的矩形

36

所以我在这里找到了以下的代码(链接)

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
someX, someY = 0.5, 0.5
plt.figure()
currentAxis = plt.gca()
currentAxis.add_patch(Rectangle((someX - .1, someY - .1), 0.2, 0.2,alpha=1))
plt.show()

这意味着:

enter image description here

但我希望得到一个只有蓝色边框且内部透明的矩形。我该怎么做?

2个回答

37

你只需要将facecolor设置为字符串'none'(而不是Python中的None

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
someX, someY = 0.5, 0.5
fig,ax = plt.subplots()
currentAxis = plt.gca()
currentAxis.add_patch(Rectangle((someX - 0.1, someY - 0.1), 0.2, 0.2,
                      alpha=1, facecolor='none'))

1
谢谢。还有一个名为“filled”的参数。 - Cupitor
5
@Naji N.B.: fill 需要一个布尔值;因此,你可以保留 fill=True,即使在循环遍历不同颜色并且 'none' 是其中之一的情况下,仍然可以使用 facecolor='none' 来得到一个空心矩形。 - Paul H
3
参考资料:matplotlib.patches.Rectangle API - Evgeni Sergeev

28

您应该将fill=None设置。

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle

someX, someY = 0.5, 0.5
plt.figure()
currentAxis = plt.gca()
currentAxis.add_patch(Rectangle((someX - .1, someY - .1), 0.2, 0.2, fill=None, alpha=1))
plt.show()

输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接