setting space between legend/pattern with the text in stacked bar-chart - gnuplot

I have gnuplot data file:
CS 31.73 18.32 20.78 22.88 1.97 1.29 0.90 2.01
FL 43.27 29.45 15.64 6.55 1.64 1.27 2.18 0.00
HB 32.44 20.43 14.89 14.53 5.42 10.92 0.80 0.31
HD 28.53 7.27 29.53 20.33 9.18 0.93 2.04 1.44
MR 22.56 36.19 10.66 15.51 9.45 4.32 0.80 0.31
ZK 27.15 10.81 27.40 16.10 8.05 7.24 1.06 1.71
All 30.00 21.35 18.05 16.82 6.14 5.39 1.11 0.85
And my gnuplot Script:
set term pos eps font 20
set style data histogram
set style histogram rowstacked
set key invert reverse above
set boxwidth 0.8
set format y "%.0f%%"
set border 3
set yrange [0:100]
set size 0.9 , 1.8
set label 1 "1092" at -0.3,103 font "Times-Roman, 17"
set label 2 "500" at 0.7,103 font "Times-Roman, 17"
set label 3 "1000" at 1.8,103 font "Times-Roman, 17"
set label 4 "500" at 2.8,103 font "Times-Roman, 17"
set label 5 "1000" at 3.8,103 font "Times-Roman, 17"
set label 6 "500" at 4.8,103 font "Times-Roman, 17"
set label 7 "500" at 5.8,103 font "Times-Roman, 17"
set output 'aspect.eps'
plot 'a3b-aspect' \
using($2):xtic(1) t "pattern 1" lc rgb "#006600" lt -1 fs pattern 1, \
'' using($3) t "pattern 2" lc rgb "#006600" lt -1 fs pattern 2, \
'' using($4) t "pattern 3" lc rgb "#330000" lt -1 fs pattern 3, \
'' using($5) t "pattern 4" lc rgb "#000099" lt -1 fs pattern 4, \
'' using($6) t "pattern 5" lc rgb "#000099" lt -1 fs pattern 5, \
'' using($7) t "pattern 6" lc rgb "#000099" lt -1 fs pattern 6, \
'' using($8) t "pattern 7" lc rgb "#000099" lt -1 fs pattern 7, \
'' using($5) t "pattern 3" lc rgb "#660000" lt -1 fs pattern 3, \
'' using($6) t "pattern 3" lc rgb "#990000" lt -1 fs pattern 3
Output:
I have to delete some space between legend(patterns above) and text. Could we custom rectangle size on that? and also invert my legend order... the bottom patterns in bar chart should be the first legend.
Thanks

The default justification of the legend key is Right which isn't appropriate when using reverse like you do: Set the Left option.
Optionally you can also increase or decrease the space reserved for the text with the width option.
To have to lower row appearing first, don't use the invert option.
So with the key settings
set key reverse above Left width 1
you get the result

Related

Shifting of axis title and sub-title in Gnuplot histogram

Mr data file is
# test.dat
# Sample No. Phenol Red Neutral Red Bromophenol Blue Bromocresol Green Mixed Indicator
1 80 69 223 222 130
2 89 62 321 267 160
3 92 67 248 276 147
Gnuplot code is
#
#
###
reset session
###
set terminal postscript eps enhanced colour font 'Times-Roman,12' size 6in,4in
set output "test.eps"
#------------------------------------------------------------------------------
set style line 1 lt 4 lw 1 lc rgb "#4169E1"
set style line 2 lt 4 lw 1 lc rgb "#2E8B57"
set style line 3 lt 5 lw 1 lc rgb "#FF4500"
set style line 4 lt 4 lw 1 lc rgb "#FAD02C"
set style line 5 lt 4 lw 1 lc rgb "#FF0080"
#------------------------------------------------------------------------------
set xtics out scale 1.5
set ytics out scale 1.5
set ytics nomirror
set yrange [0:350]
set key off
set tics font ", 16"
set xlabel "Sample No. " font ",16"
set ylabel "Moisture Content ({/Symbol \155}g of H_{2}O/g of melt)" font ",16"
set key samplen 2 spacing 1.65 font ",14" width -1.0
set key inside left horizontal
set style fill solid border
set style histogram clustered gap 1 title textcolor lt -1
# ----------------------------------------
set rmargin screen 0.9
set lmargin screen 0.1
set bmargin screen 0.4
# ----------------------------------------
set style data histogram
plot \
newhistogram "Direct titration", \
'test.dat' using 2:xtic(1) ls 4 axes x1y1 title "Phenol Red", \
'' u 3 ls 5 axes x1y1 title "Neutral Red", \
newhistogram "Back-titration", \
'test.dat.dat' using 4:xtic(1) ls 1 axes x1y1 title "Bromophenol Blue", \
'' u 5 ls 2 axes x1y1 title "Bromocresol Green", \
'' u 6 ls 3 axes x1y1 title "Mixed Indicator"
# ----------------------------------------
##
Script works fine. I would like to provide more spacing between the tick labels (1,2,3) and the sub-title (Direct titration and Back-titration) as well as spacing between the sub-titles and axis title. How can it be done?
set xlabel offset 0, -1 will shift both the subtitles and the axis title downward by one character height.
set xlabel "\nTEXT" will create a two-line axis label and place TEXT on the second line.
Modifying your xlabel command to be
set xlabel "\nSample No. " font ",16" offset 0,-1
will make both those changes.

