Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically update a matplotlib table cell text

My code reads two files of numbers and plots them dynamically in a plot window on the upper part of the window. I also put a table below the plot. I want to take the latest values as they are plotted and update cells in the table with those numbers. The table gets updated on the screen the first time that the "set_text" is called, but what is displayed on the screen doesn't keep changing, even though the table is being updated. What do I need to add to get the table to display the value as it updates it.

<snip>
def update_line(num, sdata, line1, d2data, line2, my_table):
    for i in range(0, num):
        line1.set_data(sdata[0, :num], sdata[1,:num])
        line2.set_data(d2data[0, :num], d2data[1,:num])
    tm.sleep(0.1)
    tmp = sdata[1, num]
    my_table._cells[(1, 1)]._text.set_text(tmp)
    return line1,line2, my_table,

line_ani = animation.FuncAnimation(fig, update_line, 149, fargs=(d2data, f, d1data,l, the_table), interval=1, blit=True, repeat=False)

plt.show()

This shows the resulting figure. Note that the red/blue lines animate and draw over several seconds

like image 569
user3597723 Avatar asked May 02 '14 21:05

user3597723


People also ask

What is BBOX in Matplotlib?

BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.

How do you dynamically update a plot in a loop in IPython notebook?

Try to add show() or gcf(). show() after the plot() function. These will force the current figure to update (gcf() returns a reference for the current figure).

Why is %Matplotlib inline?

The line magic command %matplotlib inline enables the drawing of matplotlib figures in the IPython environment. Once this command is executed in any cell, then for the rest of the session, the matplotlib plots will appear directly below the cell in which the plot function was called.


1 Answers

In principle the line my_table._cells[(1, 1)]._text.set_text(tmp) should update the table cell data. The reason it is not working here, is that blitting is used. It may help to just turn off blitting in the call to FuncAnimation: blit=False. If that doesn't solve the issue, one needs to do the animation manually using fig.canvas.draw() inside a loop.

Note that one should better not be using "private" attributes. Hence I would recommend

my_table.get_celld()[(1, 1)].get_text().set_text(tmp)
like image 122
ImportanceOfBeingErnest Avatar answered Oct 29 '22 18:10

ImportanceOfBeingErnest