[Tutor] help with splot & 3d data

Kent Johnson kent_johnson at skillsoft.com
Sat Nov 6 16:59:53 CET 2004


Well, this was quite a learning experience. Mostly I learned that gnuplot 
is not going to make it to my list of favorite tools...although to be fair, 
it did get the job done and it didn't take a lot of code to do it...it just 
took a long time to figure out the code, and it wasn't much fun.

Anyway...I don't think you want to use splot(), you only have two 
dimensions in your data - time and event. The other two variables are shown 
as delta-time and color. So just plain plot() does the job.

The 'vector' line style draws line segments from a start location to an end 
location. They are specified as x, y, xdelta, ydelta.

By looking at the arrowstyle demo I learned how to make the vectors draw as 
intervals - lines with little crossbars on each end - which seemed appropriate.

I divided the dataset into four and drew each one in a different line type. 
That makes them draw in different colors.

Anyway, here is the code, I hope it does what you want!

Kent

import Gnuplot

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 ] ]

     # Split the data into four datasets by stream
     # Each data set will be drawn in 'vector' style with a different color

     allData = []    # This will contain the four datasets

     for stream in [1, 2, 3, 4]:

         # Select the raw data for the stream
         rawData = [ item for item in all if item[3] == stream ]

         # Make a Gnuplot.Data to contain the raw data
         data = Gnuplot.Data(rawData,
             using=(1, 3, 2, '(0)'), # This gives 1-based index into the 
data for x, y, xdelta, ydelta
                                     # ydelta is a literal 0
             with='vectors arrowstyle %s' % stream,  # This sets the style 
of the dataset
                                                     # styles are defined below
             title='Stream %s' % stream) # This names the dataset for the 
legend

         allData.append(data)


     # Set up general plot parameters
     g = Gnuplot.Gnuplot(debug=1)
     g.title('REALLY COOL GRAPH') # (optional)
     g.xlabel('Time')
     g.ylabel('Event')

     # These lines create four vector styles that have bars at each end
     # The only difference between the styles is the line type they use,
     # which determines the color
     # Shamelessly cribbed from the gnuplot arrowstyle demo
     g('set style line 1 lt 1 lw 2') # Set a line style for the vectors
     g('set style line 2 lt 2 lw 2')
     g('set style line 3 lt 3 lw 2')
     g('set style line 4 lt 4 lw 2')

     g('set style arrow 1 heads size screen 0.008,90 ls 1') # Set an arrow 
style for the vectors
     g('set style arrow 2 heads size screen 0.008,90 ls 2')
     g('set style arrow 3 heads size screen 0.008,90 ls 3')
     g('set style arrow 4 heads size screen 0.008,90 ls 4')

     g('set key outside box')   # Include a legend; put it outside the graph

     # This actually does the plot
     # The * makes it treat the elements of allData as individual function 
arguments
     # It is the same as g.plot(allData[0], allData[1], allData[2], allData[3]
     g.plot(*allData)

     raw_input('Please press return to continue...\n')

if __name__ == '__main__':
     main()

At 08:32 PM 11/5/2004 -0500, kevin parks wrote:
>hi Python heads!
>
>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():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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] ----------------
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list