Add Error Bars to GNUPlot Barplot - gnuplot

I have a very simple chart like this
and I would like to add vertical error bars above and below the top of each bar.
my GNUPlot input is
set boxwidth 0.5
set style fill solid
set title 'title'
set key off
set ylabel 'ylabel'
set xtics font 'Arial, 24'
set ytics font 'Arial, 18'
set title font 'Arial, 24'
set ylabel font 'Arial, 16'
set terminal pngcairo size 1000,1000# gnuplot recommends setting terminal before output
set output 'pathfinder.png'
plot 'pathfinder.txt' every ::0::0 using 1:3:xtic(2) with boxes lc rgb 'red', \
'pathfinder.txt' every ::1::1 using 1:3:xtic(2) with boxes lc rgb 'green', \
'pathfinder.txt' every ::2::2 using 1:3:xtic(2) with boxes lc rgb 'blue', \
'pathfinder.txt' every ::3::3 using 1:3:xtic(2) with boxes lc rgb 'orange',
and my data
#x axis location, label, height of bar, low part of error bar, high part of error bar
0 A 209.3 200 219
1 B 4790.2 4700 4900
2 C 3771.2 3700 3900
3 D 170.3 150 200
I have seen similar posts, namely adding error bar to histogram in gnuplot and Adding error bars on a bar graph in gnuplot but I don't see how it applies with this case.
How can I modify my GNUPlot script to add error bars?
thanks so much!

The two links you mentioned basically contain the answer, however,
from you example it looks like you want filled bars with defined colors.
From help boxerrorbars:
The error bar will be drawn in the same color as the border of the
box.
So, you have several options:
(top left) "same color as the border of the box" of course doesn't make sense if you fill your bar, since the lower part of the error bar will disappear in the filled bar.
(top right) set the border e.g. "black", which will make the errorbar black as well
(bottom left) set the intensity of the solid fill, e.g. to 0.3, which will leave the border in intensity 1.0 and hence the errorbar as well.
(bottom right) split the plot in with boxes and with yerrorbars then your are completely independent for the colors. There is a point with the yerrorbar which you can let disappear by specifying ps 0 (or pointsize 0)
Script: (works with gnuplot>=5.0.0, Jan. 2015)
### filled bars with errorbars
reset session
$Data <<EOD
#x axis location, label, height of bar, low part of error bar, high part of error bar
0 A 25 17 33
1 B 50 40 60
2 C 85 71 91
3 D 40 33 47
EOD
set palette defined (1 "red", 2 "green", 3 "blue", 4 "orange")
unset colorbox
set key noautotitle
set yrange[0:100]
set grid y
set tics out
set offset 0.25,0.25,0,0
set multiplot layout 2,2
set style fill solid 1.0 noborder
plot $Data u 1:3:4:5:(0.5):0:xtic(2) w boxerrorbars lc palette
set style fill solid 1.0 border lc "black"
replot
set style fill solid 0.3 border
replot
set style fill solid 1.0
plot $Data u 1:3:(0.5):0:xtic(2) w boxes lc palette, \
'' u 1:3:4:5 w yerr pt 6 ps 1 lc "black"
unset multiplot
### end of script
Result:

Related

gnuplot 'no valid points' message when trying to plot bar graph and line chart

