Skip to content

patheffects for Line2d object : rebase of #1015 #1909

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
initial implementation of patheffect for collections
  • Loading branch information
leejjoon committed Apr 16, 2013
commit c883c000714562c8d480e4bdabdba1ada6d780a1
6 changes: 6 additions & 0 deletions examples/pylab_examples/patheffect_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
arr = np.arange(25).reshape((5,5))
ax2.imshow(arr)
cntr = ax2.contour(arr, colors="k")

plt.setp(cntr.collections,
path_effects=[PathEffects.withStroke(linewidth=3,
foreground="w")])


clbls = ax2.clabel(cntr, fmt="%2.0f", use_clabeltext=True)
plt.setp(clbls,
path_effects=[PathEffects.withStroke(linewidth=3,
Expand Down
33 changes: 28 additions & 5 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self,
urls=None,
offset_position='screen',
zorder=1,
path_effects=None,
**kwargs
):
"""
Expand Down Expand Up @@ -123,6 +124,7 @@ def __init__(self,
else:
self._uniform_offsets = offsets

self._path_effects = None
self.update(kwargs)
self._paths = None

Expand Down Expand Up @@ -255,11 +257,21 @@ def draw(self, renderer):
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(),
self._linewidths, self._linestyles, self._antialiaseds, self._urls,
self._offset_position)
if self.get_path_effects():
#from patheffects import PathEffectsRenderer
for pe in self.get_path_effects():
pe.draw_path_collection(renderer,
gc, transform.frozen(), paths, self.get_transforms(),
offsets, transOffset, self.get_facecolor(), self.get_edgecolor(),
self._linewidths, self._linestyles, self._antialiaseds, self._urls,
self._offset_position)
else:

renderer.draw_path_collection(
gc, transform.frozen(), paths, self.get_transforms(),
offsets, transOffset, self.get_facecolor(), self.get_edgecolor(),
self._linewidths, self._linestyles, self._antialiaseds, self._urls,
self._offset_position)

gc.restore()
renderer.close_group(self.__class__.__name__)
Expand Down Expand Up @@ -638,6 +650,17 @@ def update_from(self, other):
self.cmap = other.cmap
# self.update_dict = other.update_dict # do we need to copy this? -JJL

def set_path_effects(self, path_effects):
"""
set path_effects, which should be a list of instances of
matplotlib.patheffect._Base class or its derivatives.
"""
self._path_effects = path_effects

def get_path_effects(self):
return self._path_effects


# these are not available for the object inspector until after the
# class is built so we define an initial set here for the init
# function and they will be overridden after object defn
Expand Down
66 changes: 51 additions & 15 deletions lib/matplotlib/patheffects.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,46 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
"""
renderer.draw_path(gc, tpath, affine, rgbFace)

def draw_path_collection(self, renderer,
gc, master_transform, paths, all_transforms,
offsets, offsetTrans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position):
"""
Draws a collection of paths selecting drawing properties from
the lists *facecolors*, *edgecolors*, *linewidths*,
*linestyles* and *antialiaseds*. *offsets* is a list of
offsets to apply to each of the paths. The offsets in
*offsets* are first transformed by *offsetTrans* before being
applied. *offset_position* may be either "screen" or "data"
depending on the space that the offsets are in.

This provides a fallback implementation of
:meth:`draw_path_collection` that makes multiple calls to
:meth:`draw_path`. Some backends may want to override this in
order to render each set of path data only once, and then
reference that path multiple times with the different offsets,
colors, styles etc. The generator methods
:meth:`_iter_collection_raw_paths` and
:meth:`_iter_collection` are provided to help with (and
standardize) the implementation across backends. It is highly
recommended to use those generators, so that changes to the
behavior of :meth:`draw_path_collection` can be made globally.
"""
path_ids = []
for path, transform in renderer._iter_collection_raw_paths(
master_transform, paths, all_transforms):
path_ids.append((path, transform))

for xo, yo, path_id, gc0, rgbFace in renderer._iter_collection(
gc, master_transform, all_transforms, path_ids, offsets,
offsetTrans, facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):
path, transform = path_id
transform = transforms.Affine2D(transform.get_matrix()).translate(xo, yo)
self.draw_path(renderer, gc0, path, transform, rgbFace)


def draw_tex(self, renderer, gc, x, y, s, prop, angle, ismath='TeX!'):
self._draw_text_as_path(renderer, gc, x, y, s, prop, angle, ismath="TeX")

Expand Down Expand Up @@ -101,21 +141,6 @@ def draw_markers(self, renderer, gc, marker_path, marker_trans, path, trans, rgb
marker_trans + transforms.Affine2D().translate(x, y),
rgbFace)

# def draw_path_collection(self, renderer,
# gc, master_transform, paths, all_transforms,
# offsets, offsetTrans, facecolors, edgecolors,
# linewidths, linestyles, antialiaseds, urls):
# path_ids = []
# for path, transform in renderer._iter_collection_raw_paths(
# master_transform, paths, all_transforms):
# path_ids.append((path, transform))

# for xo, yo, path_id, gc0, rgbFace in renderer._iter_collection(
# gc, path_ids, offsets, offsetTrans, facecolors, edgecolors,
# linewidths, linestyles, antialiaseds, urls):
# path, transform = path_id
# transform = transforms.Affine2D(transform.get_matrix()).translate(xo, yo)
# self.draw_path(renderer, gc0, path, transform, rgbFace)

class ProxyRenderer(object):
def __init__(self, path_effect, renderer):
Expand All @@ -138,6 +163,17 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
gc, marker_path, marker_trans, path, trans,
rgbFace=rgbFace)

def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offsetTrans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position):
pe = self._path_effect
pe.draw_path_collection(self._renderer,
gc, master_transform, paths, all_transforms,
offsets, offsetTrans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position)


class Normal(_Base):
"""
Expand Down