Get the renderer that would be used to save a `.Figure`. If you need a renderer without any active draw methods use renderer._draw_disabled to temporary patch them out at your call site.
(figure, print_method=None)
| 1595 | |
| 1596 | |
| 1597 | def _get_renderer(figure, print_method=None): |
| 1598 | """ |
| 1599 | Get the renderer that would be used to save a `.Figure`. |
| 1600 | |
| 1601 | If you need a renderer without any active draw methods use |
| 1602 | renderer._draw_disabled to temporary patch them out at your call site. |
| 1603 | """ |
| 1604 | # This is implemented by triggering a draw, then immediately jumping out of |
| 1605 | # Figure.draw() by raising an exception. |
| 1606 | |
| 1607 | class Done(Exception): |
| 1608 | pass |
| 1609 | |
| 1610 | def _draw(renderer): raise Done(renderer) |
| 1611 | |
| 1612 | with cbook._setattr_cm(figure, draw=_draw), ExitStack() as stack: |
| 1613 | if print_method is None: |
| 1614 | fmt = figure.canvas.get_default_filetype() |
| 1615 | # Even for a canvas' default output type, a canvas switch may be |
| 1616 | # needed, e.g. for FigureCanvasBase. |
| 1617 | print_method = stack.enter_context( |
| 1618 | figure.canvas._switch_canvas_and_return_print_method(fmt)) |
| 1619 | try: |
| 1620 | print_method(io.BytesIO()) |
| 1621 | except Done as exc: |
| 1622 | renderer, = exc.args |
| 1623 | return renderer |
| 1624 | else: |
| 1625 | raise RuntimeError(f"{print_method} did not call Figure.draw, so " |
| 1626 | f"no renderer is available") |
| 1627 | |
| 1628 | |
| 1629 | def _no_output_draw(figure): |
no test coverage detected
searching dependent graphs…