Search code examples
gnuplothistogram

Redrawing Excel figures on gnuplot


I was working on excel and drew two histograms shown below, I have been told to redraw them using gnuplot on windows which is very new to me.
The original graph that I want to redraw is this.

                     Area 1             Area 2 
               Case 1    Case 2   Case 1  Case 2 
Parameter 1     36         66      31      72
Parameter 2     57         91      44      85
Parameter 3     62         90      50      85

enter image description here

My file is a text file and I wrote the above table as follows as I am not sure how to group the different columns together.

Area    Area1 Area1 Area2 Area2
Case  Case1 Case2   Case1   Case2
Parameter_1 36  66  31  72
Parameter_2 57  91  44  85
Parameter_3 62  90  50  85

I used the following commands and got a histogram that is grouped in the wrong way.

clear
reset
unset key
set style data histogram
set style fill solid border
set style histogram clustered
plot for [COL=2:5] 'date_mins.tsv' using COL:xticlabels(1) title columnheader

enter image description here

Kindly guide me on how to group columns together and also how to add the numbers on top of the bars. {The graph should be same as the one excel generated one.}


Solution

  • To be honest I'm regularly puzzled with histograms in gnuplot, apparently I'm not the only one. In gnuplot console, check help histograms. Although, there are a few histogram examples on the gnuplot homepage, but of course not all possible variations can be covered. Apparently, this plotting style is a bit confusing to understand. This would maybe explain that there are more than 800 questions on SO on histograms with gnuplot.

    I'm not sure if or how you can get your desired histogram efficiently, maybe there is an easy way. I would do it "manually" with the plotting style with boxes. Check the example below as a starting point. There are a few strange workarounds included, e.g. getting the titles into an array in an earlier plot for later use.

    Code:

    ### special histogram
    reset session
    
    $Data <<EOD
    Area           Area1  Area1   Area2  Area2
    Case           Case1  Case2   Case1  Case2
    "Parameter 1"  36     66      31     72
    "Parameter 2"  57     91      44     85
    "Parameter 3"  62     90      50     85
    EOD
    
    set style fill solid noborder
    set boxwidth 0.8
    set key noautotitle out center bottom horizontal reverse Left samplen 1 width 2
    
    A=2   # Areas
    C=2   # Cases
    P=3   # Parameters
    g=1   # gap
    
    PosX(a,c,p)   = ((a-1)*C*(P+g)) + (c-1)*(P+g) + p
    PosY(a,c)     = column((a-1)*C+c+1)
    PosXArea(a)   = (PosX(a,C,P)+PosX(a-1,C,P))*0.5
    PosXCase(a,c) = (PosX(a,c,P)+PosX(a,c-1,P))*0.5
    myColor(p)    = int(word("0x5b9bd5 0xed7d31 0xa5a5a5",int(p)))
    myValue(a,c)  = strcol((a-1)*C+c+1)
    
    set grid y
    set xlabel "\n\n\n"     # get empty space below the plot
    set format x ""         # no xtic labels 
    set yrange[0:]
    array Titles[P]         # array for titles
    
    plot for [a=1:A] for [c=1:C] $Data u (PosX(a,c,$0)):(PosY(a,c)):(myColor($0+1)) skip 2 w boxes lc rgb var , \
         for [a=1:A] for [c=1:C]    '' u (PosX(a,c,$0)):(PosY(a,c)):(Titles[int($0+1)]=strcol(1), myValue(a,c)) skip 2 w labels offset 0,0.7, \
         for [a=1:A] for [c=1:C]    '' u (PosXCase(a,c)):(0):(myValue(a,c)) every ::1::1 w labels offset 0,-1, \
         for [a=1:A]                '' u (PosXArea(a)):(0):('\n\n'.myValue(a,1)) every ::0::0 w labels offset 0,-1, \
         for [p=1:P] keyentry w boxes lc rgb myColor(p) ti Titles[p]
    ### end of code
    

    Result:

    enter image description here