Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

gnuplot - How to change the time format based on its data

Do you know how to change the time format based on its data? I'd like to add Year info on the X axis if the date is January like this.

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

I know 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 customize it.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That is not so straightforward, because, as you said, with gnuplot you can set only a single format without any conditionals.

You can solve this by setting the year info manually with set xtics add, like it was also done in the very similar question mixing date and time on gnuplot xaxis.

Here is 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 data. Here I've explicitly chosen a different input format, so that you can distinguish where you need which format.

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

Next I explicitly set a start value for the xrange, so that the range starts with January (because I set a label only every three months, with set xtics ...).

As a last step you manually overwrite the January labels with your custom format. In this form it may be a bit cumbersome if you have several years or if you want to be flexible. It is not a problem to iterate over a range of years. The labels which are outside of the displayed range are ignored. So you could 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 string.)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...