Question

I am new to gnuplot. I want to generate graph from data points with three components and standard deviation. My data looks like this:

TYPE1   15  20  65  5
TYPE2   20  20  60  4
TYPE3   10  30  60  6
TYPE4   30  30  40  5

I want to plot a rowstacked bar for each TYPE with the 3 components stacked and an errobar at the top. I wrote the following script to do this:

set terminal png
set output "sample.png"
set boxwidth 0.75 relative
set style fill   pattern 0 border
set style histogram rowstacked
set style data histograms
set xtics 1000 nomirror
set ytics 100 nomirror
set noytics
set mxtics 2
set mytics 2
set ytics 100
set yrange [0:150]
set ylabel "Y"
set xlabel "X"
set title "Sample graph"
plot 'data.dat' using (100*column(2)/(column(2)+column(3)+column(4))) t "A" , '' using (100*column(3)/(column(2)+column(3)+column(4))) t "B" , '' using (100*column(4)/(column(2)+column(3)+column(4))):xtic(1) t "C" 

This produced a graph which looks like this: Click Here.

But I am not able to get the errorbar on the top of each bar with deviation values in column 5. I tried different ways using rowstacked and errorbar styled bar graphs but had no luck.

Was it helpful?

Solution

For this you must know, that with the histogram style the boxes are placed at the x-positions 0, 1, etc. i.e. at the row number.

So for the errorbars you must use column(0) as x-coordinate:

set terminal pngcairo
set output "sample.png"

set boxwidth 0.75 relative
set style fill pattern 0 border
set style histogram rowstacked
set style data histograms

set yrange [0:150]

set macros
scale = '100/(column(2)+column(3)+column(4))'

set bars 2.0
plot 'data.dat' using ($2 * @scale):xtic(1) t "A" , \
     '' using ($3 * @scale) t "B" , \
     '' using ($4 * @scale) t "C",\
     '' using 0:(100):5 with errorbars notitle lw 2 lt -1

The result with 4.6.3 is:

enter image description here

For convenience I used a macro scale. The macros work as follows: You define a string, like scale = '...' in the script above. That can be used later in any expression as @scale (you must have set macros enabled). The content of the scale string is then replaced before the respective command is executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top