Adding data on the fly to a graph

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    Adding data on the fly to a graph

    Hi

    I'm implementing a program for an experiment that will capture data points over the course of several hours. I want to be able to visualise the data as it's captured. I'm open to whatever solution works best, but it seems that it's a bit of a fiddle to do this in matplotlib, and so I've turned to Gnuplot.py, which (obviously) interfaces with gnuplot.

    Here's something that works:

    Code:
    import Gnuplot
    import time
    
    def f(x):
        return 2*(x/10.0)**2
    
    
    gT=Gnuplot.Gnuplot()
    gT.title("Temperature")
    gT.xlabel("t(s)")
    gT.ylabel("T/K")
    
    
    d1=[-1]
    d2=[f(-1)]
    for i in range(10):
        d1.append(i)
        d2.append(f(i))
        print i,f(i)
        dT=Gnuplot.Data(d1,d2)
        gT.plot(dT)
        time.sleep(0.5)
    So the graph appears and then grows over time.

    Now the problem is that it seems to be slightly inelegant. We may get up to tens of thousands of data points, and the program is basically:
    1. creating an empty array
    2. appending the new data point to the array
    3. converting the arrays into gnuplot data
    4. plotting the gnuplot data (which automatically deletes all previous plotting data)

    It seems to me that is would be better to somehow just send the one extra data point, rather than sending all the data to be plotted and delete the almost identical data set that's already there!!

    Now replot is supposed to be able to do this, but then it plots each new data point as a new series, rather than appending to the series that's already there.
    (my version looks like this):
    Code:
    import Gnuplot, Gnuplot.funcutils
    import time
    
    def f(x):
        return 2*(x/10.0)**2
    
    
    gT=Gnuplot.Gnuplot()
    gT.title("Temperature")
    gT.xlabel("t(s)")
    gT.ylabel("T/K")
    
    
    d1=[-1]
    d2=[f(-1)]
    dT=Gnuplot.Data(d1,d2)
    gT.plot(dT)
    time.sleep(0.5)
    for i in range(10):
        d1.append(i)
        d2.append(f(i))
        print i,f(i)
        dT=Gnuplot.Data(i,f(i))
        gT.replot(dT)
        time.sleep(0.5)
    As you might be able to see, each data point is a different colour and shape, so this solution is clearly worse.

    Is it possible to send a single data point such that it appends to an existing series?
Working...