MrSparkly
MrSparkly

Reputation: 639

Gnuplot: set the start point for X-axis labels for timestamped data

In Gnuplot, I'm plotting timestamped values. The data looks like this:

2019-06-01 18:23:56, 508.56
2019-06-07 18:23:56, 508.56
2019-06-08 13:00:00, 492.95
...

I don't want the xtics labels to be too dense, so I print them only every 2 weeks:

set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"
set xtics format "%m/%d"
set xtics 3600*24*15
unset mxtics

It works, the labels are in 2-week intervals, but they're not pleasingly aligned to the beginning of a month. The xtics labels I see are: 06/13, 06/28, 07/13 etc. I'd like to start the labeling from the first day of a month, then the 15th of a month etc. My actual data starts on 2019-06-07. I added a fake first entry for 2019-06-01 (see above), but I get the same labeling sequence (06/13, 06/28 etc). Is there a way to set where the first xtics point should be (06/01 or 06/15) without having to manually specify "xticslabels" in the data file?

Upvotes: 1

Views: 459

Answers (1)

theozh
theozh

Reputation: 25704

You can set your tics yourself by defining a filtering function for the first occurrence of "01" or "15" within each month which returns NaN otherwise.

Edit: test data changed to illustrate that some months have 30, some 31 and February 2020 has 29 days.

Edit2: code revised after comment.

Code:

### Fixed time step "xtics" on 01 and 15 of each month
reset session

myTimeFmt = "%Y-%m-%d %H:%M:%S"

# create some test data
set samples 181
set table $Data
    plot [0:1] '+' u (a=strftime(myTimeFmt, time(0)+$1*3600*24*180)):(a[9:10]) w table
unset table

set grid xtics, ytics

flag=0
myTic(s) = (s[9:10] eq "01") && (flag==0 || flag==15) ? \
    (flag=1, strftime("%m/%d",strptime(myTimeFmt,s))) : \
    (s[9:10] eq "15") && (flag==0 || flag==1) ? \
    (flag=15, strftime("%m/%d",strptime(myTimeFmt,s))) : NaN

set xdata time
set timefmt myTimeFmt
set xrange["2019-10-01":"2020-04-30"]

plot $Data u (timecolumn(1,myTimeFmt)):3: \
        xtic(myTic(strftime(myTimeFmt,timecolumn(1,myTimeFmt)))) w lp pt 7 notitle, \
### end of code

Result:

enter image description here

Upvotes: 1

Related Questions