Search code examples
gnuplot

gnuplot: how to plot color squares for each month's temperature?


I would like to plot the following figure (from Fundamentals of Data Visualization) using gnuplot:

color

I expect the data for each location is something like:

# month temperature
01 60.0
02 78.0
03 90.0
...
12 78.0

Here is what I tried. For simplicity, I have transposed the data into a matrix.

$data << EOD
1.50 1.57 1.85 2.15 1.87 1.05 1.70 1.65 1.97 1.71 1.53 1.15
4.44 4.71 4.74 3.50 3.43 4.98 4.29 4.55 3.93 3.34 3.74 4.88
8.55 9.59 5.65 0.13 9.33 4.70 8.94 7.74 4.49 6.26 0.96 1.20
EOD

unset border
unset ytics
set xlabel 'month'

set palette rgbformula -7,2,-7
set cbrange [0:10]
set cblabel "precipitation"
set xrange [-0.5:11.5]
set yrange [-0.5:2.5]
set xtics ("Jan" 0, "Feb" 1, "Mar" 2, "Apr" 3, "May" 4, \
"Jun" 5, "Jul" 6, "Aug" 7, "Sep" 8, "Oct" 9, "Nov" 10, "Dec" 11)
plot $data matrix with image

But the effect is far from satisfactory. For example, How to generate clear borders between squares?

enter image description here


Solution

  • The plotting style with image probably cannot have borders. So, I would use the versatile style with boxxyerror. Furthermore, instead of matrix format I would add a line header and loop through the columns (since there will be always 12 months) Check the following example as well as help boxxyerror, help size, help xticlabels and help strftime for further reading.

    If you have your data in separate files you would have to modify the script accordingly.

    Script:

    ### plotting style boxxyerror as replacement for "with image"
    reset session
    
    $Data << EOD
    # location  Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
    Atlantis    1.50  1.57  1.85  2.15  1.87  1.05  1.70  1.65  1.97  1.71  1.53  1.15
    Mordor      4.44  4.71  4.74  3.50  3.43  4.98  4.29  4.55  3.93  3.34  3.74  4.88
    Wonderland  8.55  9.59  5.65  0.13  9.33  4.70  8.94  7.74  4.49  6.26  0.96  1.20
    EOD
    
    unset border
    set xlabel 'month'
    set xrange [0.5:12.5]
    
    set yrange [:] reverse
    set ytics
    
    set palette rgbformula -7,2,-7
    set cbrange [0:10]
    set cblabel "precipitation"
    
    MonthName(n) = strftime("%b",24*3600*28*n)      # get the month name from 1..12
    set key noautotitle
    set style fill solid 1.0 border rgb "white"
    set size ratio -1       # make the boxes squares
    
    plot for [i=1:12] $Data u (i):0:(0.5):(0.5):i+1:  \
            xtic(MonthName(i)):ytic(1) w boxxy fc palette lw 2
    ### end of code
    
    

    Result:

    enter image description here