gnuplot

Gnuplot: How to draw an opposing bar plot or pyramid bar diagram


Is it possible to use two positive y-axes with gnuplot? This is just a simple example for the question.

plot.gp:

reset
set style fill solid 1
set boxwidth 0.8 relative
plot 'data1.dat' index 1 using 1:2     with boxes title 'A' ,\
              '' index 2 using 1:(-$2) with boxes title 'B'

Instead of using 1:(-$2) I would like to use 1:2 in the last line in plot.gp.

data1.dat:

0.12   0.024
0.15   0.132
0.18   0.241
0.22   0.136


0.12   0.039
0.15   0.219
0.18   0.197
0.22   0.155

From:

enter image description here

To:

enter image description here


Solution

  • Same answer as the one from theoz except that the y2 labels are shifted to the left side of the plot and the set link y2 command is used to generalize the axis inversion.

    You can tweak the offset graph -1.03 to superimpose the "0" labels, then delete the duplicate "0" label by changing the y2 tic range to set y2tics 0.1,0.1.

    $Data <<EOD
    0.12   0.024
    0.15   0.132
    0.18   0.241
    0.22   0.136
    
    
    0.12   0.039
    0.15   0.219
    0.18   0.197
    0.22   0.155
    EOD
    
    set style fill transparent solid 0.5
    set boxwidth 0.8 relative
    set xzeroaxis ls -1
    
    set yrange[-0.3:0.3]
    set ytics 0,0.1
    set mytics 2
    
    # Set y2 axis to exact mirror of y1
    # Shift tic labels to the left and use right-justified text
    set link y2 via -y inv -y
    set y2tics 0,0.1
    set y2tics offset graph -1.03 right
    
    plot $Data u 1:2 index 0 axis x1y1 w boxes title 'A' ,\
            '' u 1:2 index 1 axis x1y2 w boxes title 'B'
    

    enter image description here