Skip to content

Commit 100d004

Browse files
committed
FIX: pre-composite animation frames to white background
This fixes issues with encoding that do not support transparency and when the Figure facecolor is transparent. Closes matplotlib#19040
1 parent 007c4c1 commit 100d004

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/matplotlib/animation.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from matplotlib._animation_data import (
4040
DISPLAY_TEMPLATE, INCLUDED_FRAMES, JS_INCLUDE, STYLE_INCLUDE)
4141
from matplotlib import _api, cbook
42-
42+
import matplotlib.colors as mcolors
4343

4444
_log = logging.getLogger(__name__)
4545

@@ -1002,6 +1002,9 @@ def func(current_frame: int, total_frames: int) -> Any
10021002

10031003
if savefig_kwargs is None:
10041004
savefig_kwargs = {}
1005+
else:
1006+
# we are going to mutate this below
1007+
savefig_kwargs = dict(savefig_kwargs)
10051008

10061009
if fps is None and hasattr(self, '_interval'):
10071010
# Convert interval in ms to frames per second
@@ -1057,6 +1060,18 @@ def func(current_frame: int, total_frames: int) -> Any
10571060
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
10581061
"frame size to vary, which is inappropriate for "
10591062
"animation.")
1063+
1064+
facecolor = savefig_kwargs.get('facecolor',
1065+
mpl.rcParams['savefig.facecolor'])
1066+
if facecolor == 'auto':
1067+
facecolor = self._fig.get_facecolor()
1068+
1069+
def _pre_composite_to_white(color):
1070+
r, g, b, a = mcolors.to_rgba(color)
1071+
return a * np.array([r, g, b]) + 1 - a
1072+
1073+
savefig_kwargs['facecolor'] = _pre_composite_to_white(facecolor)
1074+
savefig_kwargs['transparent'] = False # just to be safe!
10601075
# canvas._is_saving = True makes the draw_event animation-starting
10611076
# callback a no-op; canvas.manager = None prevents resizing the GUI
10621077
# widget (both are likewise done in savefig()).

0 commit comments

Comments
 (0)