Gnuplot X-Axis with Month Names

冷暖自知 提交于 2019-12-24 05:59:39

问题


I have a data file in which a floating-point time column increases from zero to some large value. Each time-point is associated with a data point.

The time points also happen to correspond to months, such that 0=Jan, 1=Feb, ..., 11=Dec, 12=Jan, 13=Feb, ...

I'd like to plot this data such that I see the month names on the X-axis instead of 0-*.

Is there a way to do this?

If it's helpful, I can also pre-generate the month name in the data.

Sample data, with the possibly-helpful month name included, follows.

#Time Month Data
0.01   Jan 0
0.055  Jan 0
0.2575 Jan 0
1.1687 Feb 0
1.9889 Feb 0
3.1037 Apr 0.00065915
3.2427 Apr 0.0035198
4.0656 May 0.03785
4.2134 May 0.041977
5.0032 Jun 0.049293
6.024  Jul 0.080957
7.0317 Aug 0.34284
7.9847 Aug 0.80902
8.0081 Sep 0.8149
8.9749 Sep 0.94009
9.0316 Oct 0.94389
9.9893 Oct 0.97967
10.058 Nov 0.97967
10.295 Nov 0.97967
11.125 Dec 0.97967
13.151 Feb 0.97967
13.696 Feb 0.97967
14.124 Mar 0.9797
15.119 Apr 0.97733

回答1:


My first thought was to try something sneaky with timefmt, but unfortunately, that doesn't work since all the months aren't the same length, so you don't actually have a linear scale here. I think the best you can really do is to specify the tics manually:

set xtics ("Jan" 0.5, "Feb" 1.5, "Mar" 2.5, ...)

This adjusts the tics so that they are centered on the range. If you don't want that, then use integer values instead:

set xtics ("Jan" 0, "Feb" 1, "Mar" 2, ...)

Of course, you can make this a little more flexible using a for do loop which was introduced in gnuplot 4.6:

MONTHS="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
set xtics ( "Jan" 0.5 )  #first one can't have `add`.
do for [i=1:15] {
    which_month = word(MONTHS,i%12+1)
    pos = i+0.5
    set xtics add ( which_month pos )
}
plot 'test.dat' u 1:3

You could even get the upper bound for your looping via the stats command looking at STATS_max -- (you'd probably use N = int(STATS_max+1) or something similar):

stats 'test.dat' u 1 nooutput #Runs stats on the file, generates STATS_max

There's also a shorthand notation which works with gnuplot 4.4 too:

MONTHS="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
set xtics ( "Jan" 0.5 )
set for [i=1:15] xtics add ( word(MONTHS,i%12+1) i+0.5 )
plot 'test.dat' u 1:3


来源:https://stackoverflow.com/questions/14846579/gnuplot-x-axis-with-month-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!