I'm trying to plot a bar chart with multiple bars on each point of the x axis, and a line chart on the same graph showing the ratio of the two columns over the top. I'm getting the bars to render fine, but getting the error "line 22: warning: Skipping data file with no valid points" for the line plot.
I'm guessing this is due to setting the plot style to histogram, but I'm not sure. Do I need to change it again to do the line plot? I've seen some examples online of single bars with a line plot over them, but I'm not sure about doing a clustered bar chart and a line. Can anyone help out please?
EDITED:
I've stripped my code and data file down from what I was using in my original post, to hopefully improve clarity and get rid of any excess data that might be messing things up.
The code I'm now using is as follows;
set terminal png size 800,500 enhanced font "Times,12"
set output 'test.png'
red = "#FF0000"; green = "#00FF00"; blue = "#0000FF"; skyblue = "#87CEEB";
set key right top
set yrange [0:0.2]
set ylabel "Percentage"
set y2range [0:1.0]
set y2label "Ratio"
set style data histogram
set style histogram cluster gap 1
set style fill solid
set boxwidth 0.9
set xtics format ""
set ytics
set y2tics
set grid ytics
set title "Some title"
plot "mydata.dat" using ($2/100.0):xtic(1) title "Group 1" linecolor rgb green, \
'' using ($3/100.0) title "Group 2" linecolor rgb blue , \
'' using 1:($2/$3) with lines axis x1y2
And the mydata.dat file looks like this;
# File of data
A 12.3 17.6
B 11.7 16.2
C 11.1 15.9
D 8.3 12.1
E 9.2 10.5
But I'm still getting a error message
"myfile.gnuplot", line 22: warning: Skipping data file with no valid points
This is again an example why it is important and helpful to always add data to a question.
You are trying to use column 1 as x coordinate. Since column 1 are characters, this won't work. Instead, use pseudocolumn 0, which is basically the line index (0-based), check help pseudocolumns. The histogram style is implicitly using pseudocolumn 0 as x-coordinate as well.
Script:
### add a line plot to a histogram
reset session
$Data <<EOD
# File of data
A 12.3 17.6
B 11.7 16.2
C 11.1 15.9
D 8.3 12.1
E 9.2 10.5
EOD
red = "#FF0000"; green = "#00FF00"; blue = "#0000FF"; skyblue = "#87CEEB";
set key right top out
set yrange [0:0.2]
set ylabel "Percentage"
set y2range [0:1.0]
set y2label "Ratio" tc "red"
set style data histogram
set style histogram cluster gap 1
set style fill solid
set boxwidth 0.9
set xtics format ""
set ytics nomirror
set y2tics tc "red"
set grid ytics
set title "Some title"
plot $Data u ($2/100.0):xtic(1) title "Group 1" lc rgb green, \
'' u ($3/100.0) title "Group 2" lc rgb blue , \
'' u 0:($2/$3) w lp pt 7 lc "red" axis x1y2 title "ratio"
### end of script
Result:
It is OK to add a line plot on top of your histogram components. I think your main problem is that you have mixed up the x and y columns of the line plot. I suggest try again with
plot "mydata.dat" using ($10/100.0):xtic(1) title "Group A" linecolor rgb green, \
'' using ($11/100.0) title "Group B" linecolor rgb blue , \
'' using 1:($10/$11) with lines axis x1y2
If that works, you can mark the answer as accepted.
If it does not work, please edit your question to explain what happened and perhaps add a snippet of the data so that we can check the formatting and column counts.

Show error bars in a multiaxis plot in Gnuplot

