Plot number of requests per minute from timestamp csv - gnuplot

I have a .csv file with unix-timestamps, each timestamp depicts one request on a server.
Now I would like to plot a diagram of the number of requests for each minute/hour in the diagram.
Like a load-diagram.
How can this be done using gnuplot?

Yes, gnuplot can plot such diagrams. In your case I suggest to use the boxes plotting style which, in contrast to the histogram style, uses a conventional numerical axis.
To count the requests per minute or per hour you must bin the time values properly. To do that, use the tm_* functions which gnuplot provides, to cut off the seconds or minutes and bin the data properly to full minutes or full hours (see also Gnuplot y-axis value from x(dates)).
To bin to minutes, use
bin_minutes(t) = t - tm_sec(t)
This just subtracts the seconds from the original time stamp. This would map all values with seconds from 0 to 59 to the same minute. To have the boxes centered properly between 0 and 59 seconds, add 30 seconds.
bin_minutes(t) = t - tm_sec(t) + 30
To bin to hours, use
bin_hours(t) = t - tm_min(t)*60 + 30*60 - tm_sec(t)
Furthermore, you must know, that gnuplot 4.6. uses the 1. Jan 2000 as reference for all time functions, and gnuplot 5.0 uses the 1. Jan 1970, like a normal Unix timestamp. So, depending on your gnuplot version you must subtract an offset from your Unix time stamp in order to get the correct values (see also the discussion in my answer to Can I plot times when they're given as UNIX timestamps with added milliseconds?).
Putting all together should give a working script (cannot test it since you didn't post any data):
set yrange [0:*]
set xdata time
offset = (GPVAL_VERSION < 5.0 ? 946684800 : 0)
bin_minutes(t) = t - tm_sec(t) + 30
bin_hours(t) = t - tm_min(t)*60 + 30*60 - tm_sec(t)
plot 'file.dat' using (bin_minutes($1-offset)):(1) smooth frequency title 'requests per minute',\
'' using (bin_hours($1-offset)):(1) smooth frequency title 'requests per hour'

Related

How do I add a certain amount of numbers data together in gnuplot from a text-file?

I have a text-file that has generated values every second by a radiation monitor via a serial-controller. How do I add this data up to hours, days, etc. for gnuplot to plot? Preferably added via gnuplot rather than a separate file.
For example:
30
32
28
30
32
Would be 5 seconds of data, how can I combine this via the gnuplot .dat file to generate a graph that is over an hour/day rather than every second?
I am new to gnuplot.
30
32
28
30
32
Generate a graph that is over an hour/day rather than every second.
If a new line is generated every second, then plotting in bins of 60 will give counts/minute, plotting in bins of 3600 will give counts/hour, etc.
set ylabel "cts/minute"
plot "datafile" using 0:1 bins binwidth=60
"using 0:1" tells the program to generate bins from the line number and take the value of each line from column 1. You have not said what you would like the x axis to show. Let's assume you want elapsed time. You may also want to specify a plot style (points, lines, steps, ...) The commands would be
set ylabel "cts/minute"
set xdata time
set xtics 60 # one tic label every minute
set xtics format "%tH:%tM" # elapsed time in hours:minutes
set xrange [0:*] # start at zero elapsed time
plot "datafile" using 0:1 bins binwidth=60 with steps

Plotting of multiple graphs on a single tablet screen

I m designing an application that is suppose to plot multiple graphs for sensor data on a single tablet screen.All the graphs shall have common x-axis that displays time(1 sec to 2 mins) but y axis data for all the plots is different. I was able to successfully plot all the graphs but not sure how to display a common x- axis for the graphs? Has anyone tried doing this?
You can definitely display multiple plots (each containing it's own graph space) with identical domain (xAxis) labeling. The key to doing this is constraining each plot's boundaries in the same way.
Let's say that for the sake of this example your series data uses a time offset in milliseconds as it's x values. For 1 second that gives us:
xMin = 1000 (1 second)
xMax = 120000 (2 minutes)
which translates to:
plot.setDomainBoundaries(1000, 12000, BoundaryMode.FIXED);
If you're using real timestamps instead of offsets, the same principle applies. You'll just have to decide what the starting timestamp should be and then calculate the ending timestamp by adding 120000 to it.

Month tics, how to set

In GnuPlot:
How to set xtics to 1st of every month?
set xtics would not work for me the as the number of seconds per month varies.
set xmtics does not work for me because months are displayed without years and it is not shown to which year belongs a month.
It seems to me that gnuplot handles time surprisingly well. I assume your data in the following (or similar) format:
05/17/12 0.8188064359
05/18/12 0.97297057
05/21/12 0.9012782504
I used random numbers spread over year and a half to test this.
So first you need to tell gnuplot your x coordinate is time and what is the format it is written in:
set xdata time
set timefmt "%m/%d/%y"
From now on, gnuplot is expecting all ranges in the specified format, including the xtics and xrange commands. The only exception is the increment in xtics which should be in seconds.
set xrange ["05/01/12":"08/01/13"]
set xtics "01/01/12",2592000,"08/01/18"
Now you might argue, that the number of seconds in each month is different by a whole day (actually 2 days in February). However, gnuplot authors seem to think about this as well and they solved the problem to our liking. In other words, the above will ensure tics every 1st of a month. Here, I can only suggest to specify the printing format of the xtics
set format x "%d %b %Y"
which will result in "01 May 2012", etc.
After this, plot your data. Oh, when using time on x-axis, gnuplot requires using in the plot command, so even with a trivial data file as I had, I had to use
plot 'data.txt' using 1:2
I used gnuplot version 4.6 patchlevel 0 and output to postscript terminal.
If you will have different experience with your version or terminal, let me know in the comments.
set xtics will work ok if you set the number of seconds to the average number of seconds in a month.
I worked this out by assuming 365.2425 days in the average year, dividing by 12 to find the average days per month (approximately 30.4), multiplying by hours per day then seconds per hour, i.e:
365.2425 / 12 * 24 * 3600 = 2629746
Assuming your data was formatted as year-month and value, i.e:
2016-01 10000
2016-02 12000
2016-03 10500
Then you’d need commands such as:
set xdata time
set timefmt "%Y-%m"
set format x "%b/%y" # Or some other output format you prefer
set xtics "2016-01", 2629746, "2016-03"
plot "mydata.dat" using 1:2 with linespoints
If you want to set xtics in multiples of months then just add a multiplier in the set xtics line, e.g to get quarterly marks:
set xtics "2016-01", 3*2629746, "2016-12"
I found that if I assumed the number of days in a month to be 30 or 31 then some at some point in the chart, months would be missing their xtics.

Plot day events from date and value in Gnuplot

I have data of this format:
2011-06-22 22:33:19 23 15
2011-06-23 09:46:13 12 79
2011-06-24 12:31:09 31 4
2011-06-24 17:34:10 7 2
2011-06-25 16:42:43 44 14
2011-06-25 20:26:52 54 9
2011-06-26 19:34:29 217 28
How can I create a histogram of daily activities with Gnuplot? By default, using these settings:
set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"
set style data boxes
set grid
plot 'data' using 1:3 t "ins", \
'data' using 1:4 t 'dels'
the boxes will fit next to each other. But I would like to leave noneventful days at 0. Just like the Reputation graph here in StackOverflow behaves. If there's nothing on a given day, it should leave an empty place in the graph. If there's one event for one day, that box should be about the maximum width for one day. If there are more than one to given day, they all should be fit to that width.
Setting boxwidth is tricky because any value seem to give me 1-pixel wide "boxes".
Thanks kindly.
if I understand you correctly, then what you are trying to do is to my knowledge not possible with gnuplot. Or at least not in an easy way. And this is the reason why I think you'll have a hard time:
You cannot plot different box widths in a single plot. So trying to plot no box on an "non eventful" day and a single column on a day with one event will work just fine. Plotting multiple columns on one day where more than one event occurs in the same plot will fail because:
You cannot set different box sizes in the same plot
You need to offset the boxes on the same day according to the amount of events
There are ways to work around that problem for example plot two boxes of the same color next to each other to "simulate" a single box on one day and then make use of the smaller box width on days with two events. But this will very soon get pretty hairy and hard to maintain.
Maybe you want to think about using a different plot style? Take a look at histograms like here. Maybe one of the styles suites your data better. Or you could think about splitting your plot up into multiple plots?

Set Axis Tick Labels in gnuplot

My input to gnuplot looks something like this:
1:00am 1 10
1:00am 30 12
1:01am 60 18
1:01am 90 20
1:02am 120 21
...
The first column contains (what I'd like to be) the X-axis labels, while the second column contains the X-axis values. (In actual point of fact, I have one row per second, so many rows have '1:00am' as in the label column, then '1:01am', etc).
I can plot the values I care about using something like:
gnuplot> plot "my_data.txt" using 2:3 with lines
And I can set the X axis to have a tick (and label) every 30 minutes (which is 30 * 60 = 1800 seconds), but the label it uses is the X axis value (something like 3600 for one hour).
What I can't get is for the labels that show up under each tick to use the values from the first column. I can't even find anything that looks promising in the gnuplot manual. I've got to assume this is possible, I just don't know where to look.
Edit:
Progress, I've discovered the xticlabels() parameter to plot. The problem is that it seems to plot these labels for every xtic (that is, one per second in my data set), rather than at the intervals defined by set xtics.
Edit and Answer:
Turns out gnuplot has support for graphing time-series data. For the curious:
set timefmt "%H:%M:%S"
set xdata time
This causes the X axis data (once reformatted to match that pattern) to be interpreted as time-series data. This is then combined with
set xtics 3600
Formats the X axis as I described.
Sorry caught this late, but yes was to suggest set timefmt ... ; set xdata time. I always pass in my time format as %s but just as a common practice.

Resources