Plot a error bar as shaded region in GNUPLOT

I have plotted a graph (X-top axis, Y-bottom axis) with fsteps function in Gnuplot. Next, I tried to add an error bar as a shaded region(transparent) to the graph, but unable to plot it on the graph. Below is the code so far I have tried and also attached the graph.
#!/usr/bin/gnuplot
reset
set border lw 30
set term pngcairo size 10000,10000 font "arial-Bold,130"
set output 'out.png'
unset key
set size ratio 1.2
set style data lines
set xtics format ""
set x2tics nomirror
set ytics out nomirror
set ytics 0,20
set x2label "Vs (km/s)" offset -1.0
set ylabel 'Depth (km)' offset 1.5
set xrange [2.5:4.8]
set yrange [314:0]
set label 3 at 2,120
set key samplen 1.7 at 3.0,135
#
set label 1 '(a)' font "arial-Bold,130" at 0.8,15 right
set label 3 "C3 (MNAI)" center font "arial-Bold,130"
set style fill transparent solid 0.25
set style fill noborder
plot 'MAN.inmd' lc rgb 'blue' lw 35 title "Initial model" with fsteps,\
'MAN.outmd' using 1:2 lc rgb 'red' lw 35 dt"-" title "Inverted model" with fsteps ,\
'MAN.outmd' using 1:($2-$3):($2+$3) with filledcurve lc "blue" notitle,
Example Data for file MAN.outmd X Y Z(Error)
0 3 0
0.4475 3.1 0
0.4475 3.5 0
2.6738 3.6 0.0552
2.6738 5 0.0552
3.8441 5.1 0.0592
3.8441 8 0.0592
3.6302 8.1 0.0395
3.6302 15.935 0.0395
4.5176 15.1 0.041
4.5176 113.296 0.041
4.2443 113.3 0.1024
4.2443 214 0.1024
4.4584 214.1 0.1077
4.4584 314 0.1077
I want output should be as given below (example)
gnuplot can easily fill the area between two "horizontal" curves (i.e. unique x-values), but as far as I know, not between two vertical curves. However, gnuplot can fill some enclosed areas. So, the workaround is to create datapoints which surround the area to be shaded. For this, you "plot" the data into a datablock, once "forward" with x-dx and once "backwards" with x+dx. This can be done easiest if you have the data already in a datablock, because then you can easily loop the data forward and backwards. In case you have your data in a file, see here: gnuplot: load datafile 1:1 into datablock
Code:
### fill between vertical curves
reset session
$Data <<EOD
0 3 0
0.4475 3.1 0
0.4475 3.5 0
2.6738 3.6 0.0552
2.6738 5 0.0552
3.8441 5.1 0.0592
3.8441 8 0.0592
3.6302 8.1 0.0395
3.6302 15.935 0.0395
4.5176 15.1 0.041
4.5176 113.296 0.041
4.2443 113.3 0.1024
4.2443 214 0.1024
4.4584 214.1 0.1077
4.4584 314 0.1077
EOD
# create datablock with circumference of shaded area
set print $XErrorFill
do for [i=1:|$Data|] {
print real(word($Data[i],1))-real(word($Data[i],3)), real(word($Data[i],2))
}
do for [i=|$Data|:1:-1] {
print real(word($Data[i],1))+real(word($Data[i],3)), real(word($Data[i],2))
}
set print
set yrange [:] reverse
set style fill noborder
plot $XErrorFill u 1:2 w filledcurves lc "light-grey" notitle, \
$Data u 1:2 w l lw 1.5 lc rgb "red" notitle
### end of code
Result:

