I'm running a gradient descent algorithm on some geo data. The goal is to assign different areas to different clusters to minimize some objective function. I am trying to make a short movie showing how the algorithm progresses. Right now my approach is to plot the map at each step, then use some other tools to make a little movie from all the static images (pretty simple). But, I have about 3000 areas to plot and the plot command takes a good 90 seconds or more to run, which kills my algorithm.
There are some obvious shortcuts: save images every Nth iteration, save all the steps in a list and make all the images at the end (perhaps in parallel). That's all fine for now, but ultimately I'm aiming for some interactive functionality where a user can put in some parameters and see their map converge in real time. Seems like updating the map on the fly would be best in that case.
Any ideas? Here's the basic command (using the latest dev version of geopandas)
fig, ax = plt.subplots(1,1, figsize=(7,5))
geo_data.plot(column='cluster',ax=ax, cmap='gist_rainbow',linewidth=0)
fig.savefig(filename, bbox_inches='tight', dpi=400)
Also tried something akin to the following (an abbreviated version is below). I open a single plot, and change it and save it with each iteration. Doesn't seem to speed things up at all.
fig, ax = plt.subplots(1,1, figsize=(7,5))
plot = geo_data.plot(ax=ax)
for iter in range(100): #just doing 100 iterations now
clusters = get_clusters(...)
for i_d, district in enumerate(plot.patches):
if cluster[i] == 1
district.set_color("#FF0000")
else:
district.set_color("#d3d3d3")
fig.savefig('test'+str(iter)+'.pdf')
update: taken a look at drawnow and other pointers from real-time plotting in while loop with matplotlib, but shapefiles seems to be too big/clunky to work in real time.