Search code examples
colorsstatistics2dgnuplothistogram

How to color a cluster histogram in gluplot?


This is my gnuoplot script:

#
set title "prova"
set auto x
set grid y
set ylabel "% of total"
set style data histogram
set style histogram cluster gap 1
set style fill pattern 1 border lt -1
set boxwidth 1.0
set xtic rotate by -0 scale 0  
plot 'data.dat' using 2:xtic(1) ti col, '' u 3 ti col, '' u 4 ti col 
#

this is the file data.dat:

#
level   V(89.4%) ER     I
Liv1    -6.7    -4.6    -9.8
Liv2    -20.5   -14.6   -25.8
Liv3    40.0    45.1    36.5
Liv4    24.8    28.7    22.2
Liv5    8.1     7.0     5.7
#

and this is the result:

enter image description here

So:

  1. I want that the 'liv1' three columns are filled in red, the 'liv2' 3 columns are filled in orange, the 'liv3' 3 columns are filled in white, the 'liv4' 3 columns are filled in light green, and the 'liv5' 3 columns are filled in dark green

  2. I want that the 1st column, the 2nd colunmn and the 3th column of each of the five sub-histo has a black texture (pattern) that can be shown in the legend as in my actualy pic


Solution

  • Actually, right away I don't know how you could do it with style histogram, hence, I would do it with boxes where you have full flexibility.

    The first plot command is used for the boxes with variable color (check linecolor variable). The colors are taken from a string list (check help word and help colorspec). $0 is the pseudcolumn 0, i.e. the row number index (check help pseudocolumns). The second plot command is plotting nothing (NaN), but it is just for the xticlabels and a grey version of the fill patterns in the legend.

    Script:

    ### defined colors and fill patterns for a box chart
    reset session
    
    $Data <<EOD
    level   V(89.4%) ER     I
    Liv1    -6.7    -4.6    -9.8
    Liv2   -20.5   -14.6   -25.8
    Liv3    40.0    45.1    36.5
    Liv4    24.8    28.7    22.2
    Liv5     8.1     7.0     5.7
    EOD
    
    set title "prova"
    set grid y
    set ylabel "% of total"
    set style fill pattern 1 border lt -1
    set boxwidth 1.0 absolute
    set key noautotitle
    set xzeroaxis ls 1 lc "black"
    PosX(row,col) = 0
    
    #             red      orange   black    light-green dark-green"
    myColors   = "0xff0000 0xffa500 0x000000 0x90ee90    0x006400"
    myColor(i) = int(word(myColors,int(i+1)))
    
    stats $Data u 0 nooutput   # get number of columns
    N = STATS_columns
    
    plot for [col=2:N] $Data u ($0*N+col):col:(myColor($0)) skip 1 w boxes lc rgb var, \
         for [col=2:N]    '' u ($0*N+N/2.+1):(NaN):xtic(1) w boxes fill pattern col-1 lc "grey" ti columnheader(col)
    ### end of script
    

    Result: (patterns you get in wxt terminal)

    enter image description here