Description
Bug summary
Matplotlib is no longer able to create gifs with a transparent background.
Code for reproduction
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = ax.plot([], [], 'ro')
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
savefig_kwargs = {
"facecolor": "none",
}
ani.save("test.gif", savefig_kwargs=savefig_kwargs)
Actual outcome
With Matplotlib version 3.8.0 (and also 3.7.3) installed this is the ouput:
A gif-file with a white background
Expected outcome
In 3.6.3 and before the file had a transparent background
Additional information
I tried many different setups, different values for facecolor (e.g. (1,1,1,1)), set "transparent": True and googeling quite a lot - old solutions on github won't work.
Like in the expected outcome stated, this worked in the past (until Matplotlib 3.6.3).
There either seems to be an error in the fix for another issue in #21831 - or this is now the new correct behavior, but it seems like the transparency should still work for gifs, as this was an issue with different formats.
Operating system
Windows 11
Matplotlib Version
3.8.0
Matplotlib Backend
TkAgg
Python version
Python 3.12.0
Jupyter version
No response
Installation
pip