使用python中的matplotlib.figure更新打印数据

使用python中的matplotlib.figure更新打印数据,matplotlib,plot,Matplotlib,Plot,我想在我的2D绘图中更新y数据,而不必每次调用“绘图” 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

我想在我的2D绘图中更新y数据,而不必每次调用“绘图”

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次,但这会减慢循环。任何其他替代方案?

有关mpl图各部分的说明,请参阅

如果要创建动画,请查看
matplotlib.animation
模块,该模块为您提供了大部分细节

您正在直接创建
图形
对象,因此我假设您知道自己在做什么,并且在其他地方负责画布的创建,但在本例中,将使用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
导入matplotlib.pyplot作为plt
#获取figure和axes对象,pyplot负责创建cavas

图,ax=plt。子图(1,1)#也可以看到我在这里给出的示例代码是一个大型代码的一部分,它使用matplotlib.figure和tkinter画布进行几种不同类型的绘图。我正在使用matplotlib.figure搜索解决方案,这样我就不必编辑整个代码了。我正在尝试绘制实时数据,而500次“绘图”迭代会减慢循环速度。是的,这正是问题所在,更改一行并删除2,它应该放在任何地方。它可以工作。小问题,如果它在y轴上的多个数据集用于单个绘图,例如(x,y1)(x,y2)(x,y3),会怎么样…它给出了错误“太多的值无法解包”@jenkris我将在这里使用一些苏格拉底式的方法。
ax.plot
返回什么?表达式左侧的逗号在做什么?