JerriHopefully everybody here is using linux and has an installed version of gnuplot on his harddrive.
Jerrignuplot is also available for windows and most of the things we will discuss today will also be possible with windows. So just try it.
JerriI will only give you a small outline about creating simple plots with gnuplot.
JerriFrom there on try to learn more yourself. The gnuplot help-system is rather good and will give you also a lot of examples.
JerriNow, everybody ready?
sundanceyupppppppp
ticallion:)
JerriFine. So lets start
JerriLater on we will need some data for a very interesting application of gnuplot.
JerriTo gather this data please download the following script from
-!- maestro [mathias@adsl-97-199-zh1.tiscali.ch] has joined #gnuplot
Angus_Youngyep :)
Jerrihttp://jerri.hopto.org/gather_load.sh.txt
JerriRename to gather_load.sh
desaster404
-!- nox5000 [~nox5000@200.84.181.151] has quit ["gtkBitchX-1.0c19 -- just do it."]
ticallionhowto instead of hopto
tarzeauforbidden
ticallionoops, sorry
JerriDamn. One moment please.
JerriOK.
Jerrihttp://jerri.hopto.org/gnuplot/gather_load.sh.txt
JerriSorry
-!- nox5000 [~nox5000@200.84.181.151] has joined #gnuplot
JerriAll of the following files will be found there.
JerriHello nox5000
nox5000:)
JerriLater on we will need some data for a very interesting application of gnuplot.
JerriTo gather this data please download the following script from
Jerrihttp://jerri.hopto.org/gnuplot/gather_load.sh.txt
JerriRename to gather_load.sh
JerriTake a look into it. It just gets the current date and the current load and output them.
JerriNow in one window lets start this script and save the output to load.data
Jerrish gather_load.sh | tee load.data
-!- decaf [~mehmed@abn198-126.izmir-ports.kablonet.net.tr] has quit [Read error: 60 (Operation timed out)]
JerriPlease do so now. I how you see a lot of number appearing.
Jerrihow=hope
JerriNow let this run while we start with the course.
Jerrilets start with our first plot.
Jerri- Plot a graph
JerriStart gnuplot in an xterm. You will see a prompt like "gnuplot> ". Now enter
Jerriplot sin(x)
JerriCongratulations. You created a plot of the sinus-function in a new x-window.    gnuplot normally plots graph with x running through a range and plotting the result of a function on x.
JerriHopefully everybody sees this. If something goes wrong please say so!
Jerriin our plot x-axis goes from -10 to 10. y-axis goes from -1 to 1.
JerriIf you don't specify the ranges for the axis' gnuplot tries to plot the whole graph into the window.
JerriLets change the xrangee to see one period of the sinus
Jerriset xrange [0:6.282]
Jerrireplot
Jerrireplot plots the functions you last specified with the plot command.
JerriEverything ok till now?
ticallionJerri: great here
JerriIf you need help with one of the commands for gnuplot or a summary of commands, use the help command. Try for example
Jerrihelp replot
Jerrior do a "help help" to get information about the help system.
JerriTo get out of help just enter return till you again see the gnuplot-prompt.
JerriNow if you want to compare the sinus and the cosinus you would plot both graphs in on window. Lets do it
Jerriplot sin(x), cos(x)
JerriYou now see two plots in one window.
Angus_Youngso x is in radians?
JerriAngus: Yes. sin and cos are the normal c-functions, which get x in radians.
Jerri- Change some properties of the plot
JerriIf you want to get some numbers out of this plot it would be nice to have a grid. Just enter
Jerriset grid
Jerrireplot
Jerriand your plot is better to read.
Jerrignuplot normally magically gets the correct number of gridlines.
JerriIf you want more or less tics (gridlines) on the axis' you use xtics or ytics.
JerriLets for example put the gridlines on the yaxis on pi and pi/2.
Jerriset xtics 3.141/2
JerriAs you can see everywhere you can enter a number, you also can enter a term.
JerriFor more information about tics do help xtics. Do so also with other settings or commands I may bring up. But not now.
nox5000set xtics 3.141/2
JerriSave this for later reading... :)
nox5000oops sorry
Jerri:)
JerriNow to nice up the plot how about some headline and titles for the axis'.
Jerriset title "My first plot"
Jerriset xlabel "x"
Jerriset ylabel "f(x)"
Jerrireplot
JerriI hope you all see a nice plot now.
JerriOutput the plot for further use
JerriWe will now take this nice plot and output it to a file.
Jerrignuplot knows a lot of filetypes. To see them do
Jerrihelp terminal
JerriDo this now!
JerriAt the bottom of this helppage you see the filetypes, your gnuplot can output.
JerriPlease check if your gnuplot-version can output png
JerriNormally it can. If not, please tell me now!
oktaythis is not like Excel at all.. lol
-!- A_Young [~joepublic@203.134.130.12] has quit [Read error: 110 (Connection timed out)]
JerriIt isn't! :-) But you will see later you have great opportunties with this tool.
JerriTo output the active plot to a png file please enter the following commands
Jerriset terminal png
Jerriset output "myfirstplot.png"
Jerrireplot
JerriNow nothing was output to the X-window.
JerriOpen up another terminal and check if there is a new file in the directory you started gnuplot from. There should be a new file "myfirstplot.png".
JerriUse for example display from the image magick kit to display that graph.
JerriYou might see, that the output to the png doesn't look exactly like the x-window-output. This is normal. gnuplot optimizes the output for the given terminal.
JerriIf you want to continue using gnuplot after changing the terminal to a file enter:
Jerriset terminal X11
JerriThe your output goes again to the xwindow.
JerriThe = Then
JerriEverybody saw this picture?
JerriIt should look like this one
ticallionJerri: uhuh
Angus_Younghmm the file is 0 bytes
Jerrihttp://jerri.hopto.org/gnuplot/myfirstplot-auto.png
tarzeauAngus_Young: same here
JerriOh...
JerriMhm.
Angus_Youngdamn this debian ;)
oktayYou might not have the PNG libraries on your systems
JerriYou didn't restart gnuplot, did you?
desasterit worked here fine (debian woody)
Angus_Youngno didn't restart it  .. the x11 window still displays the graph and so on
nox5000yes it worked
oktayAngus_Young~: debian?
ticallionworked here also
Angus_Younghmm ..
walkdand here
JerriMhm. Maybe you forget to set the terminal. Any warnings?
Jerriset terminal png
Jerriset output "myfirstplot.png"
Jerrireplot
JerriPlease try again.
tarzeauworked now
JerriAngus?
maxydebian, and it worked for me...
Angus_Youngstill 0 bytes
maxymost likely you forgot replot...?
Angus_Youngi have libpng runtimes installed
Angus_Youngno i did the replot
desasterperhaps try postscript?
JerriThree command lines.
nox5000im also using woody ;)
tarzeaui've got sid/i386 here
Angus_Younghmm .. even tried deleting it .. i will try postscript
Jerridesaster: We will do postscript later on. Maybe Angus just ignore for now and we will go on.
oktaytarzeau~: do you have png devel packages installed ?
oktaymaybe that's what Angus_Young needs
tarzeauoktay: no you need -dev packages only for compiling/developing things
tarzeauoktay: no that's not it
oktayok
Angus_Youngmm the .ps file is also 0 bytes
tarzeauAngus_Young: df -h ? enough space?
tarzeauAngus_Young: i've had permission denied where i wanted to plot...
JerriThis is strange. You seem to have a problem with your system.
Angus_Youngbizarre .. i can't see the reason
Angus_Youngah
Angus_Young"This document may describe drivers that are not available to you because they
Angus_Young were not installed"
Angus_Youngmaybe there is another gnuplot package ofr output drivers?
JerriLets take a look later on and just go on with the course. Saving the plot is not that necessary for this course.
Angus_Younganyway, continue with the lesson if you like .. i'll try to figure it out and watch at the same time ;)
JerriFine.
JerriUsing gnuplot as a filter
Jerrignuplot is able to read the commands also from stdin.
JerriSo you could have entered all of the commands line by line into a file and pipe that file to gnuplot.
JerriPlease quit from gnuplot now with the command quit.
JerriDownload the following file from
Jerrihttp://jerri.hopto.org/gnuplot/myfirstplot.plot
Jerritake a look inside and pipe it to gnuplot.
Jerricat myfirstplot.plot | gnuplot
JerriIt will create the file myfirstplot-auto.png and then exit.
JerriAngus: Sorry, but for you it will create a zero sized file.
JerriNevertheless: You would get this output
Jerrihttp://jerri.hopto.org/gnuplot/myfirstplot-auto.png
JerriYou can also load that file at the gnuplot prompt with
tarzeauyou can also have a file executable with this in the first line: #!/usr/bin/gnuplot
Jerriload "myfirstplot.plot"
JerriIt will do the same as piping it in.
JerriYou can use the save command to save all of your current settings and the last plot command to a file. Take a look at "help save" to get further informations.
Angus_Youngheh yay the file contains data this time :)
Jerribtw: Just for the curious. gnuplot also can plot 3d-surfaces.
oktayJerri~: these are allwhile inside the gnuplot shell right?
JerriCool :-)
tarzeauJerri: surfaces too?
tarzeauJerri: not just wires?
Jerriload "mz..." yes.
nox5000:)
JerriBoth tarzeau. But not very nice.
JerriJust as an example, restart gnuplot
JerriAnd then do
Jerrisplot sin(x)*cos(y)
Jerri(for further information do "help splot")
JerriIf you want to get a hiddenline plot, add
Jerriset hidden3d
Jerrireplot
-!- decaf [~mehmedk@adsluser-2562.adsl.ttnet.net.tr] has joined #gnuplot
JerriBut we would have to set a lot more settings to get a really nice picture. Please use the help system to get further information.
JerriScientific Use
JerriThis might get a little bit complicated. So ask if you have questions.
Jerrignuplot was specifically build to output data to a graph and put that into your work.
JerriWe will now use some data, plot that into a graph, fit a function to the data and output all of it to a postscript-file.
JerriFirst please load the datafile we will use from the following url
tarzeautry splot sin(x/5)*cos(y/6)
Jerrihttp://jerri.hopto.org/gnuplot/scientific.data
Jerriand take a look at it. Consider we did an experiment and saved the data for
Jerrix and y and the errors we think we did during measurement in both directions, meaning:
Jerrix y dx dy
JerriWe will now get some information about this data and some specific parameters.
Jerri(btw: I created this data using a small perl-script. So it's not really scientific data.. :-)
Jerri- Plotting data
JerriFirst restart gnuplot and let us plot just the position of the points in the dataset:
Jerriplot 'scientific.data'
Jerrignuplot reads the data from the file and uses the first two rows to create the plot.
JerriDoes everybody see this plot now?
Angus_Young:)
Jerriwe can be even more specific and tell gnuplot to use the first and the second row to plot the graph via
Jerriplot 'scientific.data' using 1:2                                                
Jerriyou can see this seems to be a gaussian curve based around 4. Lets connect the dots
ticallionJerri: just tried out succesfully tab-completion on the filename, very cool
Jerriplot 'scientific.data' using 1:2 with lines
JerriYeah. Gnuplot normally uses the readline-library and does nearly the same as the bash-shell.
Angus_Youngticallion: which version do you have? did you need a special setting for tab completion?
JerriPlease be aware, that the order of the options to plot are important. The following won't work
ticallionAngus_Young: nope
Angus_Youngdoh
ticallionAngus_Young: 3.7.2
JerriAngus: It has to be compiled with readline-library.
JerriPlease be aware, that the order of the options to plot are important. The following won't work
Jerriplot 'scientific.data' with lines using 1:2
JerriErrors
JerriNow lets use that extra data in the 3rd and 4th row to output some errorbars
Jerriplot 'scientific.data' using 1:2:3:4 with xyerrorbars
JerriIf you just want to see the yerrorbars try
Jerriplot 'scientific.data' using 1:2:4 with yerrorbars
JerriTaking a look on the data we can assume that we here have some sort of gaussian data. So we might be interested in the sharpness, position and altitude of the peak.
JerriSo lets try to put a gaussian curve through this data. We begin by defining the function. Enter the following at the gnuplot prompt:
Jerrigauss(x)=altitude/exp((sharpness*(x-position))**2)
JerriWe can guess the parameters using the plot. So enter:
-!- decaf [~mehmedk@adsluser-2562.adsl.ttnet.net.tr] has left #gnuplot []
Jerrialtitude=4.1                                                                    position=6.9
Jerrisharpness=1.5
JerriThis are three lines!
JerriNow plot data and our guessed curve.
Jerriplot 'scientific.data' using 1:2:4 with yerrorbars, gauss(x)
JerriYou should see now the datapoints and a curve going through them.
JerriMore or less.
Jerri Lets redefine our guesses:
Jerrialtitude=4.2
Jerriposition=6.7
Jerrisharpness=1.0
Jerriand replot
Jerrireplot
JerriThis is better, but not good enough. We now could readjust and replot time after time and check if we already got the correct parameters.
JerriBetter yet: Let the computer do the work. gnuplot is able to fit curves to given data and present you with the necessary parameters.
JerriFitting
JerriAs we already defined the curve and also gave some good guessed parameter to begin with just start the fit-routine
Jerrifit gauss(x) 'scientific.data' using 1:2:4 via altitude, position, sharpness
JerriThis tells gnuplot to fit the curve to the data by variing the given parameters.
JerriAfter some iterations you get the "Final set of parameters" for your parameters and some Standard Error.
JerriThe fit-Routine already set the parameters to the optimal values, so now just replot data and curve.
Jerrireplot
JerriAnd voila. Perfect fit. :)
JerriDid it work for everybody?
ticallionnice!
Angus_Youngvoila!
JerriFor further information about fit take a look at "help fit".
JerriIf you consider using the fitting-routine from gnuplot for scientific work you even should take a look at some books which describe the fitting algorithmus gnuplot uses: nonlinear least-squares (NLLS) Marquardt-Levenberg algorithm.
JerriWithout that knowledge you don't really know what the errors mean you are presented by gnuplot.
JerriFurthermore the fitting-routine doesn't work with all functions equally good. Sometimes the algorithm doesn't converge.
JerriOutput the graph for your work
JerriNow lets create a graph which presents our results and helps understanding your work... :)
JerriFirst we don't want to see the reader which file we used for this data, but which data he sees on our graph:
Jerriplot 'scientific.data' using 1:2:4 title 'measured data' with yerrorbars, gauss(x) title 'guessed curve'
JerriNow lets show where the peak exactly is. We will draw an arrow showing the peak of the measured data.
Jerriset arrow 1 from position,2 to position,gauss(position)
Jerrireplot
JerriHere we define an arrow with the number 1 pointing from position,2 to the peak of the curve. Lets write "Peak" at the beginning of the arrow:
Jerriset label 1 "Peak" at position,2-0.1 center
Jerrireplot
JerriNow lets draw some lines showing the border of the gauss-peak by defining two arrows without head.
Jerriset arrow 2 from position-1/sharpness, graph 0 to position-1/sharpness, graph 1 nohead
Jerriset arrow 3 from position+1/sharpness, graph 0 to position+1/sharpness, graph 1 nohead
Jerrireplot
JerriThe first number in the arrow definition is sort of a name for the arrow. You can edit the endings of the arrow by using the same id in a new set command.
JerriAnother thing: You can refer to a point on the graph with several different coordinate-systems. For the two arrows above I used graph-coordinates, which start at 0,0 on the left,lower corner and go to 1,1 in the right, upper corner.
JerriMore info about coordinate-systems for arrow and label can be found using
Jerrihelp coordinates
JerriNow set a title for this graph
Jerriset title "My scientific graph"
Jerriand output this graph to a postscript file
Jerriset terminal postscript color
Jerriset output "scientific.ps"
Jerrireplot
JerriUs gv (or another postscript-view) or just pipe the postscript-file to a postscript printer and admire your wonderful scientific graph.
-!- walkd [~walkd@host81-132-91-174.in-addr.btopenworld.com] has left #gnuplot []
JerriHopefully everybody could follow and see this graph on his display.
JerriNow lets leave the scientific use and go on to some geek use of gnuplot. :)
JerriNot so scientific Use
JerriOutput data over time (with simple script, e.g. load average)
JerriDo you remember the script we started at the beginning. Now we will take a look at this data.
JerriDon't stop the script. Just go to the directory you started this script in.
JerriIf you take a look at the data, this time x is a date. So gnuplot has to interpret the first two rows as a date and a time.
JerriLets tell gnuplot to interpret the time correctly. Restart gnuplot in the directory you started the script where the datafile lies.
JerriNow enter
Jerriset xdata time
Jerriset timefmt "%Y.%m.%d %H:%M:%S"
JerriThis tells gnuplot that your x-coordinate is of timeformat and how it looks like.
JerriNow plot the data
Jerriplot 'load.data' using 1:3 title "Load Avg" with lines
tarzeaucould i also use unix time (date +%s) ?
JerriNote that we used 1:3. This is necessary as gnuplot will interpret the 1st and 2nd row as a date and time. But our load is in the third row.
Jerritarzeau: Sure, but you wouldn't get nice labels on the axis.
ticallionJerri: very nice
JerriTry and see.
JerriAgain: Using gnuplot as filter
JerriNow lets use gnuplot to create a onthefly graph of the current load on the system.
JerriDownload the following gnuplot-File.
Jerrihttp://jerri.hopto.org/gnuplot/show_load.plot
JerriIt contains the same commands as we just did, with a size-setting and png as output-terminal.
JerriNote that we didn't give gnuplot a filename with output. This tells gnuplot to output the graph to stdout. Now at an normal bash-prompt do the following
Jerricat show_load.plot | gnuplot | display -
JerriHopefully you all have image magick installed.
JerriWe just created an onthefly-picture with gnuplot which wasn't saved to disk.
JerriTo save this png to disk just do
Jerricat show_load.plot | gnuplot > myload.png
JerriUsing other commands in a gnuplot-script
JerriJust for your information:
JerriYou can do things like
Jerriplot "<cat scientific.data" using 1:2 with lines
JerriMeaning gnuplot starts a command to output the lines and takes these lines to get the data for the graph.
JerriEven s.th. like this is possible
Jerriplot "<echo 'select data1,data2 from table' | mysql data | head +2" using 1:2 with lines
JerriDon't try it, it's just an example.
Jerriwhich means you can create a plot with data from a database on the fly.
JerriDo "help plot special-filenames" to get more information.
JerriWe come near the end now. One last thing to show you:
JerriOutput as graph for e.g. website
JerriConsider this an exercise for the reader. Download
Jerrihttp://jerri.hopto.org/gnuplot/show_load.cgi.txt
Jerriand take a look at the script. It shows you in principle how you can create graphs on the fly to show on a webpage. Rename from show_load.cgi.txt to show_load.cgi.
JerriWell. I hope you did get a small insight into using gnuplot. Now use the really good help system from gnuplot and go further.
tarzeauone moment
JerriQuestions:
tarzeauthere's bezier curves i found out lately and colors and multiple curves!
-!- sundance [~Dervish@ist00510.ultranet.com.tr] has quit ["Client Exiting"]
JerriWell. Ask.
tarzeausee http://apt-get.mine.nu/woody-o-meter/ for those interested
tarzeauJerri: i have the datafiles just one number each line, how could i add dates to the plots?
tarzeauthe data is filled once daily, i would like a date for each day back to when i started
oktayJerri~: Thanks. This was a pretty good introduction to gnuplot.
ticallionJerri: question...
ticallionlemme backtrack a bit, there was no difference between "plot 'scientific.data' using 1:2" and "plot 'scientific.data'"
tarzeaucan i have comments in datafiles?
Jerritarzeau: Show me the data. I don't understand.
ticallionJerri: at the beginning of the tutorial
tarzeauJerri: i have at the same url base.dat std.dat and total.dat
Jerriticallion: Yes. Normally gnuplot uses 1 and 2. But you can have a file with several rows and then can define to plot 1 and 4 with
Jerriusing 1:4
ticallionJerri: understood
Jerritarzeau: Which url?
Jerriticallion: So you can output all of your data to one file and do several different plots from it.
tarzeauJerri: http://apt-get.mine.nu/woody-o-meter/
ticallionJerri: i get it :)
Angus_Youngthanks jerri - it made learning a complex tool very easy :)
nox5000:)
JerriI hope I was of some help. Thanks too
-!- oktay [~foo@ool-4353c8e6.dyn.optonline.net] has quit ["Lost terminal"]
Angus_Youngsomething i would never have tried in fact on my own .. i would probably try to write a custom perl script for each case instead of learnign a useful tool
Jerri:) Who doesn't
ticallionJerri: very nice, gnuplot used to scare me, needed to use it at one point for plotting tc qdisc usage... gave up on it, I'm ready to give it another go, am sure it'll go smoothly with some "help bla" in gnuplot
ticallion:)))
ticalliontarzeau: when will the blog be online?!
tarzeauticallion: soon after these questions
Angus_Youngtarzeau: did you restart gnuplot before it wrote the output file before? i think that is what fixed it for me .. perhaps there is a small bug that it won't write output files the first time it runs
ticalliontarzeau: very nice, thank u for advertizing this :)
tarzeauJerri: thank you! that .cgi and |display - were nice tricks i've never used that way
Jerritarzeau: How often do you get the data. I think you have to change the routine which creates the data to do something like my gather_load.sh script.
tarzeauJerri: every morning (daily)
tarzeaui would've liked thicker curves/lines but that doesn't seem to work with png but fine with postscript output
JerriMhm. You have to change your script to get it. Can't help there now.
tarzeauJerri: ok now that i know how :)
Jerritarzeau: Yeah. Unfortunatelly gnuplot has some drawbacks. But most of the time its a great tool.
tarzeauplot "your.dat" smooth bezier with lines
tarzeauJerri: you probably know smooth bezier , don't you?
Jerritarzeau: Sure. But it's not one of the nicest things in gnuplot.
JerriIt doesn't really work with a lot of datapoints.
-!- michael [michael@bakker.li] has left #gnuplot []
JerriIts rather slow and hides peaks really good.
JerriSo I only use it sometimes.
Angus_Youngbezier is not a scientific way of fitting data .. it's just a way of making nice curves that look pretty :)
tarzeaui've never claimed something else :)
JerriAngus: Right. Thats what I wanted to say... :)
JerriSo, any more questions regarding gnuplot?
ticallionJerri: none, ur coverage was very easy to swallow :)
tarzeauit was very fresh, not too slow, not too fast, not boring either! thank you
JerriWell. So I thank you all very much for participating and wish you a good evening (or night, or what ever... :-)
Angus_Youngis the windows version good? i am just curious .. maybe some day i would be trapped at work and need to make a graph or something ;)
nox5000jajajaja :)
Angus_Youngta night :)
JerriAngus: It works like the linux gnuplot version..
Angus_Younggood :)
nox5000bye... :)

Generated by irclog2html.pl 2.1 by Jeff Waugh - find it at freshmeat.net!