I have a dataset (show-errorbar.dat) containing:
Model# DE IE Error
Apple -4.6 -128.9538 4.0
Huawei -5.2 -176.6343 5.3
One-Pro -5.2 -118.1106 3.2
#!/usr/bin/gnuplot
#set terminal pdfcairo enhanced color font 'Helvetica,12' linewidth 0.8
set terminal png
set output 'BrandError.png'
set boxwidth 1.0 relative
set bmargin 5
set style fill solid border -1
set xtic rotate by -45 scale 0
#set auto x
set style line 81 lt 0 lc rgb "#808080" lw 0.5
set grid xtics
set grid ytics
set grid mxtics
set grid mytics
set grid back ls 81
set arrow from graph 0,first -4.6 to graph 1, first -4.6 nohead lw 2 lc rgb "#000000" front
set border 11
set border lw 2.0
set xtics font ",11"
set ytics font ",14"
set tics out
set ytics nomirror
set y2tics
set y2tics font ",14"
set mxtics 10
set mytics 2
set my2tics 2
set yrange [-10:0]
set y2range [-260:0]
set key left bottom
set y2label offset -2
set ylabel offset 2
set ylabel 'DE' tc rgb "red"
set y2label 'IE' tc rgb "green"
set style data histograms
set style histogram cluster gap 2
set linetype 2 lc rgb 'red'
set linetype 3 lc rgb 'yellow'
set linetype 4 lc rgb 'green'
plot 'show-errorbars.dat' using 2 ti 'DE' lc 2 axis x1y1, '' u 3:xticlabels(1) ti 'IE' lc 4 axis x1y2
set output
enter image description here
I would like to plot a histogram comparing DE vs IE and also show error bars (data in column 4) for the IE values.
Please any help on how to go about it.
There is a variant histogram style for exactly that purpose
set style histogram errorbars gap 2 {lw W}.
Here is the help section from the docs:
The `errorbars` style is very similar to the `clustered` style, except that it
requires additional columns of input for each entry. The first column holds
the height (y value) of that box, exactly as for the `clustered` style.
2 columns: y yerr bar extends from y-yerr to y+err
3 columns: y ymin ymax bar extends from ymin to ymax
The appearance of the error bars is controlled by the current value of
`set errorbars` and by the optional <linewidth> specification.
Updated answer
Notes:
You can't mix axis choice within a single histogram. So I have removed the axes x1y1 and axes x1y2 from the plot command. Since you have explicitly given the range for both y1 and y2, the plot border and labels are not affected.
However since the green bars are now being plotted against y1, we have to scale them so that the y2 axis labels apply. So the column 3 and column 4 values will be divided by 26, which is (y2 range) / (y1 range)
In "histogram errorbars" mode each plot component looks for an extra column of data to determine the size of the errorbar. Since your column 2 data has no corresponding column of errors, we dummy it up to use all a constant not-a-number (no data) value: (NaN)
Your data contains a line of columnheaders, which could confuse the program if it thinks this is a line of data. There are a number of ways you can tell the program to skip this line; I have used set key autotitle columnhead for convenience and because it is supported by old versions of gnuplot. If you have a current version it would be better to use instead set datafile columnheaders.
I have kept all of your commands except that the plot command is replaced by the following 3 lines:
set style histogram errorbars gap 2 lw 1.5
set key autotitle columnhead
plot 'show-errorbars.dat' using 2:(NaN) ti 'DE' lc 2, '' u ($3/26.):($4/26.):xticlabels(1) ti 'IE' lc 4

Color repetition when using boxes in gnuplot

My data file has just two columns.The following MWE on those columns produces boxes with repeated colors. Is it possible to produce unique colors for each box?
reset
set term postscript eps size 5.5,4.5 enhanced color solid lw 2\
font "arial,28"
set key right
set xtics rotate -45 font ",20"
set style fill solid 1 border -1
plot 'rankdefcount.dat' using ($0):2:($0):xticlabels(1) \
notitle w boxes lc variable
quit
Here is the output I got:
After few attempts and help from the SO experts, I came up with the following solutions; none of them perfect, though.
Solution 1: ( with a random repetition using rand and rgb calls)
reset
set term postscript eps size 5.5,4.5 enhanced color solid lw 2 font \
"arial,28"
set key right
rgb(r,g,b)=int(255*r)*65536+int(255*g)*256+int(255*b)
do for [i=1:31] {
myrand=rand(int(rand(0)*i*100)+i*100)
set style line i linecolor rgb rgb(rand(0),rand(0),rand(0))
}
set xtics rotate -45 font ",20"
set style fill solid 1 border -1
plot 'rankdefcount.dat' using ($0):2:($0):xticlabels(1) \
notitle w boxes lc variable
quit
Here is the corresponding output:
With palette definition (solution 2):
reset
set term postscript eps size 5.5,4.5 enhanced color solid lw 2 font \
"arial,28"
set key right
set palette color model HSV
set palette defined (0 0 1 1,1 1 1 1)
set palette defined ( 0 0 1 0, 1 0 1 1, 6 0.8333 1 1, 7 0.8333 0 1)
set boxwidth 0.5
unset colorbox
set xtics rotate -45 font ",20"
set style fill solid 1 border -1
plot 'rankdefcount.dat' using ($0):2:($0):xticlabels(1) \
notitle w boxes lc palette
quit
This is the output:
For another solution (solution 3), replace the definition above with the following lines:
set palette color model HSV
set pm3d explicit at b
set palette rgbformulae 3, 2, 2
This is what I got:
You may try and redefine as much linetypes as boxes you want to show. The code should go before the plot.
colors="black red orange #fa8072 ...." #[as much colors as needed]
do for [L=1:words(colors)]{
set linetype L lc rgb word(colors,L)
}
You can find colors for gnuplot here.
http://www.uni-hamburg.de/Wiss/FB/15/Sustainability/schneider/gnuplot/colors.htm

