Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib animation with blit -- how to update plot title?

I use matplotlib to animate a plot, by copying the background and blitting:

f = Figure(tight_layout=True)
canvas = FigureCanvasTkAgg(f, master=pframe)
canvas.get_tk_widget().pack()
ax = f.add_subplot(111)
# Set inial plot title
title = ax.set_title("First title")
canvas.show()
# Capture the background of the figure
background = canvas.copy_from_bbox(ax.bbox)
line, = ax.plot(x, y)
canvas._tkcanvas.pack()

Periodically I update the plot:

# How to update the title here?
line.set_ydata(new_data)
ax.draw_artist(line)
canvas.blit(ax.bbox)

How could I update -- as efficiently as possible, the plot title every time I update the plot?

Edit:

title.set_text("New title")
ax.draw_artist(title)

either before or after

canvas.blit(ax.bbox)

does not update the title. I think somehow I should redraw the title artist, or I should only capture the graph, as blit(ax.bbox) overwrites the entire title plot area including the title.

like image 575
Eugen Epure Avatar asked Aug 04 '26 08:08

Eugen Epure


1 Answers

Swooping in years later to say that I too was stuck on this problem for the last few days, and disabling blit wasn't an option for me (as the fps would become way too slow). For matplotlib 3.1.3 at least, if you send a resize event to the canvas, it properly flushes the background and regenerates it with any updates. So you can work around this by detecting when you need to update the title, and then calling fig.canvas.resize_event() to force a flush. Hope this helps future people!

like image 70
bream Avatar answered Aug 05 '26 22:08

bream



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!