matplotlib save Animation with limitless data to file

Viewed 20

So, I've got a program that monitors another program and generates a live graph using matplotlib.

The code looks roughly like this (many details omitted, it's a fairly simple program):

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


fig = ...

def animate(_):
    # Get new data
    # plot new data

ani = animation.FuncAnimation(fig, animate, ...)

plt.show()

When I run the program, I get a live graph of the data displayed to me until I exit the program.

What I would like is when I exit the program, is for the graph I was looking while the program was running to be saved to a file so I can look at it again later.

The problem is, all examples I've seen so far expect a fixed amount of data to be used for the saved plot.

When I try to add a call like ani.save(...) below where I create the animation, it hangs endlessly (since it's trying to save all the data to a file). Then, when I exit, nothing is saved to a file (since I interrupted the writing I assume).

Is there a good clean way to get the plot saved to a file? Besides the "brute force" way of just saving all the data as I go and "recreating" the plot at the end right before I exit.

Ideally, I would like to still be able to see the plot as I go, and have it saved to a file once I exit the program.

0 Answers
Related