如何使用 matplotlib 在 while 循環中實時繪圖? - How do I plot in real-time in a while loop using matplotlib?

問題:

I am trying to plot some data from a camera in real time using OpenCV.我正在嘗試使用 OpenCV 實時繪製來自相機的一些數據。 However, the real-time plotting (using matplotlib) doesn't seem to be working.但是,實時繪圖(使用 matplotlib)似乎不起作用。

I've isolated the problem into this simple example:我已經將問題隔離到這個簡單的例子中:

fig = plt.figure()
plt.axis([0, 1000, 0, 1])

i = 0
x = list()
y = list()

while i < 1000:
    temp_y = np.random.random()
    x.append(i)
    y.append(temp_y)
    plt.scatter(i, temp_y)
    i += 1
    plt.show()

I would expect this example to plot 1000 points individually.我希望這個例子可以單獨繪製 1000 個點。 What actually happens is that the window pops up with the first point showing (ok with that), then waits for the loop to finish before it populates the rest of the graph.實際發生的是,窗口彈出並顯示第一個點(確定),然後等待循環完成,然後再填充圖形的其餘部分。

Any thoughts why I am not seeing points populated one at a time?有什麼想法爲什麼我沒有看到一次填充一個點?


解決方案:

參考一: https://en.stackoom.com/question/npAV
參考二: https://stackoom.com/question/npAV
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章