Skip to content

Commit 94722ef

Browse files
committed
Merge branch 'contour_hatching' of https://github.com/pelson/matplotlib into pelson-contour_hatching2
2 parents bbe1053 + babf932 commit 94722ef

File tree

7 files changed

+308
-48
lines changed

7 files changed

+308
-48
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import matplotlib.pyplot as plt
2+
import numpy
3+
4+
5+
# invent some numbers, turning the x and y arrays into simple
6+
# 2d arrays, which make combining them together easier.
7+
x = numpy.linspace(-3, 5, 150).reshape(1, -1)
8+
y = numpy.linspace(-3, 5, 120).reshape(-1, 1)
9+
z = numpy.cos(x) + numpy.sin(y)
10+
11+
# we no longer need x and y to be 2 dimensional, so flatten them.
12+
x, y = x.flatten(), y.flatten()
13+
14+
15+
# ---------------------------------------------
16+
# | Plot #1 |
17+
# ---------------------------------------------
18+
# the simplest hatched plot with a colorbar
19+
fig = plt.figure()
20+
cs = plt.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
21+
cmap=plt.get_cmap('gray'),
22+
extend='both', alpha=0.5
23+
)
24+
plt.colorbar()
25+
26+
27+
# ---------------------------------------------
28+
# | Plot #2 |
29+
# ---------------------------------------------
30+
# a plot of hatches without color with a legend
31+
plt.figure()
32+
n_levels = 6
33+
plt.contour(x, y, z, n_levels, colors='black', linestyles='-')
34+
cs = plt.contourf(x, y, z, n_levels, colors='none',
35+
hatches=['.', '/', '\\', None, '\\\\', '*'],
36+
extend='lower'
37+
)
38+
39+
# create a legend for the contour set
40+
artists, labels = cs.legend_elements()
41+
plt.legend(artists, labels, handleheight=2)
42+
43+
44+
45+
plt.show()

lib/matplotlib/collections.py

Lines changed: 40 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
@@ -547,6 +585,7 @@ def update_from(self, other):
547585
self._linewidths = other._linewidths
548586
self._linestyles = other._linestyles
549587
self._pickradius = other._pickradius
588+
self._hatch = other._hatch
550589

551590
# update_from for scalarmappable
552591
self._A = other._A

0 commit comments

Comments
 (0)