Labels in a dynamic graph

Hi everyone,

I am using matplotlib
to dynamically plot a graph with both my x and y points taken from a
measurement device. That is to say, in each iteration of my while loop
I’m reading two variables which I then want to plot with matplotlib.

I wrote something which
goes like this (disregard the Gnuplot - that’s what I’m trying to
replace with matplotlib…)

import gpib,
Numeric, time, Gnuplot, mymodule, threading
from pylab import *

def
cooldown(filename, dmm_gpib, lake_gpib):

“”“this program
scans dummy and reads HP and Lakeshore”""

A             = Numeric.arange(-1, 1, .1)
delay         = 1
f = open(filename,'w')

X = []
Y = []
figure(2)
hold(False)
try:
while A[0]<10:

gpib.write(lake_fd, ‘SDAT?’)

        gpib.write(hp_fd, 'read?')

        time.sleep(delay)

        val1 = float(gpib.read(lake_fd, 30))

        val2 = float(gpib.read(hp_fd, 30))

        X.append(val1)

        Y.append(val2)

        plot(X,Y,'.-')

        f.write(str(val1) + '\t' + str(val2) + '\n')

        f.flush()

I’m running this code in ipython with the -pylab option, so I don’t
need to use show(). My question is, how do I maintain a constant
xlabel and ylabel without having to redraw them each time I append a
point to the graph? If I try xlabel(‘something’) then obviously it’s
cleared each time I use plot(X,Y).

Any ideas?

Thanks,

Amit.

Hello Amit,

···

On Sunday 23 March 2008 09:54, Amit Finkler wrote:

Hi everyone,

I am using matplotlib to dynamically plot a graph with both my x and y
points taken from a measurement device. That is to say, in each
iteration of my while loop I'm reading two variables which I then want
to plot with matplotlib.

I wrote something which goes like this (disregard the Gnuplot - that's
what I'm trying to replace with matplotlib...)

    import gpib, Numeric, time, Gnuplot, mymodule, threading
    from pylab import *

    def cooldown(filename, dmm_gpib, lake_gpib):

    """this program scans dummy and reads HP and Lakeshore"""

    A = Numeric.arange(-1, 1, .1)
    delay = 1
    f = open(filename,'w')

    X = []
    Y = []
    figure(2)
    hold(False)
        try:
            while A[0]<10:

                gpib.write(lake_fd, 'SDAT?')
                gpib.write(hp_fd, 'read?')
                time.sleep(delay)
                val1 = float(gpib.read(lake_fd, 30))
                val2 = float(gpib.read(hp_fd, 30))
                X.append(val1)
                Y.append(val2)
                plot(X,Y,'.-')
                f.write(str(val1) + '\t' + str(val2) + '\n')
                f.flush()

I'm running this code in ipython with the -pylab option, so I don't need
to use show(). My question is, how do I maintain a *constant* xlabel and
ylabel without having to redraw them each time I append a point to the
graph? If I try xlabel('something') then obviously it's cleared each
time I use plot(X,Y).

I'm not sure I understand well, but if one uses xlabel("something") before the
while-loop or just after building the figure, it is not redrawn after
plotting.

regards
Matthias

You will want to do something like (this is just a sketch)

xdata = []
ydata = []

fig = figure()
ax = fig.add_subplot(111)
ax.set_xlabel('my xlabel')
ax.set_ylabel('my ylabel')
line, = ax.plot(xdata, ydata)

def add_point(x, y):
    xdata.append(x)
    ydata.append(y)
    if len(xdata)>30: # optional, prune the early points
        del xdata[0]
        del ydata[0]
    xmin = xdata[0]
    xmax = xdata[-1]
    line.set_data(xdata, ydata)
    ax.set_xlim(xmin, xmax)
    fig.canvas.draw()

while 1:
    x,y = get_data_point()
    add_point(x, y)

JDH

···

On Sun, Mar 23, 2008 at 3:54 AM, Amit Finkler <amit.finkler@...287...> wrote:

I am using matplotlib to dynamically plot a graph with both my x and y
points taken from a measurement device. That is to say, in each iteration of
my while loop I'm reading two variables which I then want to plot with
matplotlib.

John Hunter wrote:

I am using matplotlib to dynamically plot a graph with both my x and y
points taken from a measurement device. That is to say, in each iteration of
my while loop I'm reading two variables which I then want to plot with
matplotlib.

You will want to do something like (this is just a sketch)
xdata = []
ydata = []
fig = figure()
ax = fig.add_subplot(111)
ax.set_xlabel('my xlabel')
ax.set_ylabel('my ylabel')
line, = ax.plot(xdata, ydata)
def add_point(x, y):
xdata.append(x)
ydata.append(y)
if len(xdata)>30: # optional, prune the early points
del xdata[0]
del ydata[0]
xmin = xdata[0]
xmax = xdata[-1]
line.set_data(xdata, ydata)
ax.set_xlim(xmin, xmax)
fig.canvas.draw()
while 1:
x,y = get_data_point()
add_point(x, y)
JDH

John,

Thanks for getting back to me. Indeed this works, at least when I try
it line by line. When I inserted it into my module, it shot back some
error message which goes like this:

File
“/usr/lib/python2.4/site-packages/matplotlib/backends/backend_tkagg.py”,
line 154, in draw

FigureCanvasAgg.draw(self)

File
“/usr/lib/python2.4/site-packages/matplotlib/backends/backend_agg.py”,
line 392, in draw

self.figure.draw(renderer)

File “/usr/lib/python2.4/site-packages/matplotlib/figure.py”, line
544, in draw

for a in self.axes: a.draw(renderer)

File “/usr/lib/python2.4/site-packages/matplotlib/axes.py”, line
1004, in draw

try: self.transData.freeze()  # eval the lazy objects

ValueError: Domain error on eval_scalars in Transformation::freeze
Since it did work on the console, i.e., line by line, I think it’s only
a matter of resolving my own source code, unless of course you think
otherwise. By the way, isn’t there a way to do the set_xlim/ylim
automatically? When I use only figure(), hold(False) and plot(X, Y), it
updates it automatically, so why doesn’t it do it with the subplot?

Thanks for your help.

Amit.

···

<amit.finkler@…287…>

Odd, are you using an older version of matplotlib (it works for me).
Try seeding your data with an initial point

xdata = [x]
ydata = [y]

or setting the axis limits before plotting

ax.set_xlim(0,1)
ax.set_ylim(0,1)

Not sure why you are having problems....

JDH

···

On Tue, Mar 25, 2008 at 11:39 AM, Amit Finkler <amit.finkler@...287...> wrote:

I am using matplotlib to dynamically plot a graph with both my x and y
points taken from a measurement device. That is to say, in each iteration of
my while loop I'm reading two variables which I then want to plot with
matplotlib.