简体   繁体   English

Matplotlib set_data使我的程序冻结(请检查“更新后的问题”)

[英]Matplotlib set_data makes my program freeze (please check the 'Updated issue')

I am trying to plot two updating plots, one is a diagram and the other one is an image captured from the camera. 我正在尝试绘制两个更新的图,一个是图,另一个是从相机捕获的图像。

I get an error on this line: 我在这条线上出现错误:

"current_line.set_data(y_data)" in the "update" function.  
The error says: "AttributeError: 'list' object has no attribute 'set_data'".

Any idea why am I getting this error? 知道为什么我会收到此错误吗? If I comment out this line I will get changing images from the camera and everything except the second plot seems fine (because the second plot is not updating) but I need the second plot to be updated as well. 如果我注释掉这条线,我将获得来自相机的更改图像,除了第二个图以外,其他一切似乎都很好(因为第二个图未更新),但我也需要更新第二个图。

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

UPDATED ISSUE: 更新的问题:

The first problem is solved but now I am facing another one. 第一个问题已解决,但现在我面临另一个问题。 My program freezes after running it and it does not generate any errors.There is another thing that might be relevant to this problem, I am multithreading and this piece of code is in the main thread. 我的程序在运行后会冻结,并且不会产生任何错误。还有另一件事可能与此问题有关,我是多线程的,并且这段代码在主线程中。

ax.plot returns a list of Line2D instances (in your case, its a 1-item list). ax.plot返回Line2D实例的列表(在您的情况下,为1-item列表)。 This is because it is possible to plot multiple lines in one go with ax.plot . 这是因为可以使用ax.plot绘制多条线。

So, in your case, you just need to grab the first item of the list. 因此,就您而言,您只需要获取列表的第一项即可。 The simplest way is probably to change this line: 最简单的方法可能是更改此行:

line = ax2.plot(y_data, 'o-')

to this: 对此:

line, = ax2.plot(y_data, 'o-')

Note that while your question is about setting the data of the line, rather than adding a legend , this Q&A are relevant here, since the solution is the same: Python legend attribute error 请注意,虽然您的问题是关于设置行的data ,而不是添加legend ,但此问题与解答在此处相关,因为解决方案是相同的: Python图例属性错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM