Search code examples
bar-chartgnuplot

Grouped bar plot with multiple labels in x-axis


I am trying to replicate something close to the following graph in gnuplot as I need to use it on a latex paper. I have tried a lot but I cannot make the two-line labels at the bottom. Could you please guide me? Also, how is it possible to have the % character as part of a label in the x-axis? Latex complains about it.

enter image description here

The data are in the following format (example). Each different color corresponds to different method. Blue is method 1 (m1), orange is method 2 (m2), and brown is method 3 (m3)

#%      system1-m1  system1-m2  system1-m3  system2-m1  ...
0.5%    16          8           15          6
1%      15          17          16          8
2%      12          10          20          15

Thanks

Edit

My code so far is as follows:

set rmargin 0
set key outside tmargin center top horizontal width 3
set border
set grid
set boxwidth 0.8
set style fill solid 1.00 
set xtics nomirror rotate by 0
set format y '%1.f'
set yrange [0 to 22]
set ylabel 'Gain (\%)'
set ytics 0, 5

set style data histograms

set label 1 at -0.3, -4 '|---------System 1------------|'
set label 2 at 2.7, -4 '|---------System 2------------|'

plot "./data/metrics.dat" using 2:xtic(1) title 'Method 1' ,\
     "" using 3 title 'Method 2', \
     "" using 4 title 'Method 3',

And I have modified the .dat file as

0.5 16  8   15
1.0 15  17  16
2.0 12  10  20
0.5 13  6   4
1.0 11  13  13
2.0 14  12  14

because I cannot make it print the % character. The output graph is

enter image description here

As you can see it is not scalable. I have to put labels by hand (trial and error) and also the labels below the x-axis do not contain the % character.


Solution

  • We've been close: set format x '%.1f\%%'. The following works for me with cairolatex terminal (check help cairolatex).

    Code:

    ### percent sign for tic label in TeX
    reset session
    
    set term cairolatex
    set output 'SO70029830.tex'
    
    set title 'Some \TeX\  or \LaTeX\ title: $a^2 + b^2 = c^2$'
    set format x '%.1f\%%'
    
    plot x
    
    set output
    ### end of code
    

    Result: (screenshot)

    enter image description here

    Addition:

    Sorry, I forgot the second part of your question: the labels. Furthermore, in your graph you are using xtic(1) as tic labels, i.e. text format, so the command set format x '%.1f\%%' from my answer above will not help here. One possible solution would be to create and use your special TeX label like this:

    myTic(col) = sprintf('%.1f\%%',column(col))
    plot $Data using 2:xtic(myTic(1))
    

    For the labels, I would use arrows and labels. Each histogram is placed at integer numbers starting from 0. So, the arrows have to go from x-values -0.5 to 2.5 and from 2.5 to 5.5. The labels are placed at x-value 1 and 4. There is certainly room for improvements.

    Code:

    ### tic labels with % for TeX and lines/labels
    reset session
    set term cairolatex
    set output 'SO70029830.tex'
    
    $Data <<EOD
    0.5 16  8   15
    1.0 15  17  16
    2.0 12  10  20
    0.5 13  6   4
    1.0 11  13  13
    2.0 14  12  14
    EOD
    
    set rmargin 0
    set key outside center top horizontal width 3
    set border
    set grid
    set boxwidth 0.8
    set style fill solid 1.00 
    set xtics nomirror rotate by 0
    set format y '%1.f'
    set yrange [0 to 22]
    set ylabel 'Gain (\%)'
    set ytics 0, 5
    
    set style data histograms
    set bmargin 4
    
    set arrow 1 from -0.5, screen 0.05 to 2.5, screen 0.05 heads size 0.05,90
    set label 1 at 1, screen 0.05 'System 1' center offset 0,-0.7
    set arrow 2 from 2.5, screen 0.05 to 5.5, screen 0.05 heads size 0.05,90
    set label 2 at 4, screen 0.05 'System 2' center offset 0,-0.7
    
    myTic(col) = sprintf('%.1f\%%',column(col))
    
    plot $Data using 2:xtic(myTic(1)) title 'Method 1' ,\
         "" using 3 title 'Method 2', \
         "" using 4 title 'Method 3',
    set output
    ### enf of code
    

    Result: (screenshot from LaTeX document)

    enter image description here