Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a plot in gnuplot with the lowest value automatically subtracted from the y data?

Tags:

bash

gnuplot

I am plotting the creation times of a large batch of files in gnuplot to see if they are created linearly in time (they are not).

Here is my code:

#!/bin/bash

stat -c %Y img2/*png > timedata

echo "set terminal postscript enhanced colour
set output 'file_creation_time.eps'
plot 'timedata'" | gnuplot

The problem I have is that the y data are the creation time in seconds since unix start time, so the plot just has 1.333...e+09 on the y-axis. I would like to have the creation time of the first file scaled to zero so that the relative creation times are readable.

I encounter this problem in a number of data-plotting contexts, so I would like to be able to do this within gnuplot rather than resorting to awk or some utility to preprocess the data.

I know the first time will be the smallest since the files are named serially, so is there a way to access the first element in a file, something like

`plot 'data' using ($1-$1[firstelement])`

?

like image 509
andyras Avatar asked Apr 05 '12 19:04

andyras


1 Answers

I think you can do something like that...(the following is untested, but I think it should work...). Basically, you have to plot the file twice -- the first time through gnuplot picks up statistics about the dataset. The second time through, you use what you found on the first run-through to plot what you actually want.

set terminal unknown
plot 'datafile' using 1:2
set terminal post enh eps color
set output 'myfile.eps'
YMIN=GPVAL_Y_MIN
plot '' u 1:($2-YMIN)

If you have gnuplot 4.6, you can do the same thing with the stats command. http://www.gnuplot.info/demo/stats.html

EDIT It appears you want the first point to provide the offset (sorry, misread the question)...

If you want the first point to provide the offset, you may be able to do something like (again, untested -- requires gnuplot >= 4.3):

first=0;
offset=0;
func(x)=(offset=(first==0)?x:offset,first=1,x-offset)
plot 'datafile' using (func($1))
like image 113
mgilson Avatar answered Oct 31 '22 18:10

mgilson