full_range_seasonal.gnuplot: add a note about xtics placement
[experiments/gnuplot_seasonal_plot.git] / full_range_monthly.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 montly 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 "month temperature".
33 # Angles taken from the link below, except for the first one as pure cyan was
34 # too bright:
35 # http://www.linuxtopia.org/online_books/graphics_tools/gimp_user_manual/images/glossary/colorcircle-en.png
36 _saturation = .5
37 set palette maxcolors 12
38 set palette model HSV defined ( \
39   0  220/360. _saturation 1, \
40   3  120/360. _saturation 1, \
41   5   60/360. _saturation 1, \
42   7    0/360. _saturation 1, \
43   7  360/360. _saturation 1, \
44   9  300/360. _saturation 1, \
45   11 240/360. _saturation 1)
46
47 set cbrange [0:12]
48 set cbtics offset 0,+1 ( \
49   'Jan'  0, \
50   'Feb'  1, \
51   'Mar'  2, \
52   'Apr'  3, \
53   'May'  4, \
54   'Jun'  5, \
55   'Jul'  6, \
56   'Aug'  7, \
57   'Sep'  8, \
58   'Oct'  9, \
59   'Nov' 10, \
60   'Dec' 11, \
61   ''    12)
62
63 # Utility functions to deal with time
64
65 # When data is time, intervals are in seconds
66 one_year = 60 * 60 * 24 * 365.25
67 one_month = one_year / 12
68 three_months = one_year / 4
69
70 start_of_month(x) = strptime("%Y-%m-%d", "" . int(tm_year(x)) . "-". int(tm_mon(x) + 1) . "-01")
71
72 # strptime() seems to adjust the year automatically when passing a month > 12
73 start_of_next_month(x) = strptime("%Y-%m-%d", "" . int(tm_year(x)) . "-" . (int(tm_mon(x)) + 2) . "-01")
74
75 # Calculate the difference between two dates.
76 months_between_dates(a, b) = int((tm_year(b) - tm_year(a)) * 12 + tm_mon(b) - tm_mon(a))
77
78 # Dummy plot to gather some stats from the dataset
79 set terminal unknown
80 plot 'data.dat' using 1:2
81
82 xmin = start_of_month(GPVAL_DATA_X_MIN)
83 xmax = start_of_next_month(GPVAL_DATA_X_MAX)
84 ymin = GPVAL_Y_MIN
85 ymax = GPVAL_Y_MAX
86
87 set xrange [xmin:xmax]
88 set yrange [ymin:ymax]
89
90 # Calculate the x sampling rate for the '+' plot below.
91 # Have one sample per month, the +1 is to include the month of xmax.
92 x_samples = months_between_dates(xmin, xmax) + 1
93 set samples x_samples, 100
94
95 set xtics three_months
96
97 set terminal qt 0
98 #set terminal png notransparent nocrop truecolor rounded enhanced font "arial,8" fontscale 1.0
99 #set output 'full_range_monthly.png'
100
101 # The plot is done in three steps:
102 #
103 # 1. Plot an undefined function to force gnuplot to use the specified xrange
104 #    and yrange without rescaling them, this makes sure that changing the
105 #    sampling interval later does not affect the displayed xrange.
106 #
107 # 2. Use the boxxyerrorbars style to draw the background choosing the color
108 #    according to the month.
109 #    
110 #    When doing this, shift samples to make them fall in the middle of months.
111 #
112 #    This is needed to avoid "holes" between the boxes because samples are
113 #    evenly spaced while months lengths are not, so it may happen that
114 #    a sample crosses the month boundary even when it's not supposed to,
115 #    resulting in two different samples to draw the same box because of
116 #    start_of_month() and start_of_next_month().
117 #
118 #    Having some "buffering days"  can compensate  for the varying months
119 #    lengths.
120 #
121 # 3. Finally plot the actual data
122 plot \
123   NaN notitle, \
124   [xmin - one_month/2 : xmax - one_month/2] '+' using ($1):(0):(start_of_month($1)):(start_of_next_month($1)):(ymin):(ymax):(tm_mon($1)) with boxxy fc palette fs solid notitle, \
125   'data.dat' using 1:2 w l lc rgb "black" title "data"