|
1 | 1 | import functools |
| 2 | +import sys |
2 | 3 | import traceback |
| 4 | +from itertools import chain |
3 | 5 | from typing import Dict, Tuple, List |
4 | 6 |
|
5 | 7 | import numpy as np |
6 | | -import sys |
7 | | -from itertools import chain |
8 | 8 | from matplotlib import patheffects |
9 | 9 | from matplotlib.axes import Axes |
10 | 10 | from matplotlib.backend_bases import MouseButton |
@@ -200,17 +200,24 @@ def _repr_mimebundle_(self, *args, **kwargs): |
200 | 200 | if not InteractiveShell.initialized(): |
201 | 201 | return {"text/plain": repr(self)}, {} |
202 | 202 |
|
| 203 | + # larger graphs (see eg `tutorial/4 layouts.ipynb`) sometimes render blank |
| 204 | + # ensure they are drawn first and spinning the event loop for some time makes that less likely |
| 205 | + self.ax.figure.canvas.draw() |
| 206 | + self.ax.figure.canvas.start_event_loop(0.1) |
| 207 | + |
203 | 208 | fmt = InteractiveShell.instance().display_formatter |
| 209 | + # if ipython_display_formatter is enabled, format(figure.canvas) immediately displays and returns None |
204 | 210 | display_en = fmt.ipython_display_formatter.enabled |
205 | 211 | fmt.ipython_display_formatter.enabled = False |
206 | 212 | try: |
| 213 | + # for interactive backends, we want to render the interactive figure.canvas widget |
207 | 214 | data, meta = fmt.format(self.ax.figure.canvas, *args, **kwargs) |
208 | | - # print("canvas", list(data.keys()) if data else "none") |
| 215 | + # for non-interactive backends, this only renders the text/plain repr() |
209 | 216 | if list(data.keys()) != ["text/plain"] and data: |
210 | 217 | return data, meta |
211 | 218 | else: |
| 219 | + # so we render the figure instead, which gives a static image |
212 | 220 | data, meta = fmt.format(self.ax.figure, *args, **kwargs) |
213 | | - # print("figure", list(data.keys()) if data else "none") |
214 | 221 | return data, meta |
215 | 222 | finally: |
216 | 223 | fmt.ipython_display_formatter.enabled = display_en |
|
0 commit comments