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
Drawing rectangle with border only in matplotlib
提问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:

这使:

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



