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

时间:2014-01-29 23:35:15

标签: python matplotlib

所以我找到了以下代码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()

给出了: enter image description here

但我想要的是一个只有蓝色边框的矩形,里面是透明的。我怎么能这样做?

2 个答案:

答案 0 :(得分:20)

您只需将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 :(得分:14)

您应该设置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