Skip to content
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

patheffects for Line2d object #1015

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/pylab_examples/patheffect_demo.py
Expand Up @@ -17,10 +17,23 @@
foreground="w"),
PathEffects.Normal()])

ax1.grid(True, linestyle="-")

pe = [PathEffects.withStroke(linewidth=3,
foreground="w")]
for l in ax1.get_xgridlines() + ax1.get_ygridlines():
l.set_path_effects(pe)

ax2 = plt.subplot(132)
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
Expand Up @@ -86,6 +86,7 @@ def __init__(self,
hatch=None,
urls = None,
offset_position='screen',
path_effects=None,
**kwargs
):
"""
Expand Down Expand Up @@ -119,6 +120,7 @@ def __init__(self,
else:
self._uniform_offsets = offsets

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

Expand Down Expand Up @@ -245,11 +247,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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These bifurcations are a little concerning to me. Is it impossible that this behaviour could live in the renderer rather than having a path_effects _Base class?

#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 @@ -627,6 +639,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
50 changes: 43 additions & 7 deletions lib/matplotlib/lines.py
Expand Up @@ -144,6 +144,7 @@ def __init__(self, xdata, ydata,
pickradius = 5,
drawstyle = None,
markevery = None,
path_effects = None,
**kwargs
):
"""
Expand Down Expand Up @@ -222,6 +223,8 @@ def __init__(self, xdata, ydata,
self._invalidy = True
self.set_data(xdata, ydata)

self.set_path_effects(path_effects)

def contains(self, mouseevent):
"""
Test whether the mouse event occurred on the line. The pick
Expand Down Expand Up @@ -504,7 +507,14 @@ def draw(self, renderer):
self._lineFunc = getattr(self, funcname)
funcname = self.drawStyles.get(self._drawstyle, '_draw_lines')
drawFunc = getattr(self, funcname)
drawFunc(renderer, gc, tpath, affine.frozen())

if self.get_path_effects():
affine_frozen = affine.frozen()
for pe in self.get_path_effects():
pe_renderer = pe.get_proxy_renderer(renderer)
drawFunc(pe_renderer, gc, tpath, affine_frozen)
else:
drawFunc(renderer, gc, tpath, affine.frozen())

if self._marker:
gc = renderer.new_gc()
Expand Down Expand Up @@ -552,16 +562,31 @@ def draw(self, renderer):
marker_trans = marker_trans.scale(w)
else:
gc.set_linewidth(0)
renderer.draw_markers(
gc, marker_path, marker_trans, subsampled, affine.frozen(),
rgbFace)

if self.get_path_effects():
affine_frozen = affine.frozen()
for pe in self.get_path_effects():
pe.draw_markers(renderer, gc, marker_path, marker_trans,
subsampled, affine_frozen, rgbFace)
else:
renderer.draw_markers(
gc, marker_path, marker_trans, subsampled, affine.frozen(),
rgbFace)

alt_marker_path = marker.get_alt_path()
if alt_marker_path:
alt_marker_trans = marker.get_alt_transform()
alt_marker_trans = alt_marker_trans.scale(w)
renderer.draw_markers(
gc, alt_marker_path, alt_marker_trans, subsampled,
affine.frozen(), rgbFaceAlt)
if self.get_path_effects():
affine_frozen = affine.frozen()
for pe in self.get_path_effects():
pe.draw_markers(renderer, gc, alt_marker_path,
alt_marker_trans, subsampled,
affine_frozen, rgbFaceAlt)
else:
renderer.draw_markers(
gc, alt_marker_path, alt_marker_trans, subsampled,
affine.frozen(), rgbFaceAlt)

gc.restore()

Expand Down Expand Up @@ -1103,6 +1128,17 @@ def is_dashed(self):
'return True if line is dashstyle'
return self._linestyle in ('--', '-.', ':')

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


class VertexSelector:
"""
Manage the callbacks to maintain a list of selected vertices for
Expand Down
120 changes: 103 additions & 17 deletions lib/matplotlib/patheffects.py
Expand Up @@ -10,7 +10,6 @@
import matplotlib.transforms as transforms



class _Base(object):
"""
A base class for PathEffect. Derived must override draw_path method.
Expand All @@ -22,6 +21,9 @@ def __init__(self):
"""
super(_Base, self).__init__()

def get_proxy_renderer(self, renderer):
pe_renderer = ProxyRenderer(self, renderer)
return pe_renderer

def _update_gc(self, gc, new_gc_dict):
new_gc_dict = new_gc_dict.copy()
Expand All @@ -39,7 +41,7 @@ def _update_gc(self, gc, new_gc_dict):
return gc


def draw_path(self, renderer, gc, tpath, affine, rgbFace):
def draw_path(self, renderer, gc, tpath, affine, rgbFace=None):
"""
Derived should override this method. The argument is same
as *draw_path* method of :class:`matplotlib.backend_bases.RendererBase`
Expand All @@ -51,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 All @@ -71,22 +113,66 @@ def _draw_text_as_path(self, renderer, gc, x, y, s, prop, angle, ismath):
gc.set_linewidth(0.0)
self.draw_path(renderer, gc, path, transform, rgbFace=color)

def draw_markers(self, renderer, gc, marker_path, marker_trans, path, trans, rgbFace=None):
"""
Draws a marker at each of the vertices in path. This includes
all vertices, including control points on curves. To avoid
that behavior, those vertices should be removed before calling
this function.

*gc*
the :class:`GraphicsContextBase` instance

# 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)
*marker_trans*
is an affine transform applied to the marker.

*trans*
is an affine transform applied to the path.

This provides a fallback implementation of draw_markers that
makes multiple calls to :meth:`draw_path`. Some backends may
want to override this method in order to draw the marker only
once and reuse it multiple times.
"""
for vertices, codes in path.iter_segments(trans, simplify=False):
if len(vertices):
x,y = vertices[-2:]
self.draw_path(renderer, gc, marker_path,
marker_trans + transforms.Affine2D().translate(x, y),
rgbFace)


class ProxyRenderer(object):
def __init__(self, path_effect, renderer):
self._path_effect = path_effect
self._renderer = renderer

def draw_path(self, gc, tpath, affine, rgbFace=None):
self._path_effect.draw_path(self._renderer, gc, tpath, affine, rgbFace)

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

def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
self._path_effect._draw_text(self.renderer,
gc, x, y, s, prop, angle, ismath)

def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
self._path_effect.draw_markers(self._renderer,
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