need some advice on x y plot

N

nephish

well, for what i tried, ticstrings just contained a list of times
formatted like
[10-21 09:15, 10-21 09:44, 10-21 09:59, and so on.....]
i did write mine a little different than the example that Grant gave me
because its a little
different application of time.

i think i am having trouble knowing exactly what the set xtics line is
looking for.
thanks
 
G

Grant Edwards

ok, i am stuck a bit here with this line

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

the error code says that it is looking for a closing parenthesis.

Well, what is the value of ticstrings?

Try passing the parameter debug=1 to Guplot.Gnuplot():

gp = Gnuplot.Gnuplot(debug=1)

Then it will print out all of the gnuplot commands to stdout.
that and i am kinda new enough not to really get what %s is
all about. i know it formats a string.

http://www.google.com/search?hl=en&q=python+string+formatting

Follow the first hit.
can you simply pass a list to 'set xtics' ?

That depends. What do you mean by "a list", and what do you
mean "pass it to 'set xtics'"?
i mean, i got this from the gnuplot site

Syntax:

set xtics {axis | border} {{no}mirror} {{no}rotate {by <ang>}}
{offset <offset> | nooffset}
{ autofreq
| <incr>
| <start>, <incr> {,<end>}
| ({"<label>"} <pos> {<level>} {,{"<label>"}...) }
{ font "name{,<size>}" }
{ textcolor <colorspec> }
unset xtics
show xtics

but it does not look like all the options are necessary from
the docs, i guess i am asking, if i just wanted to pass a list
of say, 14 tics to the x axis as a list, what of the above is
necessary ?

Well, you could start up gnuplot at the command line and try a
few "set xtics" commands. That's how I figur gnuplot commands
out. The format for the set xtics command that I was using in
my demo was

set xtics ("label" pos, "label" pos, "label" pos)
i built the list by taking time stamps in seconds (the same as
being plotted for x) took the end time minus the start time
and divided by 12, incremented each by this amount until i had
12 plots (plus of course the first and last). these are all
stored in plot_times[]..

We can't read your mind. Unless you show us actual code, we
can't help. Show us a small example program with Gnuplot's
debug feature enabled that doesn't do what you want it to.

Tell us what it is you wanted the program to do and post the
output from that program. Do _not_ attempt to re-type the
output from the program. Either redirect it to a file or
cut/paste it.
 
G

Grant Edwards

well, for what i tried, ticstrings just contained a list of times
formatted like
[10-21 09:15, 10-21 09:44, 10-21 09:59, and so on.....]

OK. What did ticstrings contain in the demo I wrote for you?

[When somebody who is trying to help you asks what the value of
an object was in your program just paste the output from
repr(obj). If you try to describe the contents in English, I
guarantee it won't be sufficient.]
i did write mine a little different than the example that
Grant gave me

Well, that's your problem. Look at the ticstrings value that
worked, and look at the ticstrings value that didn't work.

What is the difference between the two?
 
F

Frithiof Andreas Jensen

If the x-axis is time, gnuplot will plot it correctly but it will connect
*all* the datapoints and scale the x-axis so that everything will fit on the
graph. Is it the autoscaling or conneting that what you think is wrong?

Getting a fixed-size x-axis is a configuration option. I think one simply
passes a range to the axis one wants to scale. I cannot remember exactly,
how though.
one of you guys know a charting app that will do this ? or is there
some other way i could do it?

RRDTool is excellent at storing & plotting time series data. It will not
"connect" missing data points but has the limitation that the sample rate
must be known when creating the database and generally adhered to. Missed
samples are NAN's.

You have to "tell" gnuplot what to plot and what not to plot. I think you
can add more than one data series to the same plot, providing the "gap"
where data is unavaliable. Else you have to "pad" it with zeros or some
other appropriate null value.
 
N

nephish

Ok, first off, thanks for all the help guys,

this part " set xtics ("label" pos, "label" pos, "label" pos) "
is mainly what i was confused about. the 'pos' part. i think that the
way i am writing
this leaves this out. in fact, i am pretty sure.

here is the code i am trying out.

def draw_chart(self, StartTime, EndTime):
# make start time and end time markers in seconds
start_tic = time.mktime(time.strptime(StartTime, '%Y-%m-%d
%H:%M:%S'))
end_tic = time.mktime(time.strptime(EndTime, '%Y-%m-%d %H:%M:%S'))
# get the difference in seconds between start and end times
diff_time = start_tic - end_tic
# get tick marks with respect to time
tic_increment = diff_time / 15
#build an array of ticmarks
tics_raw = []
tics_raw.append(start_tic)
tic_adder = start_tic
for x in range(13):
tic_adder = tic_adder + tic_increment
tics_raw.append(tic_adder)

#add the last time to the tics array
tics_raw.append(end_tic)

# change all the tic increments to reader understandable values
tics = []
for x in tics_raw:
tics.append(time.strftime('%m/%d %H:%M', time.localtime(x)))
print 'tic '+(time.strftime('%m/%d %H:%M', time.localtime(x)))

# get the plot points date / value
Sensor = self.GraphSensorEntry.get_text()
db = MySQLdb.connect(host="localhost", user="piv",
passwd="crayon99", db="DDS")
cursor=db.cursor()
cursor.execute("SELECT `Raw`, `DateTime` FROM `Process` WHERE
`Sensor_ID` = '"+Sensor+"' \
AND `DateTime` > '"+StartTime+"' AND `DateTime` < '"+EndTime+"'
ORDER BY `DateTime` ")
results = cursor.fetchall()
plot_x = []
plot_y = []
for row in results:
Value = row[0]
#convert datetime.datetime object to epoch (seconds) object
Time = time.mktime(row[1].timetuple())
print time.strftime('%m/%d %H:%M:%S', time.localtime(Time))

plot_x.append(float(Time))
plot_y.append(float(Value))
g = Gnuplot.Gnuplot(debug=1)
g.title('testing')
data = Gnuplot.Data(plot_x,plot_y)
outfile = '/home/piv/PivData/tmp/images/graph.png'
g('set term png')
g('set out "%s"' % outfile)
g('set xtics (%s)' % (tics))
g.plot(data)

self.GraphImage.set_from_file('/home/piv/PivData/tmp/images/graph.png')



and this is the terminal output i get

gnuplot> set title "testing"
gnuplot> set term png
gnuplot> set out "/home/piv/PivData/tmp/images/graph.png"
gnuplot> set xtics (['10/18 09:54', '10/17 22:42', '10/17 11:30',
'10/17 00:18', '10/16 13:06', '10/16 01:54', '10/15 14:42', '10/15
03:30', '10/14 16:18', '10/14 05:06', '10/13 17:54', '10/13 06:42',
'10/12 19:30', '10/12 08:18', '10/25 09:54'])
gnuplot> plot '/tmp/tmpn2URt2' notitle

gnuplot> set xtics (['10/18 09:54', '10/17 22:42', '10/17 11:30',
'10/17 00:18', '10/16 13:06', '10/16 01:54', '10/15 14:42', '10/15
03:30', '10/14 16:18', '10/14 05:06', '10/13 17:54', '10/13 06:42',
'10/12 19:30', '10/12 08:18', '10/25 09:54'])
^
line 0: invalid expression


it is drawing the graph though, and it looks right compared with the
data

i noticed in the docs for gnuplot, that it can do date/time and by
default uses seconds since 2000. and then you can pass the format that
you want to show it in.
would this give the same kind of result that i am looking for ?


my math in how i am doing this is kinda off too, i think.
for the stuff i am doing on our website, i use php with jpgraph, it
does things a little
different.

thanks for everything
shawn
 
G

Grant Edwards

Ok, first off, thanks for all the help guys,

this part "set xtics ("label" pos, "label" pos, "label" pos)"
is mainly what i was confused about. the 'pos' part. i think
that the way i am writing this leaves this out. in fact, i am
pretty sure.

Yup, it looks like it.
here is the code i am trying out.

[Another hint: when posting code, don't wrap it. It won't run
as it was posted, and people aren't generally going to be willing to go
through and un-wrap the lines in order to try it.]
gnuplot> set title "testing"
gnuplot> set term png
gnuplot> set out "/home/piv/PivData/tmp/images/graph.png"
gnuplot> set xtics (['10/18 09:54', '10/17 22:42', '10/17 11:30',
'10/17 00:18', '10/16 13:06', '10/16 01:54', '10/15 14:42', '10/15
03:30', '10/14 16:18', '10/14 05:06', '10/13 17:54', '10/13 06:42',
'10/12 19:30', '10/12 08:18', '10/25 09:54'])

OK, You need to get rid of the sqare brackets, and you need an
x-position value after each of the strings.
i noticed in the docs for gnuplot, that it can do date/time
and by default uses seconds since 2000. and then you can pass
the format that you want to show it in. would this give the
same kind of result that i am looking for ?

It looks like it. Though I've used custom tics in the past, it
was never for time values. Based on the help from gnuplot, I
suspect you can get what you want without doing custom tics,
but rather using the commands

set xdata time
set timefmt
set format x

Interestingly, using Unix timestamps creates some sort of
resolution problems. The following ought to work but doesn't.
It appears that there is some sort of resolution problem:

------------------------------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',using=(1,2))
gp = Gnuplot.Gnuplot(debug=1)
gp.title('Data starting at %s' % time.asctime(time.gmtime(start+xdata[0])))

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

# same data with x value as Unix timestamps

xdata = [x+start for x in xdata]
print xdata
data = Gnuplot.Data(xdata,ydata,with='linespoints',using=(1,2))

gp('set xdata time')
gp('set timefmt "%s')
gp('set format x "%r"')
gp('set xtics 120')
gp.plot(data)
pause()
------------------------------8<------------------------------
 
G

Grant Edwards

It looks like it. Though I've used custom tics in the past, it
was never for time values. Based on the help from gnuplot, I
suspect you can get what you want without doing custom tics,
but rather using the commands

set xdata time
set timefmt
set format x

Interestingly, using Unix timestamps creates some sort of
resolution problems. The following ought to work but doesn't.
It appears that there is some sort of resolution problem:

------------------------------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',using=(1,2))
gp = Gnuplot.Gnuplot(debug=1)
gp.title('Data starting at %s' % time.asctime(time.gmtime(start+xdata[0])))

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

# same data with x value as Unix timestamps

xdata = [x+start for x in xdata]
print xdata
data = Gnuplot.Data(xdata,ydata,with='linespoints',using=(1,2))

gp('set xdata time')
gp('set timefmt "%s')
gp('set format x "%r"')
gp('set xtics 120')
gp.plot(data)
pause()
------------------------------8<------------------------------

Yup. Something in the Gnuplot module is broken. Here's the
x data I'm passing it:

[1130256529.616158, 1130256589.616158, 1130256649.616158,
1130256709.616158, 1130256769.616158, 1130256829.616158,
1130256889.616158, 1130256949.616158, 1130257009.616158,
1130257069.616158]

And here's what it's passing to gnuplot

1130256512.0 0.0
1130256640.0 0.564642488956
1130256640.0 0.93203908205
1130256768.0 0.97384762764
1130256768.0 0.675463199615
1130256768.0 0.141120001674
1130256896.0 -0.442520439625
1130256896.0 -0.871575772762
1130257024.0 -0.996164619923
1130257024.0 -0.772764503956

It appears that the Gnuplot modules has coerced my data into
single-precision -- thus throwing away most of the resolution
on the x-axis.
 
G

Grant Edwards

It appears that the Gnuplot modules has coerced my data into
single-precision -- thus throwing away most of the resolution
on the x-axis.

Passing Gnuplot.Data a Numeric array object is a good
work-around. Otherwise, Gnuplot.Data will convert it into a
float (single precision) arry.

Here's a revised demo that works better:

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

Gnuplot.GnuplotOpts.prefer_fifo_data = 0

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',using=(1,2))
gp = Gnuplot.Gnuplot(debug=1)
gp.title('Data starting at %s' % time.asctime(time.gmtime(start+xdata[0])))

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

# same data with x value as Unix timestamps
xdata = [x+start for x in xdata]
a = Numeric.array(zip(xdata,ydata))

data = Gnuplot.Data(a,with='linespoints',using=(1,2))

gp('set xdata time')
gp('set timefmt "%s')
gp('set format x "%r"')
gp('set xtics 120')
gp.plot(data)
pause()


------------------------------8<------------------------------
 
D

Dennis Lee Bieber

this part " set xtics ("label" pos, "label" pos, "label" pos) "
is mainly what i was confused about. the 'pos' part. i think that the
way i am writing
this leaves this out. in fact, i am pretty sure.
pos (position) is the one part of that syntax you CANNOT leave out.
The label is optional. Sounds like you are trying to supply labels
without telling it where on the line to put them.
here is the code i am trying out.

def draw_chart(self, StartTime, EndTime):
# make start time and end time markers in seconds
start_tic = time.mktime(time.strptime(StartTime, '%Y-%m-%d
%H:%M:%S'))
end_tic = time.mktime(time.strptime(EndTime, '%Y-%m-%d %H:%M:%S'))
# get the difference in seconds between start and end times
diff_time = start_tic - end_tic
# get tick marks with respect to time
tic_increment = diff_time / 15
#build an array of ticmarks
tics_raw = []
tics_raw.append(start_tic)
tic_adder = start_tic
for x in range(13):
tic_adder = tic_adder + tic_increment
tics_raw.append(tic_adder)

#add the last time to the tics array
tics_raw.append(end_tic)

# change all the tic increments to reader understandable values
tics = []
for x in tics_raw:
tics.append(time.strftime('%m/%d %H:%M', time.localtime(x)))

I think you need to interleave the string label with the RAW numbers
(as the raw numbers are what the plotting package should be using to
position data -- hopefully you don't have to also zero-normalize them
and the actual data). Something like

tics.append('"%s" %s' % (time.strftime(...), x))
# might need time.localtime(x) for that last term.

Each list item should then be a string consisting of a double-quoted
label followed by a numeric "position" value.
--
 
N

nephish

tics.append('"%s" %s' % (time.strftime(...), x))
# might need time.localtime(x) for that last term.

ok, tried this and it worked.
but the first plot is at the last plot of data
back to that math mistake i mentioned earlier.
so, thanks much, i will be back when i mess around with it some more
and
see if i can get it right.
thanks very very much guys,

i know i sound frustrated and ignorant, but i am having a lot of fun
with this
 
N

nephish

WE DID IT !
little more tinkering and correcting this
diff = start_time - end_time (vrs the other way around)

and its working.

so many thanks gents, a lot !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top