1 #!/usr/bin/gnuplot -persist
5 # Version 4.6 patchlevel 6 last modified September 2014
6 # Build System: Linux x86_64
8 # Copyright (C) 1986-1993, 1998, 2004, 2007-2014
9 # Thomas Williams, Colin Kelley and many others
11 # gnuplot home: http://www.gnuplot.info
12 # faq, bugs, etc: type "help FAQ"
13 # immediate help: type "help" (plot window: hit 'h')
15 set title "Seasonal phenomenon, highlighted with a montly gradient in the background"
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
25 set timefmt "%Y-%m-%d"
27 set xtics out nomirror rotate by -45
29 set ylabel "y" norotate
30 set ytics out nomirror 1
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
35 # http://www.linuxtopia.org/online_books/graphics_tools/gimp_user_manual/images/glossary/colorcircle-en.png
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)
48 set cbtics offset 0,+1 ( \
63 # Utility functions to deal with time
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
70 start_of_month(x) = strptime("%Y-%m-%d", "" . int(tm_year(x)) . "-". int(tm_mon(x) + 1) . "-01")
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")
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))
78 # Dummy plot to gather some stats from the dataset
80 plot 'data.dat' using 1:2
82 xmin = start_of_month(GPVAL_DATA_X_MIN)
83 xmax = start_of_next_month(GPVAL_DATA_X_MAX)
87 set xrange [xmin:xmax]
88 set yrange [ymin:ymax]
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
95 set xtics three_months
98 #set terminal png notransparent nocrop truecolor rounded enhanced font "arial,8" fontscale 1.0
99 #set output 'full_range_monthly.png'
101 # The plot is done in three steps:
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.
107 # 2. Use the boxxyerrorbars style to draw the background choosing the color
108 # according to the month.
110 # When doing this, shift samples to make them fall in the middle of months.
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().
118 # Having some "buffering days" can compensate for the varying months
121 # 3. Finally plot the actual data
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"