3

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.

3
  • You can use title.set_text("Second title") to update the title. Commented Sep 22, 2016 at 17:28
  • @Ed Smith Unfortunately title.set_text("Second title") does not update the title, probably the title text artist should be redrawn? Commented Sep 23, 2016 at 15:15
  • Hi @Eugen Epure, you do need to redraw, I've added an answer with minimal example to show this Commented Sep 23, 2016 at 15:37

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

The key is to set the title as animated:

title = ax.set_title("First title", animated = True)

Then when updating the plot:

title.set_text("Second title")       
ax.draw_artist(title)
canvas.blit(ax.bbox)
canvas.flush_events()

Comments

-2

The following draws the plot and allows you to make changes,

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(1,1)
ax.plot(np.linspace(0.,10.,100))
title = ax.set_title("First title")
plt.show(block=False)

The title can then be updated using,

title.set_text("Second title")
plt.draw()

2 Comments

Thanks! Of course this works, but plt.draw() is very inefficient. I try to copy the background and use canvas.blit(ax.bbox) (this updates the plot at least 10-20 times faster), the problem is the title is not updated in this case, even if I call ax.draw_artist(title) .
Hi @Eugen Epure, I'd guess blit doesn't assume the title will be change. It may be better to use text with title = plt.text(5,11,'Second title') to specify the title manually and then update this. I would think blitting works with text objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.