Skip to content

Commit 6e2061a

Browse files
author
Phil Elson
committed
First pass support at contour hatching.
1 parent d6ec786 commit 6e2061a

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/matplotlib/collections.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
4646
:class:`matplotlib.cm.ScalarMappable`)
4747
* *cmap*: None (optional for
4848
:class:`matplotlib.cm.ScalarMappable`)
49+
* *hatch*: None
4950
5051
*offsets* and *transOffset* are used to translate the patch after
5152
rendering (default no offsets).
@@ -77,6 +78,7 @@ def __init__(self,
7778
norm = None, # optional for ScalarMappable
7879
cmap = None, # ditto
7980
pickradius = 5.0,
81+
hatch=None,
8082
urls = None,
8183
**kwargs
8284
):
@@ -95,6 +97,7 @@ def __init__(self,
9597
self.set_antialiased(antialiaseds)
9698
self.set_pickradius(pickradius)
9799
self.set_urls(urls)
100+
self.set_hatch(hatch)
98101

99102

100103
self._uniform_offsets = None
@@ -232,7 +235,10 @@ def draw(self, renderer):
232235
gc = renderer.new_gc()
233236
self._set_gc_clip(gc)
234237
gc.set_snap(self.get_snap())
235-
238+
239+
if self._hatch:
240+
gc.set_hatch(self._hatch)
241+
236242
renderer.draw_path_collection(
237243
gc, transform.frozen(), paths, self.get_transforms(),
238244
offsets, transOffset, self.get_facecolor(), self.get_edgecolor(),
@@ -292,6 +298,38 @@ def set_urls(self, urls):
292298

293299
def get_urls(self): return self._urls
294300

301+
def set_hatch(self, hatch):
302+
"""
303+
Set the hatching pattern
304+
305+
*hatch* can be one of::
306+
307+
/ - diagonal hatching
308+
\ - back diagonal
309+
| - vertical
310+
- - horizontal
311+
+ - crossed
312+
x - crossed diagonal
313+
o - small circle
314+
O - large circle
315+
. - dots
316+
* - stars
317+
318+
Letters can be combined, in which case all the specified
319+
hatchings are done. If same letter repeats, it increases the
320+
density of hatching of that pattern.
321+
322+
Hatching is supported in the PostScript, PDF, SVG and Agg
323+
backends only.
324+
325+
ACCEPTS: [ '/' | '\\\\' | '|' | '-' | '+' | 'x' | 'o' | 'O' | '.' | '*' ]
326+
"""
327+
self._hatch = hatch
328+
329+
def get_hatch(self):
330+
'Return the current hatching pattern'
331+
return self._hatch
332+
295333
def set_offsets(self, offsets):
296334
"""
297335
Set the offsets for the collection. *offsets* can be a scalar

lib/matplotlib/contour.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@ def __init__(self, ax, *args, **kwargs):
731731
self.linewidths = kwargs.get('linewidths', None)
732732
self.linestyles = kwargs.get('linestyles', None)
733733

734+
self.hatch = kwargs.get('hatch', [None])
735+
734736
self.alpha = kwargs.get('alpha', None)
735737
self.origin = kwargs.get('origin', None)
736738
self.extent = kwargs.get('extent', None)
@@ -909,9 +911,12 @@ def changed(self):
909911
tcolors = [ (tuple(rgba),) for rgba in
910912
self.to_rgba(self.cvalues, alpha=self.alpha)]
911913
self.tcolors = tcolors
912-
for color, collection in zip(tcolors, self.collections):
914+
thatches = self.hatch * len(tcolors)
915+
self.thatches = thatches
916+
for color, hatch, collection in zip(tcolors, thatches, self.collections):
913917
if self.filled:
914918
collection.set_facecolor(color)
919+
collection.set_hatch(hatch)
915920
else:
916921
collection.set_color(color)
917922
for label, cv in zip(self.labelTexts, self.labelCValues):

0 commit comments

Comments
 (0)