Skip to content

Contour hatching #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/pylab_examples/contourf_hatching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import matplotlib.pyplot as plt
import numpy


# invent some numbers, turning the x and y arrays into simple
# 2d arrays, which make combining them together easier.
x = numpy.linspace(-3, 5, 150).reshape(1, -1)
y = numpy.linspace(-3, 5, 120).reshape(-1, 1)
z = numpy.cos(x) + numpy.sin(y)

# we no longer need x and y to be 2 dimensional, so flatten them.
x, y = x.flatten(), y.flatten()


# ---------------------------------------------
# | Plot #1 |
# ---------------------------------------------
# the simplest hatched plot with a colorbar
fig = plt.figure()
cs = plt.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
cmap=plt.get_cmap('gray'),
extend='both', alpha=0.5
)
plt.colorbar()


# ---------------------------------------------
# | Plot #2 |
# ---------------------------------------------
# a plot of hatches without color with a legend
plt.figure()
n_levels = 6
plt.contour(x, y, z, n_levels, colors='black', linestyles='-')
cs = plt.contourf(x, y, z, n_levels, colors='none',
hatches=['.', '/', '\\', None, '\\\\', '*'],
extend='lower'
)

# create a legend for the contour set
artists, labels = cs.legend_elements()
plt.legend(artists, labels, handleheight=2)



plt.show()
41 changes: 40 additions & 1 deletion lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
:class:`matplotlib.cm.ScalarMappable`)
* *cmap*: None (optional for
:class:`matplotlib.cm.ScalarMappable`)
* *hatch*: None

*offsets* and *transOffset* are used to translate the patch after
rendering (default no offsets).
Expand Down Expand Up @@ -77,6 +78,7 @@ def __init__(self,
norm = None, # optional for ScalarMappable
cmap = None, # ditto
pickradius = 5.0,
hatch=None,
urls = None,
**kwargs
):
Expand All @@ -95,6 +97,7 @@ def __init__(self,
self.set_antialiased(antialiaseds)
self.set_pickradius(pickradius)
self.set_urls(urls)
self.set_hatch(hatch)


self._uniform_offsets = None
Expand Down Expand Up @@ -232,7 +235,10 @@ def draw(self, renderer):
gc = renderer.new_gc()
self._set_gc_clip(gc)
gc.set_snap(self.get_snap())


if self._hatch:
gc.set_hatch(self._hatch)

renderer.draw_path_collection(
gc, transform.frozen(), paths, self.get_transforms(),
offsets, transOffset, self.get_facecolor(), self.get_edgecolor(),
Expand Down Expand Up @@ -292,6 +298,38 @@ def set_urls(self, urls):

def get_urls(self): return self._urls

def set_hatch(self, hatch):
"""
Set the hatching pattern

*hatch* can be one of::

/ - diagonal hatching
\ - back diagonal
| - vertical
- - horizontal
+ - crossed
x - crossed diagonal
o - small circle
O - large circle
. - dots
* - stars

Letters can be combined, in which case all the specified
hatchings are done. If same letter repeats, it increases the
density of hatching of that pattern.

Hatching is supported in the PostScript, PDF, SVG and Agg
backends only.

ACCEPTS: [ '/' | '\\\\' | '|' | '-' | '+' | 'x' | 'o' | 'O' | '.' | '*' ]
"""
self._hatch = hatch

def get_hatch(self):
'Return the current hatching pattern'
return self._hatch

def set_offsets(self, offsets):
"""
Set the offsets for the collection. *offsets* can be a scalar
Expand Down Expand Up @@ -547,6 +585,7 @@ def update_from(self, other):
self._linewidths = other._linewidths
self._linestyles = other._linestyles
self._pickradius = other._pickradius
self._hatch = other._hatch

# update_from for scalarmappable
self._A = other._A
Expand Down
Loading