Test that object gid appears in output svg.
()
| 242 | |
| 243 | |
| 244 | def test_gid(): |
| 245 | """Test that object gid appears in output svg.""" |
| 246 | from matplotlib.offsetbox import OffsetBox |
| 247 | from matplotlib.axis import Tick |
| 248 | |
| 249 | fig = plt.figure() |
| 250 | |
| 251 | ax1 = fig.add_subplot(131) |
| 252 | ax1.imshow([[1., 2.], [2., 3.]], aspect="auto") |
| 253 | ax1.scatter([1, 2, 3], [1, 2, 3], label="myscatter") |
| 254 | ax1.plot([2, 3, 1], label="myplot") |
| 255 | ax1.legend() |
| 256 | ax1a = ax1.twinx() |
| 257 | ax1a.bar([1, 2, 3], [1, 2, 3]) |
| 258 | |
| 259 | ax2 = fig.add_subplot(132, projection="polar") |
| 260 | ax2.plot([0, 1.5, 3], [1, 2, 3]) |
| 261 | |
| 262 | ax3 = fig.add_subplot(133, projection="3d") |
| 263 | ax3.plot([1, 2], [1, 2], [1, 2]) |
| 264 | |
| 265 | fig.canvas.draw() |
| 266 | |
| 267 | gdic = {} |
| 268 | for idx, obj in enumerate(fig.findobj(include_self=True)): |
| 269 | if obj.get_visible(): |
| 270 | gid = f"test123{obj.__class__.__name__}_{idx}" |
| 271 | gdic[gid] = obj |
| 272 | obj.set_gid(gid) |
| 273 | |
| 274 | with BytesIO() as fd: |
| 275 | fig.savefig(fd, format='svg') |
| 276 | buf = fd.getvalue().decode() |
| 277 | |
| 278 | def include(gid, obj): |
| 279 | # we need to exclude certain objects which will not appear in the svg |
| 280 | if isinstance(obj, OffsetBox): |
| 281 | return False |
| 282 | if isinstance(obj, Text): |
| 283 | if obj.get_text() == "": |
| 284 | return False |
| 285 | elif obj.axes is None: |
| 286 | return False |
| 287 | if isinstance(obj, plt.Line2D): |
| 288 | xdata, ydata = obj.get_data() |
| 289 | if len(xdata) == len(ydata) == 1: |
| 290 | return False |
| 291 | elif not hasattr(obj, "axes") or obj.axes is None: |
| 292 | return False |
| 293 | if isinstance(obj, Tick): |
| 294 | loc = obj.get_loc() |
| 295 | if loc == 0: |
| 296 | return False |
| 297 | vi = obj.get_view_interval() |
| 298 | if loc < min(vi) or loc > max(vi): |
| 299 | return False |
| 300 | return True |
| 301 |
nothing calls this directly
no test coverage detected
searching dependent graphs…