How to change the time format based on its data

Do you know how to change the time format based on its data? I would like to add Year information on the X-axis if the January date is like that.

2013/01/01, 02/01, 03/01, ..., 11/01, 12/01, 2014/01/01, 02/21,....

      

I know the set format command to change it.

set format x "%m/%d"

      

or

set format x "%Y/%m/%d"

      

But I don't know how to set it up.

Thank.

+3


source to share


1 answer


It's not that easy because, as you said, with gnuplot, you can only set one format without any convention.

You can solve this problem manually by specifying the year information manually with the help set xtics add

, as was done also in the very closely related question , gnuplot xaxis mixing times and times .

Here's a working script:

set xdata time
set timefmt '%Y-%m-%d %H:%M'
set format x '%m/%d'
set xrange ['2013-01-01 00:00':*]
set xtics '2013-01-01 00:00', 3*30*24*60*60

set xtics add ('2013/01/01' '2013-01-01 00:00', '2014/01/01' '2014-01-01 00:00')

plot 'data.dat' using 1:3 with linespoints

      

So, you first set the format of your input. Here I have explicitly chosen a different input format so that you can distinguish where you need the format.

Then you set the output format for the x-axis to match what you want for the intermediate months.

Next, I explicitly set the initial value for the xrange so that the range starts in January (because I only set the label every three months, set xtics ...

).



As a final step, you manually overwrite the January labels with your custom format. In this form, it can be a little cumbersome if you have several years or want to be flexible. This has not been a problem for several years. Labels that are outside the display range are ignored. Thus, you can use:

set for [y=2000:2014] xtics add (sprintf('%d/01/01', y) ''.y.'-01-01 00:00')

      

(Note that sprintf

cannot be used for the second line.)

With the following test data file data.dat

:

2013-01-01 00:00 12
2013-02-01 12:00 15
2013-04-01 00:00 1
2013-12-01 00:00 11
2014-01-01 14:00 14
2014-04-01 03:00 20

      

I get the result (using 4.6.5):

plot showing result

+3


source







All Articles