|
28 | 28 | The base class for the messaging area. |
29 | 29 | """ |
30 | 30 |
|
31 | | -from contextlib import contextmanager |
| 31 | +from contextlib import contextmanager, ExitStack |
32 | 32 | from enum import IntEnum |
33 | 33 | import functools |
34 | 34 | import importlib |
@@ -1983,11 +1983,13 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, |
1983 | 1983 | dpi : float, default: :rc:`savefig.dpi` |
1984 | 1984 | The dots per inch to save the figure in. |
1985 | 1985 |
|
1986 | | - facecolor : color, default: :rc:`savefig.facecolor` |
1987 | | - The facecolor of the figure. |
| 1986 | + facecolor : color or 'auto', default: :rc:`savefig.facecolor` |
| 1987 | + The facecolor of the figure. If 'auto', use the current figure |
| 1988 | + facecolor. |
1988 | 1989 |
|
1989 | | - edgecolor : color, default: :rc:`savefig.edgecolor` |
1990 | | - The edgecolor of the figure. |
| 1990 | + edgecolor : color or 'auto', default: :rc:`savefig.edgecolor` |
| 1991 | + The edgecolor of the figure. If 'auto', use the current figure |
| 1992 | + edgecolor. |
1991 | 1993 |
|
1992 | 1994 | format : str, optional |
1993 | 1995 | Force a specific file format. If not given, the format is inferred |
@@ -2036,27 +2038,29 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, |
2036 | 2038 | if dpi == 'figure': |
2037 | 2039 | dpi = getattr(self.figure, '_original_dpi', self.figure.dpi) |
2038 | 2040 |
|
2039 | | - # Remove the figure manager, if any, to avoid resizing the GUI widget. |
2040 | | - # Some code (e.g. Figure.show) differentiates between having *no* |
2041 | | - # manager and a *None* manager, which should be fixed at some point, |
2042 | | - # but this should be fine. |
2043 | | - with cbook._setattr_cm(self, _is_saving=True, manager=None), \ |
2044 | | - cbook._setattr_cm(self.figure, dpi=dpi): |
2045 | | - |
| 2041 | + with ExitStack() as stack: |
| 2042 | + # Remove the figure manager, if any, to avoid resizing the GUI |
| 2043 | + # widget. Some code (e.g. Figure.show) differentiates between |
| 2044 | + # having *no* manager and a *None* manager, which should be fixed |
| 2045 | + # at some point, but this should be fine. |
| 2046 | + stack.enter_context( |
| 2047 | + cbook._setattr_cm(self, _is_saving=True, manager=None)) |
| 2048 | + stack.enter_context(cbook._setattr_cm(self.figure, dpi=dpi)) |
2046 | 2049 | if facecolor is None: |
2047 | 2050 | facecolor = rcParams['savefig.facecolor'] |
| 2051 | + if not cbook._str_equal(facecolor, 'auto'): |
| 2052 | + stack.callback( |
| 2053 | + self.figure.set_facecolor, self.figure.get_facecolor()) |
| 2054 | + self.figure.set_facecolor(facecolor) |
2048 | 2055 | if edgecolor is None: |
2049 | 2056 | edgecolor = rcParams['savefig.edgecolor'] |
2050 | | - |
2051 | | - origfacecolor = self.figure.get_facecolor() |
2052 | | - origedgecolor = self.figure.get_edgecolor() |
2053 | | - |
2054 | | - self.figure.set_facecolor(facecolor) |
2055 | | - self.figure.set_edgecolor(edgecolor) |
| 2057 | + if not cbook._str_equal(edgecolor, 'auto'): |
| 2058 | + stack.callback( |
| 2059 | + self.figure.set_edgecolor, self.figure.get_edgecolor()) |
| 2060 | + self.figure.set_edgecolor(edgecolor) |
2056 | 2061 |
|
2057 | 2062 | if bbox_inches is None: |
2058 | 2063 | bbox_inches = rcParams['savefig.bbox'] |
2059 | | - |
2060 | 2064 | if bbox_inches: |
2061 | 2065 | if bbox_inches == "tight": |
2062 | 2066 | renderer = _get_renderer( |
@@ -2094,8 +2098,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, |
2094 | 2098 | if bbox_inches and restore_bbox: |
2095 | 2099 | restore_bbox() |
2096 | 2100 |
|
2097 | | - self.figure.set_facecolor(origfacecolor) |
2098 | | - self.figure.set_edgecolor(origedgecolor) |
2099 | 2101 | self.figure.set_canvas(self) |
2100 | 2102 | return result |
2101 | 2103 |
|
|
0 commit comments