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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21445005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:52:19  来源:igfitidea点击:

Drawing rectangle with border only in matplotlib

pythonmatplotlib

提问by Cupitor

So I found the following code here:

所以我在这里找到了以下代码:

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()

Which gives: enter image description here

这使: 在此处输入图片说明

But what I want is a rectangle with only a blue border and inside of it to be transparent. How can I do this?

但我想要的是一个只有蓝色边框且内部透明的矩形。我怎样才能做到这一点?

采纳答案by Paul H

You just need to set the facecolorto the string 'none' (not the python None)

你只需要设置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'))

回答by Cupitor

You should set the fill=None.

你应该设置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()

enter image description here

在此处输入图片说明