Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing rectangle with border only in matplotlib

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?

like image 644
Cupitor Avatar asked Jan 29 '14 23:01

Cupitor


People also ask

How do I set boundaries in Matplotlib?

Matplotlib automatically arrives at the minimum and maximum values of variables to be displayed along x, y (and z axis in case of 3D plot) axes of a plot. However, it is possible to set the limits explicitly by using set_xlim() and set_ylim() functions.

How do you draw a rectangle on a graph in Python?

We can add rectangle by using Rectangle() function in patches module. The Rectangle function takes the location/size of the rectangle you need, left position, bottom location, and width & height.

How do I fill a area in Matplotlib?

You can easily fill in the area between values in a Matplotlib plot by using following functions: fill_between(): Fill the area between two horizontal curves. fill_betweenx(): Fill the area between two vertical curves.


1 Answers

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')) 
like image 75
Paul H Avatar answered Oct 04 '22 15:10

Paul H