35

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?

Community
  • 1
  • 1
Cupitor
  • 11,007
  • 19
  • 65
  • 91

2 Answers2

37

You just need to set the facecolor to the string 'none' (not the 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'))
Paul H
  • 65,268
  • 20
  • 159
  • 136
  • 1
    Thanks. There is also a parameter called 'filled' – Cupitor Jan 30 '14 at 00:13
  • 5
    @Naji N.B.: `fill` is expecting a boolean; so you could keep `fill=True` and still get a hollow rectangle with `facecolor='none'` in the case that you were looping through different colors and `'none'` was one of them. – Paul H Jan 30 '14 at 00:16
  • 3
    For reference: [matplotlib.patches.Rectangle API](http://matplotlib.org/api/patches_api.html#matplotlib.patches.Rectangle). – Evgeni Sergeev Mar 04 '17 at 10:07
27

You should set the 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

Renaud
  • 16,073
  • 6
  • 81
  • 79
Cupitor
  • 11,007
  • 19
  • 65
  • 91