Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

matplotlib real-time linear line

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.

EDIT:

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

This is related to issue #1646.

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.

I think you intended to write this:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...