Skip to content

Commit 937e745

Browse files
authored
Pass AnnotationBbox gid to renderer (#24637)
* Fixes #20044 pass AnnotationBbox to renderer * Squash past 7 commits - test gid gets passed to the renderer * Use a context manager * Remove line
1 parent e0e54b2 commit 937e745

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,13 +1452,15 @@ def draw(self, renderer):
14521452
self._renderer = renderer
14531453
if not self.get_visible() or not self._check_xy(renderer):
14541454
return
1455+
renderer.open_group(self.__class__.__name__, gid=self.get_gid())
14551456
self.update_positions(renderer)
14561457
if self.arrow_patch is not None:
14571458
if self.arrow_patch.figure is None and self.figure is not None:
14581459
self.arrow_patch.figure = self.figure
14591460
self.arrow_patch.draw(renderer)
14601461
self.patch.draw(renderer)
14611462
self.offsetbox.draw(renderer)
1463+
renderer.close_group(self.__class__.__name__)
14621464
self.stale = False
14631465

14641466

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from matplotlib.testing.decorators import check_figures_equal, image_comparison
1616
from matplotlib.testing._markers import needs_usetex
1717
from matplotlib import font_manager as fm
18+
from matplotlib.offsetbox import (OffsetImage, AnnotationBbox)
1819

1920

2021
def test_visibility():
@@ -609,3 +610,34 @@ def test_svg_font_string(font_str, include_generic):
609610

610611
assert font_info == f"{size}px {font_str}"
611612
assert text_count == len(ax.texts)
613+
614+
615+
def test_annotationbbox_gid():
616+
# Test that object gid appears in the AnnotationBbox
617+
# in output svg.
618+
fig = plt.figure()
619+
ax = fig.add_subplot()
620+
arr_img = np.ones((32, 32))
621+
xy = (0.3, 0.55)
622+
623+
imagebox = OffsetImage(arr_img, zoom=0.1)
624+
imagebox.image.axes = ax
625+
626+
ab = AnnotationBbox(imagebox, xy,
627+
xybox=(120., -80.),
628+
xycoords='data',
629+
boxcoords="offset points",
630+
pad=0.5,
631+
arrowprops=dict(
632+
arrowstyle="->",
633+
connectionstyle="angle,angleA=0,angleB=90,rad=3")
634+
)
635+
ab.set_gid("a test for issue 20044")
636+
ax.add_artist(ab)
637+
638+
with BytesIO() as fd:
639+
fig.savefig(fd, format='svg')
640+
buf = fd.getvalue().decode('utf-8')
641+
642+
expected = '<g id="a test for issue 20044">'
643+
assert expected in buf

0 commit comments

Comments
 (0)