hatched rectangle patches without edges in matplotlib
Asked Answered
U

1

12

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?

Unitive answered 23/8, 2013 at 11:7 Comment(0)
L
20

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

Linneman answered 23/8, 2013 at 13:53 Comment(3)
Can you control the linewidth of the hatch effect without increasing the border size?Angulation
The argument linewidth controls the width of the border only. According to the comments in this answer, it is not possible to control the linewidth of the hatch effect.Linneman
@Angulation and anyone else arriving here: There is a working solution to controlling linewidth of the hatch effect via rcParams here: https://mcmap.net/q/681855/-changing-hatch-color-in-matplotlibKoan

© 2022 - 2024 — McMap. All rights reserved.