How to demonstrate LaTeX formula with Gnuplot?

I have attempted to demonstrate a LaTeX formula with Gnuplot 5.0.
But I found that some LaTeX commands are unavailable.
This is my Gnuplot Code:
set termoption enhanced
set title "Title test^a \alpha $\alpha$" font "CMU-Serif, 18"
set xlabel "Month"
set ylabel "Precipitation (mm)"
set xrange [0.5: 12.5]
set xtics 1,1,12
set key
set pointintervalbox 2
plot "08_Data.dat" using 1:2 with linespoints \
linecolor "#FF7800" linewidth 2 pointtype 7 pointsize 0.75 pointinterval -1 \
title "Beijing",\
"08_Data.dat" using 1:3 with linespoints \
linecolor "#00A0DC" linewidth 2 pointtype 7 pointsize 0.75 pointinterval -1 \
title "Shanghai",
set output
pause (-1)
As a result, the LaTeX command test^a works well but the LaTeX \alpha and $\alpha$ don't work:
This is the file: 08_Data.dat :
1 2.5 38.1
2 5.1 58.4
3 10.2 81.3
4 25.4 101.6
5 27.9 114.3
6 71.1 152.4
7 175.3 129.5
8 182.9 132.1
9 48.3 154.9
10 17.8 61.0
11 5.1 50.8
12 2.5 35.6
set term tikz standalone size 7cm, 3cm fontscale 0.6
set ylabel '{\LaTeX\ -- $ \gamma $}'
set xlabel '{\LaTeX\ -- $ x $}'
set output 'example.tex'
plot [0:1] gamma(x) title '$ \gamma(x) $'
unset output
!pdflatex example
Have you tried to directly type in the alpha character in the gnuplot code? I guess gnuplot understands UTF-8.
set title "test^a α α" font "CMU-Serif, 18"
plot x
Result:

gnuplot: make only one border thicker in a surface plot

So I am trying to plot a three-dimensional sphere on the first quadrant like this --
I want to make the line BC thicker/bold from the rest. How do I do that?
This is my gnuplot script:
set term wxt 0
set parametric
set urange[0:0.5 * pi]
set vrange[0:0.5 * pi]
r = 1
fx(v,u) = r * cos(v) * cos(u)
fy(v,u) = r * sin(u) * cos(v)
fz(v) = r * sin(v)
set label 1 "A" font "Arial, 15" front at 0.000, 0.900, -0.050
set label 2 "B" font "Arial, 15" front at 0.050, 0.000, 0.900
set label 3 "C" font "Arial, 15" front at 1.050, 0.105, 0.000
set pm3d depthorder border linetype 1 linewidth 0.50
set style fill transparent solid 0.50 border
set palette
set hidden3d
unset colorbox
splot fx(u,v), fy(u,v), fz(u) w pm3d, \
"< echo '0.000 0.000 1.000'" with points pt 7 ps 0.75 lc rgb 'black', \
"< echo '0.000 1.000 0.000'" with points pt 7 ps 0.75 lc rgb 'black', \
"< echo '1.000 0.000 0.000'" with points pt 7 ps 0.75 lc rgb 'black'
set term push
set term pdf enhanced mono
set output "b1.pdf"
replot
unset output
set term pop
system(sprintf("%s", "pdfcrop b1.pdf b1.pdf"))
well, simply plot the line B,C again with lines.
And what is this line in parametric representation? fx(u,0), fy(0,0), fz(u)
splot fx(u,v), fy(u,v), fz(u) w pm3d, \
fx(u,0), fy(0,0), fz(u) w l lw 3 lc rgb "red"
Which gives you something like this... (well, different terminal...)

gnuplot - calculate distance between lines

