From a022b52958892c947c0c6a05abba229ecf09b54a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Manuel=20Mart=C3=AD?= Date: Sun, 5 Nov 2017 23:59:39 -0500 Subject: [PATCH 1/3] Override set_url() in class Tick In the class Tick, the url attribute is not passed to any of the objects that are to be drawn. Suggested solution is to override the set_url() method in Tick to transfer the url attribute content to the tick labels, that are to be drawn. --- lib/matplotlib/axis.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 7279749377d4..803643480e48 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -316,6 +316,19 @@ def set_label2(self, s): self.label2.set_text(s) self.stale = True + def set_url(self, url): + """ + Set the url of label1 and label2. + + Parameters + ---------- + url : str + """ + super().set_url(url) + self.label1.set_url(url) + self.label2.set_url(url) + self.stale = True + def _set_artist_props(self, a): a.set_figure(self.figure) From 3964f2a5dbded01b734c832741f22701bd1f4026 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 6 May 2020 01:58:57 -0400 Subject: [PATCH 2/3] Add test for urls on ticks in svg. --- lib/matplotlib/tests/test_backend_svg.py | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/matplotlib/tests/test_backend_svg.py b/lib/matplotlib/tests/test_backend_svg.py index 14d1805f3ffd..9022abe9d317 100644 --- a/lib/matplotlib/tests/test_backend_svg.py +++ b/lib/matplotlib/tests/test_backend_svg.py @@ -214,3 +214,42 @@ def test_savefig_tight(): # Check that the draw-disabled renderer correctly disables open/close_group # as well. plt.savefig(BytesIO(), format="svgz", bbox_inches="tight") + + +def test_url(): + # Test that object url appears in output svg. + + fig, ax = plt.subplots() + s = ax.scatter([1, 2, 3], [4, 5, 6]) + s.set_urls(['http://example.com/foo', 'http://example.com/bar', None]) + + b = BytesIO() + fig.savefig(b, format='svg') + b = b.getvalue() + assert b'http://example.com/foo' in b + assert b'http://example.com/bar' in b + + +def test_url_tick(): + fig1, ax = plt.subplots() + ax.scatter([1, 2, 3], [4, 5, 6]) + for i, tick in enumerate(ax.yaxis.get_major_ticks()): + tick.set_url(f'http://example.com/{i}') + + fig2, ax = plt.subplots() + ax.scatter([1, 2, 3], [4, 5, 6]) + for i, tick in enumerate(ax.yaxis.get_major_ticks()): + tick.label1.set_url(f'http://example.com/{i}') + tick.label2.set_url(f'http://example.com/{i}') + + b1 = BytesIO() + fig1.savefig(b1, format='svg') + b1 = b1.getvalue() + + b2 = BytesIO() + fig2.savefig(b2, format='svg') + b2 = b2.getvalue() + + for i in range(len(ax.yaxis.get_major_ticks())): + assert f'http://example.com/{i}'.encode('ascii') in b1 + assert b1 == b2 From 08169a90efa42dbef2e2adf57846f89faec2161e Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 6 May 2020 02:03:19 -0400 Subject: [PATCH 3/3] Ensure URLs are stored for plain lines in SVG. Fixes #17336. --- lib/matplotlib/lines.py | 2 ++ lib/matplotlib/tests/test_backend_svg.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index d2b76e4135bc..c3757ca71bf8 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -761,6 +761,7 @@ def draw(self, renderer): if len(tpath.vertices): gc = renderer.new_gc() self._set_gc_clip(gc) + gc.set_url(self.get_url()) lc_rgba = mcolors.to_rgba(self._color, self._alpha) gc.set_foreground(lc_rgba, isRGBA=True) @@ -787,6 +788,7 @@ def draw(self, renderer): if self._marker and self._markersize > 0: gc = renderer.new_gc() self._set_gc_clip(gc) + gc.set_url(self.get_url()) gc.set_linewidth(self._markeredgewidth) gc.set_antialiased(self._antialiased) diff --git a/lib/matplotlib/tests/test_backend_svg.py b/lib/matplotlib/tests/test_backend_svg.py index 9022abe9d317..e45ec7917849 100644 --- a/lib/matplotlib/tests/test_backend_svg.py +++ b/lib/matplotlib/tests/test_backend_svg.py @@ -220,14 +220,20 @@ def test_url(): # Test that object url appears in output svg. fig, ax = plt.subplots() + + # collections s = ax.scatter([1, 2, 3], [4, 5, 6]) s.set_urls(['http://example.com/foo', 'http://example.com/bar', None]) + # Line2D + p, = plt.plot([1, 3], [6, 5]) + p.set_url('http://example.com/baz') + b = BytesIO() fig.savefig(b, format='svg') b = b.getvalue() - assert b'http://example.com/foo' in b - assert b'http://example.com/bar' in b + for v in [b'foo', b'bar', b'baz']: + assert b'http://example.com/' + v in b def test_url_tick():