Search code examples
gnuplot

gnuplot: bar charts visualization


My gnuplot script plot a bar graph for 2D data either in monochrome or color format:

set term pngcairo size 800,600
set termoption noenhanced
set tics font "Helvetica,10"

#set xtics noenhanced
set ylabel "Fraction, %"
set xlabel "H-bond donor/aceptor, residue"
set yrange [0:1]
set ytics 0.1
set grid y
set key off
set boxwidth 0.9
set style fill solid 0.5

# TWO OPTIONS FOR BAR VISUALISATIONS!! NB: ADD HERE TRIGGER FROM COLOR_DATA TRIGGER
# 1 - use it with non-colored bars"
#plot "\$data" using 0:2:xtic(1) with boxes, "" using 0:2:2 with labels offset 0,1
# 2 - or use it with colored bars:
plot \$data using 0:2:3:xtic(1) with boxes lc rgb var, \
        '' using 0:2:2 with labels offset 0,1

The problem when I have just one bar, one the graph it occupiers all the graph on X: enter image description here

Would it be possible to set some minimum dimension for the bars to make the dimensions of a single bar similar for a situations with two bars, for instance: enter image description here


Solution

  • My understanding is the following: if you have only one box:

    1. maybe gnuplot tries to autoscale and the automatic boxwidth is small relative to the autorange (hence just a thin line).
    2. if you set a certain boxwidth, autoscale with scale to the given boxwidth (hence the graph filled with the box).
    3. you could set a fixed xrange, but then you are loosing the benefits of autoscale. Instead you can use set offets (check help offsets).
    4. if you have more than 1 box autoscale will work.

    Script:

    ### boxwidth with boxes style
    reset session
    
    $Data1 <<EOD
     1   Abc
    EOD
    
    $Data2 <<EOD
     1 Abc
     2 Xyz
    EOD
    
    set style fill solid 0.3
    set key out
    set rmargin screen 0.7
    set yrange[0:]
    set ytics 0.5
    
    set multiplot layout 4,1
    
        plot $Data1 u 0:1:xtic(2) w boxes ti "No special settings"
    
        set boxwidth 0.9
        plot $Data1 u 0:1:xtic(2) w boxes ti "set boxwidth"
    
        set offsets 1,1,0,0
        plot $Data1 u 0:1:xtic(2) w boxes ti "set offsets"
    
        set offsets 0,0,0,0
        plot $Data2 u 0:1:xtic(2) w boxes ti "more than 1 box"
    
    unset multiplot
    ### end of script
    

    Result:

    enter image description here