如何根据其数据更改时间格式 [英] How to change the time format based on its data

查看:83
本文介绍了如何根据其数据更改时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道如何根据其数据更改时间格式吗? 如果日期是这样的一月,我想在X轴上添加Year信息.

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"

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

但是我不知道如何自定义它.

But I don't know how to customize it.

谢谢.

推荐答案

这并不是那么简单,因为正如您所说的,使用gnuplot只能设置一种格式而没有任何条件.

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

您可以通过使用set xtics add手动设置年份信息来解决此问题,就像在非常相似的问题

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.

这是一个有效的脚本:

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.

然后,您设置x轴的输出格式,该格式对应于中间月份所需的格式.

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

接下来,我明确为xrange设置一个起始值,以便该范围从1月开始(因为我每三个月才用set xtics ...设置一个标签).

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')

(请注意,sprintf不能用于第二个字符串.)

(Note, that sprintf cannot be used for the second string.)

带有以下测试数据文件data.dat:

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

我得到结果(使用4.6.5):

I get the result (using 4.6.5):

这篇关于如何根据其数据更改时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