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 seasonal 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 "season temperature"
 
  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)
 
  46 # Seasons order as in the northern hemiphere
 
  47 set cbtics offset 0,+3 ( \
 
  54 # Utility functions to deal with seasons
 
  56 # When data is time, intervals are in seconds
 
  57 one_year = 60 * 60 * 24 * 365.25
 
  58 three_months = one_year / 4
 
  60 # Argument is a month between 0 and 11
 
  61 season(x) = ((int(x) + 1) % 12) / 3
 
  63 # Argument is a season between 0 and 3.
 
  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
 
  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")
 
  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))
 
  76 # One season is 3 months.
 
  77 seasons_between_dates(a, b) = int(months_between_dates(a, b) / 3)
 
  79 # Dummy plot to gather some stats from the dataset
 
  81 plot 'data.dat' using 1:2
 
  83 xmin = start_of_season(GPVAL_DATA_X_MIN)
 
  84 xmax = start_of_season(GPVAL_DATA_X_MAX + three_months)
 
  88 set xrange [xmin:xmax]
 
  89 set yrange [ymin:ymax]
 
  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
 
  97 # NOTE that the ticks will be placed at the beginning of months regardless of
 
  98 # the day of the month of xmin. See:
 
  99 # https://stackoverflow.com/questions/42212039/gnuplot-xtic-labels-mispositioning-with-dates
 
 100 set xtics xmin,three_months
 
 103 #set terminal png notransparent nocrop truecolor rounded enhanced font "arial,8" fontscale 1.0
 
 104 #set output 'full_range_seasonal.png'
 
 106 # Use the boxxyerrorbars style to draw the background choosing the color
 
 107 # according to the season.
 
 109   '+' 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, \
 
 110   'data.dat' using 1:2 w l lc rgb "black" title "data"