9

When trying to add a rectangle patch with a hatch pattern to a plot it seems that it is impossible to set the keyword argument edgecolor to 'none' when also specifying a hatch value. In other words I am trying to add a hatched rectangle WITHOUT an edge but WITH a pattern filling. This doesnt seem to work. The pattern only shows up if I also allow an edge to be drawn around the rectangle patch.

Any help on how to achieve the desired behaviour?

1 Answers1

17

You should use the linewidth argument, which has to be set to zero.

Example (based on your other question's answer):

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)

# generate some data:
x,y = np.meshgrid(np.linspace(0,1),np.linspace(0,1))
z = np.ma.masked_array(x**2-y**2,mask=y>-x+1)

# plot your masked array
ax.contourf(z)

# plot a patch
p = patches.Rectangle((20,20), 20, 20, linewidth=0, fill=None, hatch='///')
ax.add_patch(p)
plt.show()

You'll get this image: enter image description here

Community
  • 1
  • 1
carla
  • 2,181
  • 19
  • 26
  • Can you control the linewidth of the hatch effect without increasing the border size? – jkokorian Jan 20 '15 at 22:21
  • 1
    The argument `linewidth` controls the width of the border only. According to the comments in [this answer](http://stackoverflow.com/questions/14325773/how-to-change-marker-border-width-and-hatch-width), it is not possible to control the linewidth of the hatch effect. – carla Jan 21 '15 at 14:54
  • 2
    @jkokorian and anyone else arriving here: There is a working solution to controlling linewidth of the hatch effect via rcParams here: https://stackoverflow.com/a/56675011/13917918 – cjstevens Jul 21 '21 at 12:14