Different coloured bars in gnuplot bar chart? - gnuplot

I have a very simple dataset:
Critical 2
High 18
Medium 5
Low 14
Creating a bar chart in gnuplot out of this dataset is easy, but all the bars are the same colour. I want to have it so that Critical is black, high is red, etc, but there seem to be hardly any online tutorials for doing this.
Can anyone point me in the right direction?

set xrange [-.5:3.5]
set yrange [0:]
set style fill solid
plot "<sed 'G;G' test.dat" i 0 u (column(-2)):2:xtic(1) w boxes ti "Critical" lc rgb "black",\
"<sed 'G;G' test.dat" i 1 u (column(-2)):2:xtic(1) w boxes ti "High" lc rgb "red" ,\
"<sed 'G;G' test.dat" i 2 u (column(-2)):2:xtic(1) w boxes ti "Medium" lc rgb "green",\
"<sed 'G;G' test.dat" i 3 u (column(-2)):2:xtic(1) w boxes ti "Low" lc rgb "blue"
This takes sed and triple spaces your file so that gnuplot sees each line as a different dataset (or "index"). You can plot each index separately using index <number> or i <number> for short as I have done. Also, the index number is available as column(-2) which is how we get the boxes properly spaced.
Possibly a slightly more clean (gnuplot only) solution is using filters:
set xrange [-.5:3.5]
set yrange [0:]
set style fill solid
CRITROW(x,y)=(x eq "Critical") ? y:1/0
HIGHROW(x,y)=(x eq "High") ? y:1/0
MIDROW(x,y) =(x eq "Medium") ? y:1/0
LOWROW(x,y) =(x eq "Low") ? y:1/0
plot 'test.dat' u ($0):(CRITROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "black" ti "Critical" ,\
'' u ($0):(HIGHROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "red" ti "High" ,\
'' u ($0):(MIDROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "green" ti "Medium" ,\
'' u ($0):(LOWROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "blue" ti "Low"
This solution also doesn't depend on any particular ordering in your datafile (which is why I prefer it slightly to the other solution. We accomplish the spacing here with column(0) (or $0) which is the record number in the dataset (in this case, the line number).

Here is how you can do this using the linecolor variable option.
If you know, that the lines are always in the same, known order, you can use the row number (zeroth column, $0) as linetype index:
set style fill solid noborder
set linetype 1 lc rgb 'black'
set linetype 2 lc rgb 'red'
set linetype 3 lc rgb 'yellow'
set linetype 4 lc rgb 'green'
set yrange [0:*]
unset key
plot 'alerts.txt' using 0:2:($0+1):xtic(1) with boxes linecolor variable
If the order may vary, you can use a gnuplot-style indexing function, which determines the index of the warning level from a string with space-separated words:
alerts = 'Critical High Medium Low'
index(s) = words(substr(alerts, 0, strstrt(alerts, s)-1)) + 1
set style fill solid noborder
set linetype 1 lc rgb 'black'
set linetype 2 lc rgb 'red'
set linetype 3 lc rgb 'yellow'
set linetype 4 lc rgb 'green'
set yrange [0:*]
unset key
plot 'alerts.txt' using 0:2:(index(strcol(1))):xtic(1) with boxes linecolor variable

Related

Fill the area between two curves

I have two functions and want to fill the area between them
enset xlabel "x"
set ylabel "y"
set yrange[-20:30]
set xrange[-30:120]
plot 10*sin(0.2*x) title "f(x) = 10 sin(0.2x)" lw 2
replot sqrt(x) title "g(x) = sqrt(x)" lw 2
replot [0:73.4] -sqrt(x)+10*sin(0.2*x) with boxes title "Área de intersección"
But I didn't get what i expected. How can I fill this space correctly?
f(x) = 10*sin(0.2*x)
g(x) = sqrt(x)
set key opaque box
set xrange [0:73.4]
plot '+' using 1:(f(x)):(g(x)) with filledcurves between fillcolor "grey" notitle, \
f(x) with lines lt 2 lw 2 title "f(x)", \
g(x) with lines lt 3 lw 2 title "g(x)"
Adjust titles, colors, linewidth as you like.

gnuplot partially stacked bars

I would like to make a gnuplot graph with one full bar and another one stacked.
The data is something like
x data E M L
[10,20) 0.000000 1.081916 2.0958133 5.473606
[20,40) 0.000000 2.331769 2.1402838 4.528341
[40,80) 3.262375 2.201788 1.3499280 3.023847
>80 2.368121 2.132216 0.7322889 2.610368
where E, M and L are the three "green" components.
I have tried to do something like
set style data histograms
set style histogram rowstacked
#set style histogram cluster gap 1
set style fill solid
...
plot newhistogram, "size_class_data/tree_paracou.txt" using 2:xticlabel(1) linecolor rgb data_color title "Model",\
newhistogram, '' using 3:xticlabel(1) linecolor rgb early_color title "d",\
'' using 4 linecolor rgb mid_color title "g",\
'' using 5 linecolor rgb late_color title "f"
but the two graphs lie on two different parts of the x axis.
The newhistogram command has an option at for setting the coordinate of the first box.
In you case you can fix the boxwidth, and start the second histogram at 0 + boxwidth:
set style data histograms
set style histogram rowstacked
set style fill solid
set boxwidth 0.33 absolute
plot "foo.dat" using 2:xticlabel(1) skip 1 linecolor rgb "black" title "Model",\
newhistogram at 0.33, '' using 3 skip 1 linecolor rgb "red" title "d",\
'' using 4 skip 1 linecolor rgb "green" title "g",\
'' using 5 skip 1 linecolor rgb "blue" title "f"

How can I plot two surfaces that intersect each other

I've been trying to make 3d plots of surfaces from datasets using gnuplot. I have been partially successful using the following code:
set view 65,45
unset colorbox
set lmargin 14
set pm3d depthorder nohidden3d
set ylabel "angles [deg]"
set zlabel offset -2,0 "energy [eV]"
set palette defined (0 'blue', 1 "blue", 3 "blue", 4 'red')
splot "S1.dat" u 1:2:3:4 w pm3d ti "S1", "T2.dat" u 1:2:3:4 w pm3d ti "T2"
which results in the following image:
image1
The problem is, the border region (the "seam") between the two surfaces is not clearly visible because the tiles are colored with one color only. To make things clearer, here's what I obtain without pm3d (with or without hidden3d):
set terminal png size 800,800
set output "testimg2.png"
set view 65,45
unset colorbox
set lmargin 14
set isosamples 19,44
set hidden3d
set ylabel "angles [deg]"
set zlabel offset -2,0 "energy [eV]"
set palette defined (0 'blue', 1 "blue", 3 "blue", 4 'red')
#splot "S1.dat" u 1:2:3:4 w pm3d ti "S1", "T2.dat" u 1:2:3:4 w pm3d ti "T2"
splot "S1.dat" u 1:2:3 w l lc rgb "blue" ti "S1", "T2.dat" u 1:2:3 w l lc rgb "red" ti "T2"
unset hidden3d
set output "testimg3.png"
splot "S1.dat" u 1:2:3 w l lc rgb "blue" ti "S1", "T2.dat" u 1:2:3 w l lc rgb "red" ti "T2"
image 2 (hidden3d)
image 3 (no hidden3d)
So essentially what I want is a picture like my second image, where you can clearly see the border between the surfaces, but with filled tiles.
The closest I've seen to this is fig 3 in the following page:
http://www.gnuplotting.org/klein-bottle/
where you can see where the bottle meets the handle.
Can anyone help? If this is not possible with gnuplot, can anyone suggest an alternative?
to plot 2 planes in 3d, enter functions for z, comma separated, e.g.
plane 1: 4x+2y+2z = 12
plane 2: -3x-2y+8z = 12
gnuplot> splot (-4*x-2*y+12)/2, (3*x+2*y+12)/8
plot of 2 planes

Change the color of the points in labelled plot

The script works, without any change in color, however:
plot '_numXY' using 2:3:(sprintf('%d', $1)) \
with labels offset 0,1 point pointtype 6 ps 2 notitle lc rgb "blue"
The points are black. I want to see the points in blue color.
Why does lc rgb "blue" not work?
The lc settings are ignored for the labels plotting style if they don't follow immediately the point option. In your case you must only place the notitle at the end.
plot '_numXY' using 2:3:(sprintf('%d', $1)) \
with labels offset 0,1 point pointtype 6 ps 2 lc rgb "blue" notitle
As a demonstration example:
set samples 11
set xrange [0:10]
plot '+' using 1:1:(sprintf('%d', $1)) \
with labels offset 0,1 point pointtype 6 ps 2 lc rgb "blue" notitle
The result with 4.6.5 is:

Change patterns of bars in gnuplot

How to change the pattern of the last column ?
Here is the script:
set ylabel "Accuracy"
set style fill solid border
set style line 1 lt 1 lc rgb "blue"
set style line 2 lt 1 lc rgb "green"
plot "orientation.csv" u (column(0)):2:(0.5):($2!=0.875?1:2) title "" w boxes lc variable fs pattern 1;
Unfortunately there is no option fillstyle variable. You must split the plot command in two parts: one for each pattern:
set boxwidth 0.5 absolute
unset key
set style fill solid border
set style line 1 lt 1 lc rgb "blue"
set style line 2 lt 1 lc rgb "green"
plot "orientation.csv" u 0:($2 != 0.875 ? $2 : 1/0) with boxes ls 1 fs pattern 1,\
"" u 0:($2 != ? 1/0 : $2) with boxes ls 2 fs pattern 2

Resources