From: kevin p. <ke...@ma...> - 2004-11-06 00:20:37
|
i have some data that would like to plot with Gnuplot-py and need to get a little fancier than i am used to. I have data that looks like: 0 5.5 0 1 2 5.5 2 4 4 5.5 4 2 6 5.5 5 3 8 5.5 7 1 10 5.5 9 4 12 5.5 11 3 14 5.5 0 2 16 5.5 2 1 18 5.5 4 4 20 5.5 5 3 The first number is the start time of the event. The second is the duration. The third is the event identifier and the last is what stream it belongs to. I would like to use splot and points of different colors to convey as much info as i can. The X axis would be the time axis (that would take care of the first two parameters) and the Y axis could be the third column. Then for the fouth item, there are only four possibilities (1, 2, 3, or 4), so i thought that i could use points of red, blue, green, black to indicate that. So that is where i am headed... But i have a long way to go and am a gnuplot novice and not a great Python hacker to boot.. Could anyone help me along? I would be grateful for any help. Cheers, kevin Here is some (*gasp*) code. ------------ [snip] ---------------- #! /usr/bin/env python from Numeric import * import Gnuplot, Gnuplot.funcutils def main(): all = [ [ 0, 5.5, 0, 1 ], [ 2, 5.5, 2, 4 ], [ 4, 5.5, 4, 2 ], [ 6, 5.5, 5, 3 ], [ 8, 5.5, 7, 1 ], [ 10, 5.5, 9, 4 ], [ 12, 5.5, 11, 3 ], [ 14, 5.5, 0, 2 ], [ 16, 5.5, 2, 1 ], [ 18, 5.5, 4, 4 ], [ 20, 5.5, 5, 3 ], [ 22, 5.5, 7, 2 ], [ 24, 5.5, 9, 1 ], [ 26, 5.5, 11, 4 ], [ 28, 5.5, 0, 3 ], [ 30, 5.5, 2, 2 ], [ 32, 5.5, 4, 1 ], [ 34, 5.5, 5, 3 ], [ 36, 5.5, 7, 4 ], [ 38, 5.5, 9, 2 ], [ 40, 5.5, 11, 3 ], [ 42, 5.5, 0, 4 ], [ 44, 5.5, 2, 1 ], [ 46, 5.5, 4, 2 ], [ 48, 5.5, 5, 4 ], [ 50, 5.5, 7, 3 ], [ 52, 5.5, 9, 1 ], [ 54, 5.5, 11, 2 ], [ 56, 5.5, 0, 4 ], [ 58, 5.5, 2, 1 ], [ 60, 5.5, 4, 3 ], [ 62, 5.5, 5, 2 ], [ 64, 5.5, 7, 4 ], [ 66, 5.5, 9, 3 ], [ 68, 5.5, 9, 1 ], [ 70, 5.5, 0, 2 ], [ 72, 5.5, 2, 4 ], [ 74, 5.5, 4, 1 ], [ 76, 5.5, 5, 2 ], [ 78, 5.5, 7, 3 ], [ 80, 5.5, 9, 4 ], [ 82, 11.5, 11, 2 ], [ 84, 5.5, 0, 1 ], [ 86, 5.5, 2, 3 ], [ 88, 7.5, 4, 4 ], [ 90, 11.5, 5, 1 ], [ 92, 5.5, 7, 3 ], [ 94, 3.5, 9, 2 ], [ 96, 7.5, 11, 4 ], [ 98, 7.5, 0, 2 ], [ 100, 11.5, 2, 3 ], [ 102, 5.5, 4, 1 ], [ 104, 7.5, 5, 4 ], [ 106, 11.5, 7, 2 ], [ 108, 5.5, 9, 1 ], [ 110, 5.5, 11, 3 ], [ 112, 8.5, 0, 4 ], [ 114, 5.5, 2, 1 ], [ 116, 5.5, 4, 3 ], [ 118, 5.5, 5, 2 ], [ 120, 9.5, 7, 1 ], [ 122, 3.5, 9, 4 ], [ 124, 3.5, 11, 2 ], [ 126, 5.5, 0, 3 ], [ 128, 5.5, 2, 2 ], [ 130, 5.5, 4, 1 ], [ 132, 5.5, 5, 3 ], [ 134, 10, 7, 2 ], [ 136, 8, 9, 4 ], [ 138, 6, 11, 1 ], [ 140, 4, 0, 3 ] ] g = Gnuplot.Gnuplot(debug=1) g.title('REALLY COOL GRAPH') # (optional) g.xlabel('Start Time') g.ylabel('Duration') g('set data style linespoints') # give gnuplot an arbitrary command # Plot a list of (x, y) pairs (tuples or a Numeric array would # also be OK): #g.plot([[0,1.1], [1,5.8], [2,3.3], [3,4.2]]) g.plot(all) raw_input('Please press return to continue...\n') g.reset() # -- --------------- print '############### test splot ##################################' #g.splot(Gnuplot.Data(all, with='linesp', inline=1,)) g.splot(Gnuplot.Data(all, using=(1,), with='points', inline=1,)) #g.plot(Gnuplot.Data(d, with='lp 4 4')) #g.plot(Gnuplot.Data(d, cols=(0,1), inline=0), #Gnuplot.Data(d, cols=(0,2), inline=0)) #g.plot(Gnuplot.Data(d, cols=(0,1), inline=1), #Gnuplot.Data(d, cols=(0,2), inline=1)) #g.splot(Gnuplot.Data(all, with='linesp', inline=1)) # when executed, just run main if __name__ == '__main__': main() ------------ [snip] ---------------- |
From: Michael H. <mh...@ka...> - 2004-11-06 02:31:39
|
kevin parks wrote: > I have data that looks like: > > 0 5.5 0 1 > 2 5.5 2 4 > 4 5.5 4 2 > 6 5.5 5 3 > 8 5.5 7 1 > 10 5.5 9 4 > 12 5.5 11 3 > 14 5.5 0 2 > 16 5.5 2 1 > 18 5.5 4 4 > 20 5.5 5 3 It's easiest if you put your data into a file in the above format, rather than typing it into Python as a list constant. > [...] I would like to use splot and points of different colors to > convey as much info as i can. The X axis would be the time axis (that > would take care of the first two parameters) and the Y axis could be > the third column. Then for the fouth item, there are only four > possibilities (1, 2, 3, or 4), so i thought that i could use points of > red, blue, green, black to indicate that. I don't believe gnuplot offers a way to vary the style of points depending on a data column, so I believe you should read the data into Python, then use Python to pick it apart into four separate data sets, then plot the four sets in one plot using four different data styles. I believe the following script should do the trick. It uses some newer python features (2.2?) so parts might have to be changed if you are using an older version of python. Run it with the name of your data file as command-line argument. |#! /usr/bin/env python import sys import Numeric import Gnuplot [filename] = sys.argv[1:] data = [] for l in open(filename).readlines(): l = l.strip().split() data.append([float(l[0]), float(l[1]), float(l[2]), int(l[3])]) values = [1,2,3,4] # Here you can adjust the style options for each subset. The # interpretation of the numbers depends on what "terminal" you are # using. styles = { 1 : 'points pt 1', 2 : 'points pt 2', 3 : 'points pt 3', 4 : 'points pt 4', } subsets = {} for value in values: subsets[value] = [l for l in data if l[3] == value] g = Gnuplot.Gnuplot() items = [ Gnuplot.Data( subsets[value], with=styles[value], using=(1,3,), ) for value in values ] g.plot(*items) raw_input('Press enter to continue') Yours, Michael | -- Michael Haggerty mh...@al... |
From: kevin p. <ke...@ma...> - 2004-11-06 04:44:42
|
Michael Haggery, Thanks for taking the time to respond. This is a very elegant and instructive. I like the way that you set the styles in the styles dictionary. I would not have thought to do that. I wasn't aware that the data for Gnuplot.py, could also go into a separate file. That certainly makes for less cluttered code. I have be playing and tweeking the code you sent and tried 'steps' and linesplots and changing the grid. Everything works, but i wonder if there is not some more elegant way to represent this data. I am wondering if there is a way to not only use plot, but splot? Having x be the time (column 1), y be the value from column 2 and have z be the last value (column 4) and then having the linespoints being not only a different style but spread out on the z axis to boot. Perhaps that would be more salient? Problem is, when i try to change: items = [ Gnuplot.Data( subsets[value], with=styles[value], using=(1,3,), ) for value in values ] g.plot(*items) to use splot the whole thing barfs..... and i can't quite get my head around the splot entry in the manual. I wonder if you could suggest an splot approach that is more visually clear. It seems that splot is quite a different beast. In any case thanks for you instructions thus far. That is helpful handiwork in that script, a real pea-shooter! cheers, kevin On Nov 5, 2004, at 9:31 PM, Michael Haggerty wrote: > kevin parks wrote: > >> I have data that looks like: >> >> 0 5.5 0 1 >> 2 5.5 2 4 >> 4 5.5 4 2 >> 6 5.5 5 3 >> 8 5.5 7 1 >> 10 5.5 9 4 >> 12 5.5 11 3 >> 14 5.5 0 2 >> 16 5.5 2 1 >> 18 5.5 4 4 >> 20 5.5 5 3 > > > It's easiest if you put your data into a file in the above format, > rather than typing it into Python as a list constant. > >> [...] I would like to use splot and points of different colors to >> convey as much info as i can. The X axis would be the time axis (that >> would take care of the first two parameters) and the Y axis could be >> the third column. Then for the fouth item, there are only four >> possibilities (1, 2, 3, or 4), so i thought that i could use points >> of red, blue, green, black to indicate that. > > > I don't believe gnuplot offers a way to vary the style of points > depending on a data column, so I believe you should read the data into > Python, then use Python to pick it apart into four separate data sets, > then plot the four sets in one plot using four different data styles. > I believe the following script should do the trick. It uses some > newer python features (2.2?) so parts might have to be changed if you > are using an older version of python. Run it with the name of your > data file as command-line argument. > > |#! /usr/bin/env python > > import sys > import Numeric > import Gnuplot > > [filename] = sys.argv[1:] > > data = [] > > for l in open(filename).readlines(): > l = l.strip().split() > data.append([float(l[0]), float(l[1]), float(l[2]), int(l[3])]) > > values = [1,2,3,4] > > # Here you can adjust the style options for each subset. The > # interpretation of the numbers depends on what "terminal" you are > # using. > styles = { > 1 : 'points pt 1', > 2 : 'points pt 2', > 3 : 'points pt 3', > 4 : 'points pt 4', > } > > subsets = {} > for value in values: > subsets[value] = [l for l in data if l[3] == value] > > g = Gnuplot.Gnuplot() > items = [ > Gnuplot.Data( > subsets[value], > with=styles[value], > using=(1,3,), > ) > for value in values > ] > > g.plot(*items) > > raw_input('Press enter to continue') > > > Yours, > Michael > | > > -- > Michael Haggerty > mh...@al... > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: > Sybase ASE Linux Express Edition - download now for FREE > LinuxWorld Reader's Choice Award Winner for best database on Linux. > http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click > _______________________________________________ > Gnuplot-py-users mailing list > Gnu...@li... > https://lists.sourceforge.net/lists/listinfo/gnuplot-py-users |