|
1 | 1 | import numpy as np |
2 | 2 | from io import BytesIO |
3 | | -import os |
4 | 3 | import re |
5 | 4 | import tempfile |
6 | 5 | import warnings |
@@ -145,3 +144,67 @@ def test_svgnone_with_data_coordinates(): |
145 | 144 | buf = fd.read().decode() |
146 | 145 |
|
147 | 146 | assert expected in buf |
| 147 | + |
| 148 | + |
| 149 | +def test_gid(): |
| 150 | + """Test that object gid appears in output svg.""" |
| 151 | + from matplotlib.offsetbox import OffsetBox |
| 152 | + from matplotlib.axis import Tick |
| 153 | + |
| 154 | + fig = plt.figure() |
| 155 | + |
| 156 | + ax1 = fig.add_subplot(131) |
| 157 | + ax1.imshow([[1., 2.], [2., 3.]], aspect="auto") |
| 158 | + ax1.scatter([1, 2, 3], [1, 2, 3], label="myscatter") |
| 159 | + ax1.plot([2, 3, 1], label="myplot") |
| 160 | + ax1.legend() |
| 161 | + ax1a = ax1.twinx() |
| 162 | + ax1a.bar([1, 2, 3], [1, 2, 3]) |
| 163 | + |
| 164 | + ax2 = fig.add_subplot(132, projection="polar") |
| 165 | + ax2.plot([0, 1.5, 3], [1, 2, 3]) |
| 166 | + |
| 167 | + ax3 = fig.add_subplot(133, projection="3d") |
| 168 | + ax3.plot([1, 2], [1, 2], [1, 2]) |
| 169 | + |
| 170 | + fig.canvas.draw() |
| 171 | + |
| 172 | + gdic = {} |
| 173 | + for idx, obj in enumerate(fig.findobj(include_self=True)): |
| 174 | + if obj.get_visible(): |
| 175 | + gid = f"test123{obj.__class__.__name__}_{idx}" |
| 176 | + gdic[gid] = obj |
| 177 | + obj.set_gid(gid) |
| 178 | + |
| 179 | + fd = BytesIO() |
| 180 | + fig.savefig(fd, format='svg') |
| 181 | + fd.seek(0) |
| 182 | + buf = fd.read().decode() |
| 183 | + fd.close() |
| 184 | + |
| 185 | + def include(gid, obj): |
| 186 | + # we need to exclude certain objects which will not appear in the svg |
| 187 | + if isinstance(obj, OffsetBox): |
| 188 | + return False |
| 189 | + if isinstance(obj, plt.Text): |
| 190 | + if obj.get_text() == "": |
| 191 | + return False |
| 192 | + elif obj.axes is None: |
| 193 | + return False |
| 194 | + if isinstance(obj, plt.Line2D): |
| 195 | + if np.array(obj.get_data()).shape == (2, 1): |
| 196 | + return False |
| 197 | + elif not hasattr(obj, "axes") or obj.axes is None: |
| 198 | + return False |
| 199 | + if isinstance(obj, Tick): |
| 200 | + loc = obj.get_loc() |
| 201 | + if loc == 0: |
| 202 | + return False |
| 203 | + vi = obj.get_view_interval() |
| 204 | + if loc < min(vi) or loc > max(vi): |
| 205 | + return False |
| 206 | + return True |
| 207 | + |
| 208 | + for gid, obj in gdic.items(): |
| 209 | + if include(gid, obj): |
| 210 | + assert gid in buf |
0 commit comments