Matplotlib animation with blit — how to update plot title?

℡╲_俬逩灬. 提交于 2019-12-23 01:44:16

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/39644461/matplotlib-animation-with-blit-how-to-update-plot-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!