What is the correct way to update a matplotlib figure whenever new data is saved in a directory?

44 views
Skip to first unread message

Joshua Dorrington

unread,
Jan 2, 2019, 8:47:25 AM1/2/19
to watchdog-python
I am trying to produce a real-time animation of simulation output, so that every time data from a new timestep is saved in a specific directory, the plot is updated with the new data.
I have tried creating a custom watchdog.Handler class, that will create a figure, and update it whenever an on_created() function is triggered:

class PlottingHandler(PatternMatchingEventHandler):
       
   
def setup_plot(self,proj,cmap):
       
self.proj=proj
       
self.cmap=cmap
       
self.fig , _ =plt.subplots(1,1,figsize=(10,10))
       
self.ax = plt.axes(projection=proj)
        plt
.tight_layout()
        plt
.show()
       
   
def update_plot(self,file):
       
self.ax.cla()
        data
=iris.load_cube(file)
        iplt
.contourf(data, 30, cmap=self.cmap,fig=self.fig)
       
self.fig.canvas.draw()
       
self.fig.canvas.flush_events()
        plt
.pause(0.1)
       
   
def on_created(self,event):

 
       
self.update_plot(event.src_path)



  Then I have basically followed the watchdog quickstart guide:

 
 
 
#general modules
import numpy as np
import os
import sys
import time
import threading
#monitoring modules
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
#data load modules
from iris import load_cube, Constraint
import warnings
warnings
.simplefilter('ignore')
#plotting modules
import iris.plot as iplt
import matplotlib.pyplot as plt
import cartopy.crs as ccrs


path
="~/"  
color_map
= plt.get_cmap('inferno')
proj
= ccrs.Mollweide()


plt
.ion()
handler
=PlottingHandler()


handler
.setup_plot(color_map,proj)


observer
=Observer()

observer
.schedule(handler,path)



print("start")

observer
.start()


#Hopefully stops kernel from closing
while True:
   
1

This works when run in an interactive IDE environment, but when I run it from terminal, the "new file created" is printed correctly but the plot freezes up and never updates.
I imagine this is a threading issue, but I'm not very knowledgeable about that side of things, so any advice would be much appreciated.

Thanks!

Reply all
Reply to author
Forward
0 new messages