need some advice on x y plot

Grant Edwards grante at visi.com
Fri Oct 21 21:04:16 CEST 2005


On 2005-10-21, nephish at xit.net <nephish at xit.net> wrote:

> the time is DateTime.DateTime object from a mySQLdb query. the
> value is a number anywhere between 0 and 15. the datetime is
> formatted like 2005-10-20 08:40:34
>
> i could strip it and make a timestamp out of it. but reading
> the number of seconds since january of 1970 doesn't make a
> neat chart.

Pass the data values as floating point numbers (I typically use
seconds since the test run started).  If you the x tics to be
something else, then change them.  It's pretty simple to label
the x-axis using normal date/time notation.

> any suggestions?
>
> oh, and can you output from Gnuplot to a png or jpeg or
> something like that.

Is that a question? If so, the answer is yes.

> the Gnuplot documentation says that it can make a bitmap png
> but i dont know how to do that with the python wrapper. the
> docs for this are a little cryptic. still a newbie here. if
> you know a good tutorial on this, i would really appreciate
> the link.

Once you start doing "odd" stuff (e.g. custom labels for the x-axis,
output other than X11 or postscript), it's usually simplest to
just send normal gnuplot commands.

------------------------------8<------------------------------
import Gnuplot,time,sys,math

def pause():
    sys.stdout.write("Press enter to continue: ")
    sys.stdin.readline()

def fgrid(start,stop,count):
    for i in xrange(count):
        yield start + ((stop-start)*i)/count

start = time.time()

xdata = [x for x in fgrid(0,600.0,10)]      # two minutes worth
ydata = [math.sin(x/100.0) for x in xdata]

data = Gnuplot.Data(xdata,ydata,with='linespoints')

gp = Gnuplot.Gnuplot()
gp.title('Data starting at %s' % time.ctime(start+xdata[0]))

# x axis will use default tics (seconds since start of run)
gp.plot(data)
pause()

# now let's change x tics to time of day in HH:MM:SS with
# ticmarks at 120 second intervals

ti = 120.0
startTic = int((start+xdata[0])//ti * ti)
endTic = int((start+xdata[-1])//ti * ti)
ti = int(ti)

ticx = range(startTic,endTic+ti,ti)
tics = [(x, time.strftime('%H:%M:%S',time.localtime(x))) for x in ticx]
ticstrings = ['"%s" %f' % (t[1],t[0]-start) for t in tics]

gp('set xtics (%s)' % ','.join(ticstrings))
gp.plot(data)
pause()

outfile = 'foo.png'
gp('set term png')
gp('set out "%s"' % outfile)
gp.plot(data)
------------------------------8<------------------------------

-- 
Grant Edwards                   grante             Yow!  ... The waitress's
                                  at               UNIFORM sheds TARTAR SAUCE
                               visi.com            like an 8" by 10" GLOSSY...



More information about the Python-list mailing list