My question is is there a way to make a horizontal histogram in gnuplot? The following is a made up dataset for a histogram plot:
sample M N O P Q R S T Total
A 39.17 8.33 11.07 8.57 22.20 22.93 1.47 1.13 114.87
B 61.68 15.53 16.23 15.55 35.68 36.35 2.32 2.23 185.58
C 30.20 7.35 7.55 7.48 16.90 17.50 1.18 1.10 89.25
D 12.57 5.18 4.98 5.77 9.88 8.18 0.67 0.83 48.07
E 36.80 9.00 11.07 9.43 21.43 22.63 0.93 1.03 112.33
F 62.05 15.57 15.95 15.70 35.68 36.63 2.27 2.47 186.32
G 30.23 7.33 7.55 7.48 17.10 17.68 1.10 1.25 89.70
H 12.97 4.87 5.22 5.18 10.37 7.90 0.98 0.70 48.18
I 17.67 12.80 9.40 5.67 32.47 18.53 1.13 0.63 98.30
J 72.30 13.70 17.43 15.43 30.62 38.43 2.25 2.43 192.60
K 25.70 7.58 12.45 4.60 19.30 18.88 1.10 0.80 90.40
L 16.15 3.80 3.53 10.43 7.27 8.80 0.88 1.10 51.97
I adapted the code from here to plot a histogram:
set terminal png font "Times-Roman,9"
set output 'sample.png'
set border 3 front linetype -1 linewidth 1.000
set boxwidth 0.85 absolute
set style fill solid 1.00 border lt -1
set grid nopolar
set grid noxtics nomxtics ytics nomytics noztics nomztics \
nox2tics nomx2tics noy2tics nomy2tics nocbtics nomcbtics
set grid layerdefault linetype 0 linewidth 1.000, linetype 0 linewidth 1.000
set key outside right top vertical Left reverse noenhanced autotitles columnhead nobox
set key invert samplen 4 spacing 1 width 0 height 0
set style histogram rowstacked title offset character 0, 0, 0
set datafile missing '-'
set style data histograms
#set style histogram gap 1
set xtics border in scale 0,0 nomirror rotate by -45 offset character 0, 0, 0 autojustify
set xtics norangelimit font ",8"
set xtics ()
#set noytics
set ylabel "% of total"
set yrange [ 0 : 100 ] #noreverse nowriteback
plot 'sample.txt' using (100.*$2/$10):xtic(1) t column(2), for [i=3:9] '' using (100.*column(i)/column(10)) title column(i)
This gives me:
Is there a way in gnuplot to make it go sideways?
This is a pretty old question, but still kind of unanswered.
As @Christoph suggested, the plotting style boxxyerror
is the way to go for a workaround.
However, so far I haven't seen example code, e.g. on the gnuplot demo page.
And as far as I know, gnuplot 5.2.8 and probably 5.4 still don't have a plotting style horizontal stacked histogram (please correct me if I'm wrong).
Actually, the column "Total" is not necessary, because gnuplot can do the total sum automatically. I guess the function sum
is available since 4.6.0. With the "Total" column, it might work with some adaption even with earlier versions (however, no stats
at that times).
Update:
boxxyerror
from 6 column to shorter 4 column syntax. Check help boxxyerror
.STATS_columns
for this.
Data: SO19800832.dat
sample M N O P Q R S T Total
A 39.17 8.33 11.07 8.57 22.20 22.93 1.47 1.13 114.87
B 61.68 15.53 16.23 15.55 35.68 36.35 2.32 2.23 185.58
C 30.20 7.35 7.55 7.48 16.90 17.50 1.18 1.10 89.25
D 12.57 5.18 4.98 5.77 9.88 8.18 0.67 0.83 48.07
E 36.80 9.00 11.07 9.43 21.43 22.63 0.93 1.03 112.33
F 62.05 15.57 15.95 15.70 35.68 36.63 2.27 2.47 186.32
G 30.23 7.33 7.55 7.48 17.10 17.68 1.10 1.25 89.70
H 12.97 4.87 5.22 5.18 10.37 7.90 0.98 0.70 48.18
I 17.67 12.80 9.40 5.67 32.47 18.53 1.13 0.63 98.30
J 72.30 13.70 17.43 15.43 30.62 38.43 2.25 2.43 192.60
K 25.70 7.58 12.45 4.60 19.30 18.88 1.10 0.80 90.40
L 16.15 3.80 3.53 10.43 7.27 8.80 0.88 1.10 51.97
Script: (works with gnuplot>=4.6.0, March 2012)
### horizontal stacked histogram
reset
FILE = "SO19800832.dat"
set xlabel "% of total"
set xrange [0:103]
set yrange [:] reverse
set offsets 0,0,0.5,0.5
set style fill solid 1.0
set key out
# automatic extraction of number of columns
set datafile separator "@" # some character which does not appear in the file
stats FILE u (ColCount=words(strcol(1))-2) nooutput
set datafile separator whitespace
myBoxwidth = 0.8
plot for [col=2:ColCount+1] FILE u (total=sum [i=2:ColCount+1] column(i), \
x0=column(col),((sum [i=2:col-1] column(i))+x0/2.)/total*100):0: \
(x0/2./total*100):(myBoxwidth/2.):ytic(1) w boxxy ti columnhead(col)
### end of script
Result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With