Active Hub
Active Hub

Reputation: 61

Gnuplot: Aligning bars of histogram in multiplot

I am trying to plot 3 histograms in a multiplot. Three dat files are:

Sta1.dat:

attn,   mu2 ,  mu3, mu4
20,    4.9, 17.1, 78.1 
25,    4.0, 22.0, 74.0 
30,    2.0, 17.0, 81.0 
35,   11.5, 21.7, 66.8 
40,    4.7, 18.0, 77.4 
45,    3.8,  8.9, 87.3 
50,    0.6, 17.3, 82.1 
55,    2.0,  3.4, 94.6 
60,    1.0,  1.3, 97.6 

Sta2.dat

attn,   mu2 ,  mu3, mu4
20,   5.3, 20.6, 74.2
25,   9.2, 27.2, 63.6
30,   9.5, 20.3, 70.2
35,   9.9, 22.1, 68.0
40,   5.3, 19.0, 75.7
45,   3.4,  9.6, 86.9
50,   2.3, 15.3, 82.4
55,   2.7, 10.6, 86.7
60,   1.7,  1.0, 97.3

Sta3.dat

attn,   mu2 ,  mu3, mu4
20,    6.8, 20.6, 72.6
25,    6.2, 29.5, 64.3
30,    5.3, 23.6, 71.1
35,    4.5, 15.6, 79.9
40,    5.5, 17.4, 77.1
45,    3.7, 10.5, 85.8
50,    9.1, 16.6, 74.3
55,    2.8,  3.8, 93.4
60,    1.0,  1.6, 97.4

In num.jpg, Station2 and Station3 bars of histogram are aligned because the legends displayed. In station1, I didnt specify the legend and because of that bars of histogram are not aligned.

enter image description here

Can someone help in getting the adjustments in the commands to align all the bars.

set terminal pngcairo
set output "num.png"
set style data histogram
set style histogram rowstacked
set style fill solid
set key outside
set boxwidth 0.5

set size 1,1
set origin 0,0

set datafile separator ","
set multiplot layout 4,1 

set ytics border font ",6" offset 0.7,0
set key title 'Signal Strength' font ",7"
set key font ",5"
set key bottom right
set size 1,0.25
set origin 0,0.50
set lmargin at screen 0.1
set tmargin at screen 0.48
set bmargin at screen 0.27
set rmargin at screen 0.85
set title "Station3" font ",6" offset 0,-0.7
set xtics border font ",6" offset 0,0.7
plot for [COL=2:4] 'sta3.dat' using COL:xtic(1) ti col
unset xtic

set size 1,0.25
set origin 0,0.75
set lmargin at screen 0.1
set tmargin at screen 0.73
set bmargin at screen 0.52
set rmargin at screen 0.85
set xzeroaxis
set title "Station2" font ",6" offset 0,-0.7
plot for [COL=2:4] 'sta2.dat' using COL:xtic(1) ti col
unset key

set size 1,0.25
set origin 0,0.95
set lmargin at screen 0.1
set tmargin at screen 0.96
set bmargin at screen 0.77
set rmargin at screen 0.85
set title "Station1" font ",6" offset 0,-0.7
plot for [COL=2:4] 'sta1.dat' using COL:xtic(1)
unset multiplot
exit

Upvotes: 1

Views: 338

Answers (1)

grsousajunior
grsousajunior

Reputation: 778

I've made a couple of changes in your code to make use of multiplot command options. I removed the individual margin, size, and origin adjustments and applied a global font size on terminal settings. The weird behavior (I don't know how) come from ti col on last plot command.

set terminal pngcairo font ",8"
set output "num.png"
set style data histogram
set style histogram rowstacked
set style fill solid
set key outside
set boxwidth 0.5

set datafile separator ","

set key bottom right Left  # Better alignment to text
set key title 'Signal Strength' offset -1.5,0
set xtics nomirror
set yrange [0:100] # The same yrange to all plots

# layout, direction, 
# margins and 
# spacing for plots (see 'help multiplot')
set multiplot \
    layout 3,1 upwards \
    margins 0.08,0.85,0.08,0.94 \
    spacing 0.1,0.07
# ----------------------------------------------------
set title "Station3" offset 0,-0.7
plot for [COL=2:4] 'sta3.dat' using COL:xtic(1) ti col
# ----------------------------------------------------
set xtics tc bgnd # To draw an "invisible" xtics
set title "Station2"
plot for [COL=2:4] 'sta2.dat' using COL:xtic(1) ti col
# ----------------------------------------------------
unset key
set title "Station1"
plot for [COL=2:4] 'sta1.dat' using COL:xtic(1) ti col
unset multiplot
exit 

The final result: Result

Upvotes: 1

Related Questions