f92cf0792a15ad10f9f91b6888aeef8cea76ceee
[experiments/gnuplot_seasonal_plot.git] / full_range_seasonal.gnuplot
1 #!/usr/bin/gnuplot -persist
2 #
3 #    
4 #       G N U P L O T
5 #       Version 4.6 patchlevel 6    last modified September 2014
6 #       Build System: Linux x86_64
7 #    
8 #       Copyright (C) 1986-1993, 1998, 2004, 2007-2014
9 #       Thomas Williams, Colin Kelley and many others
10 #    
11 #       gnuplot home:     http://www.gnuplot.info
12 #       faq, bugs, etc:   type "help FAQ"
13 #       immediate help:   type "help"  (plot window: hit 'h')
14
15 set title "Seasonal phenomenon, highlighted with a seasonal gradient in the background"
16
17 set border 3
18 set grid xtics back
19 set key noreverse enhanced autotitles box linetype -1 linewidth 1.000
20 set key vert out bottom center
21 set key width 2 height 1
22 set rmargin 8
23
24 set xdata time
25 set timefmt "%Y-%m-%d"
26 set format x "%b %y"
27 set xtics out nomirror rotate by -45
28
29 set ylabel "y" norotate
30 set ytics out nomirror 1
31
32 # Define a color palette representing the "season temperature"
33 _saturation = .2
34 set palette maxcolors 4
35 set palette model HSV defined ( \
36   0  200/360. _saturation 1, \
37   1  100/360. _saturation 1, \
38   2   80/360. _saturation 1, \
39   3   20/360. _saturation 1, \
40   3    0/360. _saturation 1, \
41   3  360/360. _saturation 1, \
42   4  290/360. _saturation 1)
43
44 set cbrange [0:4]
45
46 # Seasons order as in the northern hemiphere
47 set cbtics offset 0,+3 ( \
48   'Winter' 0, \
49   'Spring' 1, \
50   'Summer' 2, \
51   'Autumn' 3, \
52   '' 4)
53
54 # Utility functions to deal with seasons
55
56 # When data is time, intervals are in seconds
57 one_year = 60 * 60 * 24 * 365.25
58 three_months = one_year / 4
59
60 # Argument is a month between 0 and 11
61 season(x) = ((int(x) + 1) % 12) / 3
62
63 # Argument is a season between 0 and 3.
64 #
65 # NOTE: when rotating modulo 12, increasing by 11 is the same as subtracting
66 # by 1, the increment is used here because gnuplot does not handle the modulo
67 # with negative numbers very well
68 first_month_of_season(x) = ((int(x) * 3) + 11) % 12
69
70 # Winter starts the previous year if the months are January (0) of February (1)
71 start_of_season(x) = strptime("%Y-%m-%d", "" . (int(tm_year(x)) - (int(tm_mon(x)) < 2)) . "-" . int(first_month_of_season(season(tm_mon(x))) + 1) . "-21")
72
73 # Calculate the difference between two dates.
74 months_between_dates(a, b) = int((tm_year(b) - tm_year(a)) * 12 + tm_mon(b) - tm_mon(a))
75
76 # One season is 3 months.
77 seasons_between_dates(a, b) = int(months_between_dates(a, b) / 3)
78
79 # Dummy plot to gather some stats from the dataset
80 set terminal unknown
81 plot 'data.dat' using 1:2
82
83 xmin = start_of_season(GPVAL_DATA_X_MIN)
84 xmax = start_of_season(GPVAL_DATA_X_MAX + three_months)
85 ymin = GPVAL_Y_MIN
86 ymax = GPVAL_Y_MAX
87
88 set xrange [xmin:xmax]
89 set yrange [ymin:ymax]
90
91 # Calculate the x sampling rate for the '+' plot below.
92 # Have one sample per season, the +1 is to include the season of xmax.
93 # This way we draw only the strictly needed boxxys for the background.
94 x_samples = seasons_between_dates(xmin, xmax) + 1
95 set samples x_samples, 100
96
97 set xtics xmin,three_months
98
99 set terminal qt 0
100 #set terminal png notransparent nocrop truecolor rounded enhanced font "arial,8" fontscale 1.0
101 #set output 'full_range_seasonal.png'
102
103 # Use the boxxyerrorbars style to draw the background choosing the color
104 # according to the season.
105 plot \
106   '+' using (0):(0):(start_of_season($1)):(start_of_season($1 + three_months)):(ymin):(ymax):(season(tm_mon($1))) with boxxy fc palette fs solid notitle, \
107   'data.dat' using 1:2 w l lc rgb "black" title "data"