我想更新我的2D图中的y数据,而不必每次都调用'plot'
from matplotlib.figure import Figure
fig = Figure(figsize=(12,8), dpi=100)
for num in range(500):
if num == 0:
fig1 = fig.add_subplot(111)
fig1.plot(x_data, y_data)
fig1.set_title("Some Plot")
fig1.set_ylabel("Amplitude")
fig1.set_xlabel("Time")
else:
#fig1 clear y data
#Put here something like fig1.set_ydata(new_y_data), except that fig1 doesnt have set_ydata attribute`
我可以清理并绘制500次,但它会减慢循环速度。还有其他选择吗?
答案 0 :(得分:1)
有关mpl figure部分的描述,请参阅http://matplotlib.org/faq/usage_faq.html#parts-of-a-figure。
如果您正在尝试制作动画,请查看matplotlib.animation
模块,该模块会为您处理大部分细节。
您正在直接创建Figure
对象,因此我假设您知道自己在做什么,并且正在处理其他地方的画布创建,但是对于此示例,将使用pyplot界面来创建数字/轴
import matplotlib.pyplot as plt
# get the figure and axes objects, pyplot take care of the cavas creation
fig, ax = plt.subplots(1, 1) # <- change this line to get your axes object differently
# get a line artist, the comma matters
ln, = ax.plot([], [])
# set the axes labels
ax.set_title('title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
# loop over something that yields data
for x, y in data_source_iterator:
# set the data on the line artist
ln.set_data(x, y)
# force the canvas to redraw
ax.figure.canvas.draw() # <- drop this line if something else manages re-drawing
# pause to make sure the gui has a chance to re-draw the screen
plt.pause(.1) # <-. drop this line to not pause your gui