Gnuplot: how to fill a bar with both a color background and a pattern

I want to fill a bar with both a color background and a pattern. Is it possible in Gnuplot?
I am using Gnuplot 4.6.5
The code I have now:
# set terminal pngcairo transparent enhanced font "arial,10" fontscale 1.0 size 500, 350
# set output 'histograms.2.png'
set boxwidth 0.9 absolute
set style fill solid 1.00 border lt -1
set key inside right top vertical Right noreverse noenhanced autotitles nobox
set style histogram clustered gap 1 title offset character 0, 0, 0
set datafile missing '-'
set style data histograms
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 title "US immigration from Northern Europe\nPlot selected data columns as histogram of clustered boxes"
i = 22
set yrange [0:2000]
set style line 1 lc rgb 'orange';
set style line 2 lc rgb 'pink';
plot for [i=2:7] 'data.dat' using i:xtic(1) ti col ls i%2+1;
The data file:
Region Denmark France Demark-Women France-women Demark-man France-men
1891-1900 1000 1100 500 600 500 500
1901-1910 1500 1600 1000 600 500 1000
Here are the links to download the script: https://dl.dropboxusercontent.com/u/45318932/histograms2.plt and data file: https://dl.dropboxusercontent.com/u/45318932/data.dat
The script gives me:
What I want is:
It would be much appreciated if someone can help me improve the code to produce the second figure. Thanks.
That one is very tricky because you cannot change the background color of the fill patterns. And by default the background color of the patterns is white, and not transparent or empty.
The only terminal which can be manipulated adequately is the lua tikz terminal. Here, I first draw all the color boxes, and later in a second iteration the fill patterns. To have a new iteration, I use the newhistogram option, which however causes a gap in the legend.
To remove the white background of the fill patterns, I remove the relevant parts from the output stream with sed. Quite hacky, but it works:
set terminal lua tikz standalone size 5in, 3in color
set output '| sed ''s/\\gpfill{color=gpbgfillcolor}//g'' > histograms.tex'
set boxwidth 0.9 absolute
set style fill solid 1.00 border lt -1
set key inside right top vertical Right noreverse noenhanced autotitles nobox
set style histogram clustered gap 1 title offset character 0, 0, 0
set datafile missing '-'
set style data histograms
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 title "US immigration from Northern Europe\nPlot selected data columns as histogram of clustered boxes"
i = 22
set yrange [0:2000]
set xrange [-1:2]
set style line 1 lc rgb 'orange';
set style line 2 lc rgb 'pink';
plot for [i=2:7] 'data.dat' using i:xtic(1) ti columnhead(i > 3 ? 10 : i) ls i%2+1 fillstyle solid noborder,\
newhistogram at -1, \
'' using 2 ti 'total' lt -1 fillstyle empty,\
'' using 3 notitle lt -1 fillstyle empty,\
'' using 4 title 'women' lt -1 fillstyle pattern 5,\
'' using 5 notitle lt -1 fillstyle pattern 5,\
'' using 6 title 'men' lt -1 fillstyle pattern 6,\
'' using 7 notitle lt -1 fillstyle pattern 6
set output
system('pdflatex histograms.tex')
Result with 4.6.5:
Just found out, that you can specify the patterns to have no background color like
... fillstyle pattern 6 transparent
For which terminals that works depends on the gnuplot version:
plot x with filledcurves x1 fillstyle solid fc rgb '#990000',\
x with filledcurves x1 fillstyle pattern 4 transparent lc rgb 'white'
Result (with svg terminal and version 4.6.5):

Add a single point at an existing plot

