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
Related
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
I've got the following data:
# id min 1st quart median 3rd quart max sum std-dev name
1 0.00029 0.02590 0.06331 0.12910 6.50177 1524.58705 0.13483 spec
2 1.50041 1.59762 1.67226 1.79827 13.45151 26583.69935 0.48373 schema
3 0.00206 0.01292 0.02505 0.09679 116.93156 5337.36854 2.06006 truss
And the following gnuplot script:
set terminal png enhanced background rgb 'white' size 1920,1200 font "Verdana 10"
set output "charts/summary.png"
set boxwidth 0.2 absolute
set title "Validating all data"
set xrange[0:4]
set yrange[0.00005:50]
set logscale y
set grid y
set tics scale 0
set xtics rotate by -45
set xtics nomirror
set ytics nomirror
set border 2
set style fill solid 0.25 border -1
set style data boxplot
plot "data/summary" using 1:3:2:6:5:(0.6):xticlabels(9) with candlesticks title 'Quartiles' whiskerbars, \
'' using 1:4:4:4:4:(0.6) with candlesticks lt -1 notitle
The output of which looks like:
The problem being that I can't figure out how to set the background color of the boxes in the boxplot. I can only seem to turn on and off the fill-in color by removing the set style fill solid 0.25 border -1 line.
Although the name of keyword doesn't suggest so, the linecolor seems to affect the color of the boxes, thus for example:
plot "data/summary" using 1:3:2:6:5:(0.6):xticlabels(9) with candlesticks lc rgb 'blue' title 'Quartiles' whiskerbars, \
'' using 1:4:4:4:4:(0.6) with candlesticks lt -1 notitle
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:
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):
is it possible to change the color of bars in a Gnuplot script dynamically?
I have the following script
reset
fontsize = 12
set term postscript enhanced eps fontsize
set output "bargraph_speedup.eps"
set style fill solid 1.00 border 0
set style histogram
set style data histogram
set xtics rotate by -45
set grid ytics linestyle 1
set xlabel "Benchmarks" font "bold"
set ylabel "Relative execution time vs. reference implementation" font "bold"
set datafile separator ","
plot 'bm_speedup.dat' using 2:xtic(1) ti "Speedup" linecolor rgb "#00FF00"
which generates this plot:
Is it possible to make the color of the bars which are below zero red?
Thanks,
Sven
You can mimic this behavior using the boxes style:
My test data:
zip 2
baz 2
bar -1
cat 4
foo -3
And then plotting with gnuplot:
set style line 1 lt 1 lc rgb "green"
set style line 2 lt 1 lc rgb "red"
set style fill solid
plot 'test.dat' u (column(0)):2:(0.5):($2>0?1:2):xtic(1) w boxes lc variable
# #xval:ydata:boxwidth:color_index:xtic_labels
You could split your data file into two parts, positive values and negative, and plot them separately:
plot 'bm_speedup_pos.dat' using 2:xtic(1) ti "Faster" linecolor rgb "#00FF00", \
'bm_speedup_neg.dat' using 2:xtic(1) ti "Slower" linecolor rgb "#FF0000"
Or, if you only need to generate a few graphs, a few times, a common technique is to generate the raw graph in gnuplot, then post-process it in an image editor to adjust the colors. If you go that route, I suggest having gnuplot generate the graph in SVG format, which will give you much better looking graphs than any of the bitmap formats.
Doesn't seem like histogram lets you do it. May be like this:
set boxwidth 0.3
f(v)=v<0?1:2
plot 'bm_speedup.dat' using 0:2:(f($2)):xticlabels(1) with boxes ti "Speedup" lc variable
Actually you can also use linecolor rgb variable and give the color like this:
plot 'bm_speedup.dat' using 2:xtic(1):($2 >= 0 ? 0x00FF00 : 0xFF0000) ti Speedup lc rgb variable