Search code examples
gnuplothistogram

Setting uniform y-range for broken axis histogram in gnuplot


I am trying to plot a histogram (using gnuplot (5.4 version)) with broken axis but I am not able to make the y-range uniform in both the plots. For plot where y-range is from [0:-8], the interval is of -1. But for plot where y-range is from [-16:-20], the interval is of -0.5 instead of -1. So is there any way to make the interval uniform in both plots? Any suggestions in that regard will be highly helpful.

Thanks

This is my current output using the below gnu script

set encoding iso_8859_1

set terminal postscript eps enhanced size 5.7, 4.3 color solid linewidth 2 font 'Times-Roman, 22'
set output 'test4.eps'

red = "#FF0000"; green = "#00FF00"; blue = "#0000FF"; skyblue = "#87CEEB";

reset
unset key
bm = 0.05
lm = 0.12
rm = 0.95
tm = 0.5
gap = 0.03
size =0.68
kk = 0.5 # relative height of bottom plot
y1 = 0.0; y2 = -8.0; y3 = -16; y4 = -20
ntics = 5

set style histogram cluster gap 1
set style data histogram
#set key autotitle columnheader
set style fill solid 1.0 border -1

set multiplot
set border 2+4+8
unset xtics
set x2tics nomirror
set x2tics rotate 
#set grid ytics
set ytics nomirror 
set xlabel 'Metals' font 'Times-Roman, 22' offset 0, 16.5;
set ylabel 'd-band' font 'Times-Roman, 22' offset -1.5, -6; 
#set lmargin at screen lm
#set rmargin at screen rm
#set bmargin at screen bm
#set tmargin at screen bm + size * kk
set lmargin at screen lm
set rmargin at screen rm
set bmargin at screen bm + size * kk + gap
#set tmargin at screen bm + size * (abs(y2-y1) / (abs(y2-y1) + abs(y4-y3))) + gap
set tmargin at screen bm + size + gap

set yrange [y2:y1]
plot "d-band.dat" using 2:x2tic(1) title "Neutral"  linecolor rgb '#616161',   \
     "d-band.dat" using 3 title "Positive" linecolor rgb '#EF6C00',   \
     "d-band.dat" using 4 title "Negative" linecolor rgb '#039BE5',  

unset x2tics
unset xtics
#unset ytics
unset xlabel
unset ylabel
set key at 11, -18.3
set border 2+1+8
#set ytics nomirror
#set bmargin at screen bm + size * kk + gap
#set tmargin at screen bm + size + gap

#set lmargin at screen lm
#set rmargin at screen rm
set bmargin at screen bm
set tmargin at screen bm + size * kk
#set tmargin at screen bm + size * (abs(y2-y1) / (abs(y2-y2) + abs(y4-y3)))

set yrange [y4:y3]

plot "d-band.dat" using 2:x2tic(1) title "Neutral" linecolor rgb '#616161',   \
     "d-band.dat" using 3 title "Positive" linecolor rgb '#EF6C00',   \
     "d-band.dat" using 4 title "Negative" linecolor rgb '#039BE5',  

unset multiplot


Solution

  • One possible solution without multiplot... you could do something with nonlinear axis, check help nonlinear. Since I don't have your data, I created some simpler data and plotted it just with boxes.

    • you add a nonlinear mapping to your y-axis.
    • for separating the y-axis you place a white rectangle on top of everything. This is not a nice workaround, but works for pixel graphics (e.g. like .PNG). You need to test, whether this will also work properly for .EPS, .PDF or other vector formats.

    This can probably be fine tuned, e.g. to customize the gap and let the -16 appear on the y-axis, etc.

    Script: (works for gnuplot>=5.2.0)

    ### broken axis with equal ytic spacing using nonlinear axis
    reset session
    
    $Data <<EOD
    A   -19.5
    B    -4.0
    C    -1.0
    D   -18.3
    E    -7.0
    F    -5.0
    EOD
    
    y1 =  0.0
    y2 = -8.0
    y3 = -16
    y4 = -20
    
    set yrange[-y4:y1]
    f(y) = y<y3 ? y-y3+y2 : y
    g(y) = y<y2 ? y+y3-y2 : y
    set nonlinear y via f(y) inverse g(y)
    
    set x2tics out mirror
    unset xtics
    set grid y
    set key noautotitle
    set boxwidth 0.7
    set style fill solid 0.3
    set obj 1 rect from graph -0.01, first y2-0.1 to screen 1.0, first y2-0.5 fc "white" front fs noborder
    
    plot $Data u 0:2:0:x2tic(1) w boxes lc var
    ### end of script
    

    Result:

    enter image description here