Set Axis Tick Labels in gnuplot - 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.

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

Plot number of requests per minute from timestamp csv

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'

Plot data with quarterly tic marks

I am plotting data that is recorded on the last day of the 3rd, 6th, 9th, and 12th months of the year. I would like the tic marks to be at 03/09, 06/09, ...
After reading the documentation, I thought this could be done by saying
set xtics "03/09", 7889220
because there are about 7889220 seconds in three months. But rather than starting with March, 2009, the tic marks start on the next day, shown here (with the remaining part of the plot removed):
Is there a way to force the tic marks to be at end of months?
UPDATED:
The date format in the input file is mm/dd/yyyy, which I am reading with these commands:
set xdata time
set timefmt "%m/%d/%Y"
and I'm then doing this:
set format x "%m/%y"
set xrange ["03/31/2009":"12/31/2010"]
Discussion:
The Gnuplot behaviour when you use set format x "%m/%y" will be to place xtics at month boundaries since that really is what this command is asking Gnuplot to do.
To solve your problem there may be two posisble approaches that you can take here depending on how large your data set is.
Approach1:
If you do have time stamps in your data file one possibility is to just use the xtics directly for plotting (suitable if you have a large dataset)
So you do away with all the time commands in your script and just use
plot 'Mydata.dat' u 2:xtic(1) w points
Approach2:
The other option is to set custom xtics, however you will have to do this by hand and if you have a large dataset this might be cumbersome (suitable if the dataset has tens of points)
set xtics ("03/09" "03/31/2009", "06/09" "06/30/2009", "09/09" "09/30/2009", "12/09" "12/31/2009")
Will give you tics at the exact days you need them to be.
Assumption:
I assume that the first column in your file are the time stamps and second column are the data values. Below, I show a graph where I use the manual setting approach (Approach 2).
Result with dummy data:

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?

Resources