Can gnuplot calculate the distance between two lines or maybe two points?
I'm having a plot where two (main) lines are plotted. For the moment let's assume that the first line is always above the second one. Is there a way to calculate the distance from line 2 to line 1 at a given x-value?
here is a picture of what my plot looks like and which distance I want to calculate:
The vertical lines are just for style and have nothing to do with the actual plot, their data is stored in test.dat and test2.dat.
My data-files of the lines look like this:
line1
0 118.1
2.754 117.77
4.054 117.64
6.131 116.17
7.7 116.04
8.391 115.36
10.535 115.25
11.433 116.03
12.591 116.22
19.519 118.59
line2
19.4 118.51
15.2 116.56
10.9 115.94
10.35 114.93
9.05 114.92
8.3 115.9
5.9 116.19
4.2 116.62
2.2 117.66
-0.3 118.06
My plotting-code looks like this:
set term wxt enhanced
cd 'working directory'
unset key
set size 0.9,0.9
set origin 0.1,0.1
set title 'TITLE'
unset border
set label 21 " rotate by 45" at -3.0,0.0 rotate by 45 point ps 2
set xrange [0:19.519]
set yrange [110:119]
set xtics nomirror(0, 2.745, 4.054, 6.131, 7.7, 8.391, 10.535, 11.433, 12.591, 19.519) rotate by 90 offset 0,-0.1 right
set ytics " ", 30000
plot "line1.dat" using ($1):($2):2 with labels offset 1, 1.8 rotate by 90, "line1.dat" using 1:2 with lines lt 1 lw 1 lc rgb '#000000', +112 lt 1 lw 1 lc rgb '#000000' , 'test.dat' with lines lt 1 lw 1 lc rgb '#000000', +110 lt 1 lw 1 lc rgb '#000000', 'line2.dat' with lines lt 0.5 lw 1 lc rgb '#000000', 'test2.dat' with lines lt 0.5 lw 1 lc rgb '#000000'
You can measure the distance manually. Move the mouse to the first point and type 'r'. Then as you move the mouse around, the x and y offsets, distance and angle are displayed. Type '5' to draw a line segment and to toggle between degrees and tangent display. Zooming in beforehand increases accuracy.
By the way, typing 'h' in the plot window will display a list of keybindings to the console.
An answer to this "rather old" question still might be of interest to OP, if not, maybe to others.
Yes, you can calculate and plot the difference of two lines. It requires some linear interpolation. Simply assign the desired x-value to the variable myX.
Data:
SO17717287_1.dat
0 118.1
2.754 117.77
4.054 117.64
6.131 116.17
7.7 116.04
8.391 115.36
10.535 115.25
11.433 116.03
12.591 116.22
19.519 118.59
SO17717287_2.dat
19.4 118.51
15.2 116.56
10.9 115.94
10.35 114.93
9.05 114.92
8.3 115.9
5.9 116.19
4.2 116.62
2.2 117.66
-0.3 118.06
Script: (works for gnuplot>=4.6.0)
### calculating and plotting a difference between two curves
reset
FILE1 = "SO17717287_1.dat"
FILE2 = "SO17717287_2.dat"
set border 1
unset key
set origin 0.05,0.05
set size 0.9,0.8
set xrange [0:19.519]
set xtics nomirror rotate by 90 offset 0,-0.1 right
set yrange [110:119]
unset ytics
myX = 15.2
getYa(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? ya=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? ya=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
getYb(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? yb=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? yb=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
set samples 2 # set to minimal possible value for plotting '+'
plot x1=y1=NaN FILE1 u 1:2:2:xtic(1) w labels offset 0,0.5 left rotate by 90, \
'' u 1:(getYa(myX),$2) w l lc rgb 'black', \
'' u 1:2:(0):(110-$2) w vec lt 0 nohead, \
+112 w l lc rgb 'black', \
x1=y1=NaN FILE2 u 1:(getYb(myX),$2) w l lt 0 lc rgb 'black', \
'+' u (myX):(ya):(0):(yb-ya) w vec heads lc rgb "red", \
'+' u (myX):(ya):(sprintf("%.3f",yb-ya)):xtic(sprintf("%g",myX)) w labels tc rgb "red" offset 0,1, \
'+' u (myX):(ya):(0):(110-ya) w vec nohead lt 0 lc rgb "red"
### end of script
Result: (created with gnuplot 4.6.0)

Resources