Init of FuncAnimation does not take any new data

Hi there,

I’m using Matplotlib for plotting my Solarpower data. I have some data and every 10 minutes the script will retrieve new data and add it to the graph. I’m using FuncAnimation to update the graph. A slow animation of 10 minutes is ok. My problem is that data is updated FIRST after 10 minutes. Before that moment the graph is empty.

FuncAnimation is using an init function and a update function. The first data is displayed after 10 minutes when the first update function is called. The init function, which is called twice by the way, could be used to present the first data that is available. That would be an easy idea! However, this is not working.

line.set_data(xdata, ydata) is called from the init function, xdata, ydata is present but nothing is happening.

This is my FuncAnimation:

ani = FuncAnimation(fig, Update_Graph, xdata, init_func=Init_Graph, blit=True, interval=UPDATE_INT, repeat_delay=REP_DEL, repeat=False)

This is my update function:

def Update_Graph(frame):

    global targetdate
    global graphmode

    if graphmode == "follow":
        targetdate = TodayStr()
        
    # Get todays data from file
    legend,dataarray = ParseDate(targetdate)
    if not dataarray:
        print(targetdate, "is not available in the data set.\n")
        return 0
    
    xdata, ydata = GetPowerDataTimeSeries(dataarray)        # new data

    line.set_data(xdata, ydata)

    return line,

This is the init function:

def Init_Graph():

    # Get todays data from file
    legend,dataarray = ParseDate(targetdate)
    if not dataarray:
        print(targetdate, "is not available in the data set.\n")
        return 0
    
    npstarttime, npendtime = FirstLastDayNPstr(dataarray)
    xdata, ydata = GetPowerDataTimeSeries(dataarray) 

    ax.set_xlim(np.datetime64(npstarttime), np.datetime64(npendtime))
    ax.set_ylim(0, 3300)

    # set collected data
    line.set_data(xdata, ydata)

    return line,

I have tried calling Update_Graph before the graph is displayed hoping that it would take the data but this is not the case…
There is something interesting happening that might explain why this is not working.
I log all steps to logfile to see what is happening. (logging steps are removed above)
In the the init function I set xlim with:

ax.set_xlim(np.datetime64(npstarttime), np.datetime64(npendtime))

Right after this I read the xlim and writ it to file:


    if DEBUG > 1:
        xmin, xmax = ax.get_xlim()     # xlim is NOT set! It will get date of 1970
        ymin, ymax = ax.get_ylim()
        ts = datetime.datetime.fromtimestamp(xmin).strftime('%Y-%m-%d %H:%M:%S')
        flog.write("  setting from date:" + ts + "\n")
        ts = datetime.datetime.fromtimestamp(xmax).strftime('%Y-%m-%d %H:%M:%S')
        flog.write("  setting to date:" + ts + "\n")
        
        flog.write("  setting ylim from-to: " + str(ymin) + " - " + str(ymax) + "\n")
        flog.flush()

This is what it reads:

setting from date:1970-01-01 06:12:28
setting to date:1970-01-01 06:12:28

So, setting the xlim in init is totally pointless.
If anyone could help me please.
Thanks already in advance.

Hi there, anyone some updates?

Without knowing how you created line and what the contents of npstarttime are, its hard to help. fromtimestamp will also not work with matplotlib date numbers. Use mdates.num2date if you need a datetime.

Both code parts for Update_Graph and Init_Graph are working and the graph is updated. Except that it is updated after the first interval has expired. UPDATE_INT is now set to 10000 (10 seconds). That is slow but absolutely fine if I could run it just once in Init_Graph. That is not working… I’m now experimenting with interval=1 and set it in the first update to 10000. That would basically do what I want. But i don’t know how to change it dynamically.