-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Pass gid to renderer #15087
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
Pass gid to renderer #15087
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
The ``gid`` is now correctly passed to svg output | ||
````````````````````````````````````````````````` | ||
|
||
Previously, if a figure, axis, legend or some other artists had a custom | ||
``gid`` set (e.g. via ``.set_gid()``), this would not be reflected in | ||
the svg output. Instead a default gid, like ``figure_1`` would be shown. | ||
This is now fixed, such that e.g. ``fig.set_gid("myfigure")`` correctly | ||
shows up as ``<g id="myfigure">`` in the svg file. If you relied on the | ||
gid having the default format, you now need to make sure not to set the | ||
``gid`` parameter of the artists. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import numpy as np | ||
from io import BytesIO | ||
import os | ||
import re | ||
import tempfile | ||
import warnings | ||
|
@@ -145,3 +144,67 @@ def test_svgnone_with_data_coordinates(): | |
buf = fd.read().decode() | ||
|
||
assert expected in buf | ||
|
||
|
||
def test_gid(): | ||
"""Test that object gid appears in output svg.""" | ||
from matplotlib.offsetbox import OffsetBox | ||
from matplotlib.axis import Tick | ||
|
||
fig = plt.figure() | ||
|
||
ax1 = fig.add_subplot(131) | ||
ax1.imshow([[1., 2.], [2., 3.]], aspect="auto") | ||
ax1.scatter([1, 2, 3], [1, 2, 3], label="myscatter") | ||
ax1.plot([2, 3, 1], label="myplot") | ||
ax1.legend() | ||
ax1a = ax1.twinx() | ||
ax1a.bar([1, 2, 3], [1, 2, 3]) | ||
|
||
ax2 = fig.add_subplot(132, projection="polar") | ||
ax2.plot([0, 1.5, 3], [1, 2, 3]) | ||
|
||
ax3 = fig.add_subplot(133, projection="3d") | ||
ax3.plot([1, 2], [1, 2], [1, 2]) | ||
|
||
fig.canvas.draw() | ||
|
||
gdic = {} | ||
for idx, obj in enumerate(fig.findobj(include_self=True)): | ||
if obj.get_visible(): | ||
gid = f"test123{obj.__class__.__name__}_{idx}" | ||
gdic[gid] = obj | ||
obj.set_gid(gid) | ||
|
||
fd = BytesIO() | ||
fig.savefig(fd, format='svg') | ||
fd.seek(0) | ||
buf = fd.read().decode() | ||
fd.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could just write:
|
||
|
||
def include(gid, obj): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really literally, whether or not to include an object in the test. (Not all objects in the figure actually appear in the svg, so we need to exclude them). I've added a docstring to it. |
||
# we need to exclude certain objects which will not appear in the svg | ||
if isinstance(obj, OffsetBox): | ||
return False | ||
if isinstance(obj, plt.Text): | ||
if obj.get_text() == "": | ||
return False | ||
elif obj.axes is None: | ||
return False | ||
if isinstance(obj, plt.Line2D): | ||
if np.array(obj.get_data()).shape == (2, 1): | ||
return False | ||
elif not hasattr(obj, "axes") or obj.axes is None: | ||
return False | ||
if isinstance(obj, Tick): | ||
loc = obj.get_loc() | ||
if loc == 0: | ||
return False | ||
vi = obj.get_view_interval() | ||
if loc < min(vi) or loc > max(vi): | ||
return False | ||
return True | ||
|
||
for gid, obj in gdic.items(): | ||
if include(gid, obj): | ||
assert gid in buf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need the draw() here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I don't draw it, the ticks will not be the ones that are exported, because the ticker only creates them when drawing the figure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, of course.