8000 Transparency in Animations · Issue #5335 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Transparency in Animations #5335
Closed
Closed
@cbreeden

Description

@cbreeden

This report is somewhat of an extension to #5302. It seems easy to miss handle transparent regions while working with nbagg. The following is an example taken from the animation examples:

%matplotlib nbagg

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

def update_line(num, data, line):
    line.set_data(data[...,:num])
    return line,

fig = plt.figure()

plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')

data = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')

ani = animation.FuncAnimation(fig, update_line, 25, fargs=(data, l),
    interval=50, blit=True)

ani.save('im.gif', writer='imagemagick', fps=8)
ani.save('im.mp4', extra_args=['-vcodec', 'libx264'])

The output produced is:

im

As you can see, there is again some issues with the rendering of text in the figure (not axes) area where nbagg has set transparency to true. The im.mp4 file looks nearly identical to the gif.

I believe matplotlib saves each frame as a png and then sends those pngs to be saved as an animation through convert (imagemagick) or ffmpeg. I believe the issue here is that while gifs properly support transparency, they only support either full alpha or no alpha, which forces imagemagick to decide between alpha or no alpha for each intermediate alpha which gives a pixelated appearance.

If we insert the code [for some reason with plt.rc_context({'nbagg.transparent': False}) doesn't work here]:

import matplotlib
matplotlib.rcParams['nbagg.transparent'] = False

Then we will get this:

im2

Perhaps we can just disable transparency for gif files all together (I don't think matplotlib can properly handle boolean alpha levels), and then set the default frame format to gif files for animations; animation.frame_format: 'gif'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0