41

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?

2 Answers 2

42

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'))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. There is also a parameter called 'filled'
@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.
29

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.