I am using the following script to fit a function on a plot. In the output plot I would like to add a single value with etiquette on the fitting curve lets say the point f(3.25). I have read that for gnuplot is very tricky to add one single point on a plot particularly when this plot is a fitting function plot.
Has someone has an idea how to add this single point on the existing plot?
set xlabel "1000/T (K^-^1)" font "Helvetica,20"
#set ylabel "-log(tau_c)" font "Helvetica,20"
set ylabel "-log{/Symbol t}_c (ns)" font "Helvetica,20"
set title "$system $type $method" font "Helvetica,24"
set xtics font "Helvetica Bold, 18"
set ytics font "Helvetica Bold, 18"
#set xrange[0:4]
set border linewidth 3
set xtic auto # set xtics automatically
set ytic auto # set ytics automatically
#set key on bottom box lw 3 width 8 height .5 spacing 4 font "Helvetica, 24"
set key box lw 3 width 4 height .5 spacing 4 font "Helvetica, 24"
set yrange[-5:]
set xrange[1.5:8]
f(x)=A+B*x/(1000-C*x)
A=1 ;B=-227 ; C=245
fit f(x) "$plot1" u (1000/\$1):(-log10(\$2)) via A,B,C
plot [1.5:8] f(x) ti "VFT" lw 4, "$plot1" u (1000/\$1):(-log10(\$2)) ti "$system $type" lw 10
#set key on bottom box lw 3 width 8 height .5 spacing 4 font "Helvetica, 24"
set terminal postscript eps color dl 2 lw 1 enhanced # font "Helvetica,20"
set output "KWW.eps"
replot
There are several possiblities to set a point/dot:
1. set object
If you have simple points, like a circle, circle wedge or a square, you can use set object, which must be define before the respective plot command:
set object circle at first -5,5 radius char 0.5 \
fillstyle empty border lc rgb '#aa1100' lw 2
set object circle at graph 0.5,0.9 radius char 1 arc [0:-90] \
fillcolor rgb 'red' fillstyle solid noborder
set object rectangle at screen 0.6, 0.2 size char 1, char 0.6 \
fillcolor rgb 'blue' fillstyle solid border lt 2 lw 2
plot x
To add a label, you need to use set label.
This may be cumbersome, but has the advantage that you can use different line and fill colors, and you can use different coordinate systems (first, graph, screen etc).
The result with 4.6.4 is:
2. Set an empty label with point option
The set label command has a point option, which can be used to set a point using the existing point types at a certain coordinate:
set label at xPos, yPos, zPos "" point pointtype 7 pointsize 2
3. plot with '+'
The last possibility is to use the special filename +, which generates a set of coordinates, which are then filtered, and plotted using the labels plotting style (or points if no label is requested:
f(x) = x**2
x1 = 2
set xrange[-5:5]
set style line 1 pointtype 7 linecolor rgb '#22aa22' pointsize 2
plot f(x), \
'+' using ($0 == 0 ? x1 : NaN):(f(x1)):(sprintf('f(%.1f)', x1)) \
with labels offset char 1,-0.2 left textcolor rgb 'blue' \
point linestyle 1 notitle
$0, or equivalently column(0), is the coordinate index. In the using statement only the first one is taken as valid, all other ones are skipped (using NaN).
Note, that using + requires setting a fixed xrange.
This has the advantages (or disadvantages?):
You can use the usual pointtype.
You can only use the axis values as coordinates (like first or second for the objects above).
It may become more difficult to place different point types.
It is more involved using different border and fill colors.
The result is:
Adding to Christoph's excellent answers :
4. use stdin to pipe in the one point
replot "-" using 1:(f($1))
2.0
e
and use the method in 3rd answer to label it.
5. bake a named datablock
(version > 5.0) that contains the one point, then you can replot without resupplying it every time:
$point << EOD
2.0
EOD
replot $point using 1:(f($1)):(sprintf("%.2f",f($1))) with labels
6. A solution using a dummy array of length one:
array point[1]
pl [-5:5] x**2, point us (2):(3) pt 7 lc 3
7. Or through a shell command (see help piped-data):
pl [-5:5] x**2, "<echo e" us (2):(3) pt 7 lc 3
pl [-5:5] x**2, "<echo 2 3" pt 7 lc 3
8. Special filename '+'
pl [-5:5] x**2, "+" us (2):(3) pt 7 lc 3
It seems to be the shortest solution. But note that while it looks like a single point, these are like 500 points (see show samples) plotted on the same position.
To have only one point the sampling needs to be temporarily adjusted (see help plot sampling)
pl [-5:5] x**2, [0:0:1] "+" us (2):(3) pt 7 lc 3
9. Function with zero sampling range length
Shortest to type, but plotting as many points on top of each other as many specified with samples
pl [-5:5] x**2, [2:2] 3 w p pt 7 lc 3

Resources