full_range_seasonal.gnuplot: add a note about xtics placement
[experiments/gnuplot_seasonal_plot.git] / modular_cartesian.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 wrapping around each year"
16
17 set border 3
18 set grid xtics back
19 set nokey
20 set rmargin 8
21
22 set xdata time
23 set timefmt "%Y-%m-%d"
24 set format x "%s"
25 set xtics out nomirror rotate by -45
26 set ytics out nomirror 1
27
28 # Dummy plot to gather some stats from the dataset
29 set terminal unknown
30 plot 'data.dat' using 1:2
31
32 xmin = GPVAL_DATA_X_MIN
33 xmax = GPVAL_DATA_X_MAX
34
35 # Plot over 12 months
36 set xrange[0:12]
37 set xtics ( \
38   'Jan'  0, \
39   'Feb'  1, \
40   'Mar'  2, \
41   'Apr'  3, \
42   'May'  4, \
43   'Jun'  5, \
44   'Jul'  6, \
45   'Aug'  7, \
46   'Sep'  8, \
47   'Oct'  9, \
48   'Nov' 10, \
49   'Dec' 11, \
50   ''    12)
51
52 year_min = tm_year(xmin)
53 year_max = tm_year(xmax)
54
55 set cbrange [year_min:year_max + 1]
56
57 # Generate colorbox labels programmatically
58 cblabels = 'set cbtics ( '
59 do for [i=year_min:year_max] {
60    cblabels = cblabels . '"' . i .'" ' . i .','
61 }
62 cblabels = cblabels . '"" ' . int(year_max + 1) .','
63 cblabels = cblabels . ' )'
64 eval(cblabels)
65
66 # The value 12 works well with the default colorbox height
67 cbtics_offset = 12 / (year_max - year_min)
68 set cbtics offset 0,+cbtics_offset
69
70 set palette rgbformulae 31,-11,32
71 set palette maxcolors (year_max - year_min + 1)
72
73 # Scale the data so that one year covers the xrange of 12 months,
74 # this allows samples in the same time of the year to be at the abscissa.
75 one_year = 60 * 60 * 24 * 365.25
76 scale_date_to_month(x) = (int(x) % int(one_year)) / one_year * 12
77
78 # With this hacky-ish wrap() function we loose some points when the data wraps
79 # around, but AFAIK this is unavoidable without preprocessing the data.
80 #
81 # Value wraps around if the year changes between 'new' and 'old'.
82 year_wrap(old, new) = tm_year(old) == tm_year(new) ? scale_date_to_month(new) : NaN
83
84 if (GPVAL_VERSION < 5.0) \
85   tcol(x) = timecolumn(x); \
86 else \
87   tcol(x) = timecolumn(x, "%Y-%m-%d")
88
89 set terminal qt 0
90 #set terminal png notransparent nocrop truecolor rounded enhanced font "arial,8" fontscale 1.0
91 #set output 'modular_cartesian.png'
92
93 saved = -1
94 plot \
95   'data.dat' using (prev=saved, saved=tcol(1), year_wrap(prev, saved)):2:(tm_year(tcol(1))) with lines lc palette lw 1.5