Skip to content
Open
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
20 changes: 19 additions & 1 deletion lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,20 @@ def __init__(self, width, height, svgwriter, basename=None, image_dpi=72,
id=mpl.rcParams['svg.id'],
attrib={'xmlns:xlink': "http://www.w3.org/1999/xlink"})
self._write_metadata(metadata)
self._write_highlight_gradient()
self._write_default_style()

def _write_highlight_gradient(self):
# Add highlight gradient (id="_m_overlayGradient")
highlightGradient = """
<radialGradient id="_m_overlayGradient" cx="40%" cy="40%" r="70%">
<stop offset="10%" stop-color="#ffffff" stop-opacity="0.8"/>
<stop offset="40%" stop-color="#ffffff" stop-opacity="0"/>
<stop offset="100%" stop-color="#ffffff" stop-opacity="0"/>
</radialGradient>"""
self.writer._XMLWriter__write(f"<defs>\n{highlightGradient}\n</defs>")
return

def _get_clippath_id(self, clippath):
"""
Returns a stable and unique identifier for the *clippath* argument
Expand Down Expand Up @@ -704,7 +716,8 @@ def draw_path(self, gc, path, transform, rgbFace=None):
self.writer.end('a')

def draw_markers(
self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
self, gc, marker_path, marker_trans, path,
trans, rgbFace=None, highlight=False):
# docstring inherited

if not len(path.vertices):
Expand Down Expand Up @@ -742,6 +755,11 @@ def draw_markers(
attrib['y'] = _short_float_fmt(y)
attrib['style'] = self._get_style(gc, rgbFace)
writer.element('use', attrib=attrib)
if highlight:
mask_attrib = attrib.copy()
del mask_attrib['style']
mask_attrib['fill'] = "url(#_m_overlayGradient)"
writer.element('use', attrib=mask_attrib)
if gc.get_url() is not None:
self.writer.end('a')
writer.end('g')
Expand Down
10 changes: 9 additions & 1 deletion lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,17 @@ def draw(self, renderer):
gc.set_dashes(*self._linestyles[0])
gc.set_antialiased(self._antialiaseds[0])
gc.set_url(self._urls[0])

kwargs = {}
from .backends.backend_svg import RendererSVG
svg_func = getattr(RendererSVG.draw_markers, "__qualname__", None)
local_func = getattr(renderer.draw_markers, "__qualname__", None)
if hasattr(self, "_highlight_svg") and svg_func == local_func:
kwargs["highlight"] = self._highlight_svg

renderer.draw_markers(
gc, paths[0], combined_transform.frozen(),
mpath.Path(offsets), offset_trf, tuple(facecolors[0]))
mpath.Path(offsets), offset_trf, tuple(facecolors[0]), **kwargs)
else:
# The current new API of draw_path_collection() is provisional
# and will be changed in a future PR.
Expand Down
8 changes: 7 additions & 1 deletion lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ def create_artists(self, legend, orig_handle,

legline.set_transform(trans)

if hasattr(orig_handle, "_highlight_svg"):
legline._highlight_svg = orig_handle._highlight_svg

return [legline]


Expand Down Expand Up @@ -504,10 +507,13 @@ class HandlerPathCollection(HandlerRegularPolyCollection):
r"""Handler for `.PathCollection`\s, which are used by `~.Axes.scatter`."""

def create_collection(self, orig_handle, sizes, offsets, offset_transform):
return type(orig_handle)(
leg_handle = type(orig_handle)(
[orig_handle.get_paths()[0]], sizes=sizes,
offsets=offsets, offset_transform=offset_transform,
)
if hasattr(orig_handle, "_highlight_svg"):
leg_handle._highlight_svg = orig_handle._highlight_svg
return leg_handle


class HandlerCircleCollection(HandlerRegularPolyCollection):
Expand Down
11 changes: 9 additions & 2 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,17 +885,24 @@ def draw(self, renderer):
else:
# Don't scale for pixels, and don't stroke them
marker_trans = marker_trans.scale(w)

kwargs = {}
from .backends.backend_svg import RendererSVG
svg_func = getattr(RendererSVG.draw_markers, "__qualname__", None)
local_func = getattr(renderer.draw_markers, "__qualname__", None)
if hasattr(self, "_highlight_svg") and svg_func == local_func:
kwargs["highlight"] = self._highlight_svg
renderer.draw_markers(gc, marker_path, marker_trans,
subsampled, affine.frozen(),
fc_rgba)
fc_rgba, **kwargs)

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(), fcalt_rgba)
affine.frozen(), fcalt_rgba, **kwargs)

gc.restore()

Expand Down
Loading