Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 510482f

Browse files
authored
Merge pull request #17338 from QuLogic/svg-url
ENH: Support url on more Artists in svg
2 parents c0e1cfc + 08169a9 commit 510482f

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

lib/matplotlib/axis.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,19 @@ def set_label2(self, s):
316316
self.label2.set_text(s)
317317
self.stale = True
318318

319+
def set_url(self, url):
320+
"""
321+
Set the url of label1 and label2.
322+
323+
Parameters
324+
----------
325+
url : str
326+
"""
327+
super().set_url(url)
328+
self.label1.set_url(url)
329+
self.label2.set_url(url)
330+
self.stale = True
331+
319332
def _set_artist_props(self, a):
320333
a.set_figure(self.figure)
321334

lib/matplotlib/lines.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ def draw(self, renderer):
761761
if len(tpath.vertices):
762762
gc = renderer.new_gc()
763763
self._set_gc_clip(gc)
764+
gc.set_url(self.get_url())
764765

765766
lc_rgba = mcolors.to_rgba(self._color, self._alpha)
766767
gc.set_foreground(lc_rgba, isRGBA=True)
@@ -787,6 +788,7 @@ def draw(self, renderer):
787788
if self._marker and self._markersize > 0:
788789
gc = renderer.new_gc()
789790
self._set_gc_clip(gc)
791+
gc.set_url(self.get_url())
790792
gc.set_linewidth(self._markeredgewidth)
791793
gc.set_antialiased(self._antialiased)
792794

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,48 @@ def test_savefig_tight():
214214
# Check that the draw-disabled renderer correctly disables open/close_group
215215
# as well.
216216
plt.savefig(BytesIO(), format="svgz", bbox_inches="tight")
217+
218+
219+
def test_url():
220+
# Test that object url appears in output svg.
221+
222+
fig, ax = plt.subplots()
223+
224+
# collections
225+
s = ax.scatter([1, 2, 3], [4, 5, 6])
226+
s.set_urls(['http://example.com/foo', 'http://example.com/bar', None])
227+
228+
# Line2D
229+
p, = plt.plot([1, 3], [6, 5])
230+
p.set_url('http://example.com/baz')
231+
232+
b = BytesIO()
233+
fig.savefig(b, format='svg')
234+
b = b.getvalue()
235+
for v in [b'foo', b'bar', b'baz']:
236+
assert b'http://example.com/' + v in b
237+
238+
239+
def test_url_tick():
240+
fig1, ax = plt.subplots()
241+
ax.scatter([1, 2, 3], [4, 5, 6])
242+
for i, tick in enumerate(ax.yaxis.get_major_ticks()):
243+
tick.set_url(f'http://example.com/{i}')
244+
245+
fig2, ax = plt.subplots()
246+
ax.scatter([1, 2, 3], [4, 5, 6])
247+
for i, tick in enumerate(ax.yaxis.get_major_ticks()):
248+
tick.label1.set_url(f'http://example.com/{i}')
249+
tick.label2.set_url(f'http://example.com/{i}')
250+
251+
b1 = BytesIO()
252+
fig1.savefig(b1, format='svg')
253+
b1 = b1.getvalue()
254+
255+
b2 = BytesIO()
256+
fig2.savefig(b2, format='svg')
257+
b2 = b2.getvalue()
258+
259+
for i in range(len(ax.yaxis.get_major_ticks())):
260+
assert f'http://example.com/{i}'.encode('ascii') in b1
261+
assert b1 == b2

0 commit comments

Comments
 (0)