1

I need to increase FPS of matplotlib FuncAnimation. Blitting is one way but it destroys the figure styles and customizations. How do I preserve the style while applying blit ?

I am trying to animate time series data. I developed a class to do just that using matplotlib but the fps was horrible. I then tried to plot multiple datapoints at once in a frame so as to speed up the rendering, it was better but the results were yet again insuffucient. Finally I read about using blit in FuncAnimation and the plot was drawing blazing fast, but all of my styling disappeared when I turned blit on. I am new to matplotlib and already spent days in debugging everything but this issue is undecipherable. If anyone can find the issue/ suggest other methods to improve the fps, I will be very grateful.

Full Code - https://github.com/namajain/EEGanalysis/blob/master/src/viz/emdVizWithBlit.py

I have also added a trimmed down code in the post to reproduce how the figure is all black instead of the light blue color and the seaborn style

import pandas import matplotlib.animation import matplotlib.pyplot as plt

class emdVizBlit():

def __init__(self, rd=None, csv=None, ncomp=5, fplot=20, xran=128, fa=.8):

    self.rd=[1]*100000
    self.nComp = ncomp
    self.nPoints = fplot
    self.xRange = xran
    self.cMap = plt.get_cmap('inferno').colors
    self.freqAdjust = fa

def emdPlot(self):
    fig, ax = plt.subplots(nrows=self.nComp + 2, sharex=True, sharey=True)
    fig.set_facecolor('xkcd:light blue')
    fig.set_size_inches(19.3, 10.91)
    mng = plt.get_current_fig_manager()
    mng.window.showMaximized()
    fig.tight_layout()
    plt.style.use('seaborn-whitegrid')

    def getColor(j):
        return self.cMap[(len(self.cMap) - 1) // (self.nComp + 2) * (j)]

    def getLine(axe):

        ln, = axe.plot([], [], color=getColor(0), animated=True)
        return ln

    lines = [getLine(axe) for axe in ax]
    anim_running = True

    def absanimate(i):
        nonlocal ax

        # if isRedrawIter(i):
        # redrawPlotOutline(i)

        # plotBaseSignal(i)
        # plotImfSignal(i)
        # plotResidueSignal(i)
        return lines


    anim = matplotlib.animation.FuncAnimation(fig, absanimate, frames=len(self.rd) // self.nPoints,
                                              interval=self.nPoints / 256 * self.freqAdjust,
                                              repeat=False, blit=True)
    plt.show()


if __name__ == '__main__':
    ev = emdVizBlit()
    ev.emdPlot()

Expected

Image of emdViz

What actually appears is all the blue area turned black, with the axes appearing correctly enter image description here

Naman Jain
  • 63
  • 1
  • 9
  • Interesting. I cannot reproduce; when I run the code it gives me the expected light-blue background with all axes ticks visible. Can you state (1) the operating system, (2) the matplotlib version in use, (3) the matplotlib backend in use? I suspect there is a problem with the interval beeing to short. It is set to below 1 millisecond, which matplotlib isn't capable of achieving anyways. So does it work it you set e.g. `interval=10`? – ImportanceOfBeingErnest Feb 19 '19 at 14:20
  • Hi @ImportanceOfBeingErnest , its not working with the interval 10 also, matplotlib is 2.2.3 with qt5agg backend on Win 10 and python 3.7 – Naman Jain Feb 19 '19 at 14:39
  • Ok, I can reproduce with matplotlib 2.2.3. I'm not sure if there has been a bugfix in between though. But you can still try with matplotlib 3.0.2, which works fine for me. – ImportanceOfBeingErnest Feb 19 '19 at 14:41
  • @ImportanceOfBeingErnest, thanks, it started displaying properly after I updated to version 3 ! – Naman Jain Feb 19 '19 at 14:52

0 Answers0