You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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]:
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'
This is just a post to document an attempt and the difficulties I ran into. Good for me to reference when I try again, or perhaps useful if anyone else takes a peek.
I tried to give (while crossing my fingers) _png.write_png a 32-Bit RGBA bitmap from a numpy array as an experiment:
%matplotlib nbagg
import pylab
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib import _png
import numpy as np
pylab.plot([1,2,3])
canvas = pylab.get_current_fig_manager().canvas
agg = canvas.switch_backends(FigureCanvasAgg)
agg.draw()
s = agg.buffer_rgba()
# get the width and the height to resize the matrix
l,b,w,h = agg.figure.bbox.bounds
w, h = int(w), int(h)
s = np.array(s.tolist()).reshape((w,h,4)) #Convert to RGBA to np.array
filename_or_obj = 'testwrite.png'
renderer = agg.renderer
with open(filename_or_obj, 'wb') as fn:
_png.write_png(memoryview(s), fn, agg.figure.dpi)
Get error: IndexError: Unexpected SeqBase<T> length.
I think it has to do with write_png wants s to have a array_view::converter_contiguous method:
Haven't figured a way around that yet. The goal is to then use numpy to flatten RGBA -> RGB to see if something like this would work for making the animations better by default.
EDIT: This error has been fixed. I was referencing github src code for write_png (which wants 2 or 3 arguments) where matplotlib 1.4.3 wanted 5 arguments for width and height. I guess this has changed since. Note, whoever changed the error message to Unexpected SeqBase<T> to write_png wnats at most 3 arguments... kodus 👍
Could you try setting blit=False It looks to me like the issue is that the frames are not blitted correctly and all frames but the first ones are have the text plotted multiple times.
I got the same results with blit=False. I also tried using writer=imagemagick_file to see what frames we are giving imagemagick and here are a few samples:
I used convert's +adjoin argument to deconstruct those frames from the gif and this is what they look like:
It looks like imagemagick really wants to keep the alpha channel (as shown in the first gif), but is then forced to decide between full alpha or no alpha which is what gives the pixelated appearance, and then for the later frames I guess it gives up on the whole alpha channel idea for some reason unknown to me.
I have written some quick code as a possible solution, but I ran into issues with animations in my git-installed version of matplotlib. I will need to figure this out, but here is something I was considering:
Activity
[-]Tansparency in Animations[/-][+]Transparency in Animations[/+]cbreeden commentedon Oct 29, 2015
This is just a post to document an attempt and the difficulties I ran into. Good for me to reference when I try again, or perhaps useful if anyone else takes a peek.
I tried to give (while crossing my fingers)
_png.write_pnga 32-Bit RGBA bitmap from a numpy array as an experiment:Get error:
IndexError: Unexpected SeqBase<T> length.I think it has to do with
write_pngwantssto have aarray_view::converter_contiguousmethod:matplotlib/src/_png.cpp
Line 82 in c32bde5
Haven't figured a way around that yet. The goal is to then use numpy to flatten RGBA -> RGB to see if something like this would work for making the animations better by default.
EDIT: This error has been fixed. I was referencing github src code for write_png (which wants 2 or 3 arguments) where matplotlib 1.4.3 wanted 5 arguments for width and height. I guess this has changed since. Note, whoever changed the error message to
Unexpected SeqBase<T>towrite_png wnats at most 3 arguments... kodus 👍jenshnielsen commentedon Oct 29, 2015
Could you try setting
blit=FalseIt looks to me like the issue is that the frames are not blitted correctly and all frames but the first ones are have the text plotted multiple times.cbreeden commentedon Oct 30, 2015
I got the same results with
blit=False. I also tried usingwriter=imagemagick_fileto see what frames we are giving imagemagick and here are a few samples:I used convert's +adjoin argument to deconstruct those frames from the gif and this is what they look like:
It looks like imagemagick really wants to keep the alpha channel (as shown in the first gif), but is then forced to decide between full alpha or no alpha which is what gives the pixelated appearance, and then for the later frames I guess it gives up on the whole alpha channel idea for some reason unknown to me.
cbreeden commentedon Oct 30, 2015
Issue update:
I have written some quick code as a possible solution, but I ran into issues with animations in my git-installed version of matplotlib. I will need to figure this out, but here is something I was considering:
This would add an
flattenkwarg in savefig that can be called when animation.py when it wants it.jklymak commentedon Aug 16, 2018
Closing, as I don't see the issue with current nbagg and master matplotlib. OTOH, feel free to reopen if I'm in error.