0

I am generating a live plot using matplotlib's funcAnimation function such as in the example below. To be clear I am plotting data that is dynamically updating in real-time and I want to view these updates in the browser rather than in its own local plotting window.

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

def animate(i):
    plt.clf()

    #generate random data
    x = np.array([i for i in range(100)])
    y = np.random.normal(loc=0,scale=1, size = 100)
    
    plt.scatter(x,y)

def main():
    fig = plt.figure()
    ani = animation.FuncAnimation(fig, animate, interval=1000)
    ani.save('animation.gif', fps=10)
    plt.show()
    #mpld3.show()

if __name__ == '__main__':
    main()

It opens the plot in its own little window. enter image description here

I was wondering what is the best/quickest way to open this in browser instead? I tried using mpld3 as recommended in some places (see the commented bits of code in my code snippet), but all I get is a static page that does not update. I would like the plot to be redrawn in the browser during each redraw done by the animate fuction as it does when the plot is generated in it's own window instead as in the first image.

enter image description here

Thanks in advance for any help anyone has to offer!

wrahman
  • 123
  • 1
  • 10
  • It sounds like you may need `Bokeh`: https://stackoverflow.com/questions/53472164/matplotlib-animation-in-browser – Joshua Voskamp Nov 12 '21 at 17:51
  • I saw that comment before posting. Unfortunately based on the links and tutorials, to use Bokeh I will need to abandon my matplotlib based plotting script and use their own plotting scripts for stuff like scatter plots. My matplotlib based plotting library is extensive and it is not feasible for me to translate all of it to use their plotting tools instead. There doesn't appear to be much in the way of something that will take a matplotlib figure handle and making it bokeh compatible while maintaining the liveplot feature I am after. – wrahman Nov 12 '21 at 18:20
  • https://matplotlib.org/3.1.0/gallery/user_interfaces/embedding_in_tk_sgskip.html – pippo1980 Dec 23 '21 at 16:59

0 Answers0