本文介绍了matplotlib 实时线性线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a major setback on this question on a while now...

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

我试图实时绘制一条直线,但无法得出结果.提前致谢.到目前为止,我有一个人物要登场,但画布上什么也没画.

I am trying to draw a linear line in real time but I am unable to come up with the result. Thanks in advance. So far, I have a figure coming up but nothing is being drawn on the canvas.

有趣....我现在可以在线上绘制点,但是现在,我无法显示每个点之间的连接...我还注意到,如果您在绘制时删除了 ko- ...什么也没出现,有人知道为什么吗?

Interesting.... I am now able to plot the dots on the line but now, I am unable to show their connectivity between each point... I also noticed that if you removed ko- when it is being plotted... nothing appears, does anybody know why?

import numpy as n
import pylab as p
import time

x=0
y=0
p.ion()
fig=p.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 = i
    y = i
    line.set_data(x,y)
    p.draw()

推荐答案

在循环中添加 p.pause(.001).您需要留出时间让gui事件循环触发和更新显示.

add a p.pause(.001) in your loop. You need to allow time for the gui event loops to trigger and update the display.

这与问题#1646 有关.

您遇到的另一个问题是,当您执行 set_data 时,它会替换用传入的 xy 绘制的数据,不追加到已经存在的数据上.(要清楚地看到这一点,请使用 p.pause(1))当您删除 'ko-' 时,默认情况下没有标记与连接点的线,您正在绘制单个点,因此什么也没出现.

The other issue you have is that when you do set_data it replaces the data that is plotted with the x and y passed in, not append to the data that is already there. (To see this clearly use p.pause(1)) When you remove 'ko-', which defaults to no marker with a line connecting points you are plotting a single point, hence nothing shows up.

我认为你打算写这个:

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)

这篇关于matplotlib 实时线性线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:22