|
25 | 25 | The base class for the Toolbar class of each interactive backend.
|
26 | 26 | """
|
27 | 27 |
|
28 |
| -from contextlib import contextmanager |
| 28 | +from contextlib import contextmanager, suppress |
29 | 29 | from enum import Enum, IntEnum
|
30 | 30 | import functools
|
31 | 31 | import importlib
|
@@ -709,6 +709,23 @@ def stop_filter(self, filter_func):
|
709 | 709 | Currently only supported by the agg renderer.
|
710 | 710 | """
|
711 | 711 |
|
| 712 | + def _draw_disabled(self): |
| 713 | + """ |
| 714 | + Context manager to temporary disable drawing. |
| 715 | +
|
| 716 | + This is used for getting the drawn size of Artists. This lets us |
| 717 | + run the draw process to update any Python state but does not pay the |
| 718 | + cost of the draw_XYZ calls on the canvas. |
| 719 | + """ |
| 720 | + no_ops = { |
| 721 | + meth_name: lambda *args, **kwargs: None |
| 722 | + for meth_name in dir(RendererBase) |
| 723 | + if (meth_name.startswith("draw_") |
| 724 | + or meth_name in ["open_group", "close_group"]) |
| 725 | + } |
| 726 | + |
| 727 | + return _setattr_cm(self, **no_ops) |
| 728 | + |
712 | 729 |
|
713 | 730 | class GraphicsContextBase:
|
714 | 731 | """An abstract base class that provides color, line styles, etc."""
|
@@ -2089,14 +2106,10 @@ def print_figure(
|
2089 | 2106 | functools.partial(
|
2090 | 2107 | print_method, orientation=orientation)
|
2091 | 2108 | )
|
2092 |
| - no_ops = { |
2093 |
| - meth_name: lambda *args, **kwargs: None |
2094 |
| - for meth_name in dir(RendererBase) |
2095 |
| - if (meth_name.startswith("draw_") |
2096 |
| - or meth_name in ["open_group", "close_group"]) |
2097 |
| - } |
2098 |
| - |
2099 |
| - with _setattr_cm(renderer, **no_ops): |
| 2109 | + ctx = (renderer._draw_disabled() |
| 2110 | + if hasattr(renderer, '_draw_disabled') |
| 2111 | + else suppress()) |
| 2112 | + with ctx: |
2100 | 2113 | self.figure.draw(renderer)
|
2101 | 2114 |
|
2102 | 2115 | bbox_inches = self.figure.get_tightbbox(
|
|
0 commit comments