Python Matplotlib set#U数据使我的程序冻结(请检查';更新版';)
我正在尝试绘制两个更新图,一个是图表,另一个是从相机捕获的图像 我在这一行上得到一个错误:Python Matplotlib set#U数据使我的程序冻结(请检查';更新版';),python,opencv,matplotlib,python-multithreading,Python,Opencv,Matplotlib,Python Multithreading,我正在尝试绘制两个更新图,一个是图表,另一个是从相机捕获的图像 我在这一行上得到一个错误: "current_line.set_data(y_data)" in the "update" function. The error says: "AttributeError: 'list' object has no attribute 'set_data'". 知道我为什么会犯这个错误吗?如果我注释掉这一行,我将从相机中获得不断变化的图像,除了第二个绘图外,其他一切看起来都很好(因为第二个绘
"current_line.set_data(y_data)" in the "update" function.
The error says: "AttributeError: 'list' object has no attribute 'set_data'".
知道我为什么会犯这个错误吗?如果我注释掉这一行,我将从相机中获得不断变化的图像,除了第二个绘图外,其他一切看起来都很好(因为第二个绘图没有更新),但我也需要更新第二个绘图
y_data = [0]
# Capture intial frame
ret, initial_frame = lsd.cap.read()
# Function for making the initial figure
def makeFigure():
fig = plt.figure()
# Show frame
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2)
plot_frame = ax1.imshow(initial_frame, animated=True)
# Set the limits of the plot and plot the graph
ax2 = plt.subplot2grid((2, 2), (1, 0), colspan=2)
ax2.set_title('Title')
ax2.set_ylabel('Y-Label')
ax2.set_ylim(0, 100)
ax2.set_xlim(0, 100)
ax2.grid()
line = ax2.plot(y_data, 'o-')
return fig, plot_frame, line
def update(i, current_frame, current_line, y_data):
# Capture original frame_new_RGB from camera
ret, frame_new_original = lsd.cap.read()
# Changing frame_new_original's color order
frame_new_RGB = cv2.cvtColor(frame_new_original, cv2.COLOR_BGRA2RGB)
y_data.append(randint(0, 9))
# Update figure
current_line.set_data(y_data)
# Update frame
current_frame.set_data(frame_new_RGB)
# Make figures and animate the figures
curr_fig, curr_frame, curr_line = makeFigure()
anim = FuncAnimation(curr_fig, update, fargs=[curr_frame, curr_line, y_data], interval=10)
plt.show()
# When everything done, release the capture
lsd.cap.release()
cv2.destroyAllWindows()
更新版:
第一个问题解决了,但现在我面临另一个问题。我的程序在运行后会冻结,不会产生任何错误。还有一点可能与此问题有关,我是多线程的,这段代码位于主线程中。
ax.plot
返回Line2D
实例的列表(在您的情况下,它是一个1项列表)。这是因为使用ax.plot
可以一次性绘制多条线
所以,在你的例子中,你只需要抓住列表的第一项。最简单的方法可能是更改此行:
line = ax2.plot(y_data, 'o-')
为此:
line, = ax2.plot(y_data, 'o-')
请注意,虽然您的问题是关于设置行的
数据,而不是添加图例,但此问题与此相关,因为解决方案是相同的:ax.plot
返回Line2D
实例列表(在您的情况下,它是一个1项列表)。这是因为使用ax.plot
可以一次性绘制多条线
所以,在你的例子中,你只需要抓住列表的第一项。最简单的方法可能是更改此行:
line = ax2.plot(y_data, 'o-')
为此:
line, = ax2.plot(y_data, 'o-')
请注意,虽然您的问题是关于设置行的数据,而不是添加图例
,但此问题与此相关,因为解决方案是相同的:实际上,我尝试过,但我的程序只是在更改后冻结,因此我认为这可能不是正确的做法。。。但现在看来,冻结的问题还有另一个原因!你知道为什么我运行代码时它会冻结吗?它不会产生任何错误,只是冻结了!还有一件事可能与这个问题有关。我是多线程的,这段代码在主线程中。实际上,我试过了,但我的程序在更改后就冻结了,所以我认为这可能不是正确的做法。。。但现在看来,冻结的问题还有另一个原因!你知道为什么我运行代码时它会冻结吗?它不会产生任何错误,只是冻结了!还有一件事可能与这个问题有关。我是多线程的,这段代码在主线程中。