matplotlib实时线性线

matplotlib实时线性线,matplotlib,real-time,Matplotlib,Real Time,我有一段时间在这个问题上遇到了重大挫折 import numpy as np import matplotlib.pyplot as plt plt.ion() fig = plt.figure(1) ax = fig.add_subplot(111) ax.set_title("linear realtime") line, = ax.plot([],[]) i = 0 while ( i < 1000 ): #EDIT: # this is jus

我有一段时间在这个问题上遇到了重大挫折

import numpy as np
import matplotlib.pyplot as plt

plt.ion()
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.set_title("linear realtime")
line, = ax.plot([],[])

i = 0 
while ( i < 1000 ):
        #EDIT:
        # this is just sample data, but I would eventually like to set data 
        # where it can be floating numbers...
        line.set_data(i,i)             
        fig.canvas.draw()
        i += 1
在循环中添加一个
p.pause(.001)
。您需要留出时间让gui事件循环触发和更新显示

这是有关的

另一个问题是,当您执行
set_data
时,它会替换用传入的
x
y
绘制的数据,而不是附加到已经存在的数据中。(要清楚地看到这一点,请使用
p.pause(1)
)在删除
'ko-'
时,默认为没有带线连接点的标记,因为您正在绘制单个点,因此不会显示任何内容

我想你打算写这个:

x=0
y=0 

fig=plt.figure(1)
ax=fig.add_subplot(111)
ax.set_xlim(0,10)
ax.set_ylim(0,10)
line,=ax.plot(x,y,'ko-')
for i in range(10):
    x = np.concatenate((line.get_xdata(),[i]))
    y = np.concatenate((line.get_ydata(),[i]))
    line.set_data(x,y)
    plt.pause(1)

谢谢你的意见,我试着添加了这一点,但两点之间的连接和删除ko-的问题仍然存在。我甚至试着在暂停时增加一个更高的值。
x=0
y=0 

fig=plt.figure(1)
ax=fig.add_subplot(111)
ax.set_xlim(0,10)
ax.set_ylim(0,10)
line,=ax.plot(x,y,'ko-')
for i in range(10):
    x = np.concatenate((line.get_xdata(),[i]))
    y = np.concatenate((line.get_ydata(),[i]))
    line.set_data(x,y)
    plt.pause(1)