how to make rainbow box in GNUPLOT - gnuplot

I want to make a rainbow filled box on the visible light area with planck distribution curve.
I don't have any idea how to fill this box with rainbow...
I set a box like
set object 1 rectangle from 3600,0 to 8200,3.6 fs solid 0.4
and
fc rgb
is for coloring.
But only what I can do is fill it with one color.
I saw some coloring demos for plotting, like with pm3d or some palette,
but it doesn't put em onto obj coloring with palette.
it's from wiki and I want that rainbow box:

I actually wanted to make the exact same plot last year. I could not find a gnuplot solution (which I am pretty sure exists...), and in the end I wrote a script with an awk part to generate a file with RGB colors for a range of wavelengths, and then plotted those in gnuplot with impulses lc rgb variable. I used code from a few SO questions, but cannot remember the original sources. Anyway, using the code below:
#!/bin/bash
seq 4000 1 7000 | awk '{
wv=$1/10
if(wv < 380){
rd = -(380. - 440.) / (440. - 380.)
gr = 0.0
bl = 1.0}
if(wv >= 380 && wv < 440){
rd = -(wv - 440.) / (440. - 380.)
gr = 0.0
bl = 1.0}
if(wv >= 440 && wv < 490){
rd = 0.0
gr = (wv - 440.) / (490. - 440.)
bl = 1.0}
if(wv >= 490 && wv < 510){
rd = 0.0
gr = 1.0
bl = -(wv - 510.) / (510. - 490.)}
if(wv >= 510 && wv < 580){
rd = (wv - 510.) / (580. - 510.)
gr = 1.0
bl = 0.0}
if(wv >= 580 && wv < 645){
rd = 1.0
gr = -(wv - 645.) / (645. - 580.)
bl = 0.0}
if(wv >= 645 && wv <= 730){
rd = -(wv - 780.) / (780. - 645.)
gr = 0.0
bl = 0.0}
if(wv > 730){
rd = -(730 - 780.) / (780. - 645.)
gr = 0.0
bl = 0.0}
rgb=int(255*rd)*2^16+int(255*gr)*2^8+int(255*bl)
printf("%7.4f %f %f %f %f %3d %3d %3d %10d\n",$1/10000,41,rd,gr,bl,rd*255,gr*255,bl*255,rgb)
}' > rgb.tmp
gnuplot << GNU
set term pdf size 7,7 font "courier,16"
set output 'bblaw.pdf'
set encoding iso
set border -1 lw 0.90
set tics front
set style line 1 pt 7 ps 1.50 lt -1 lw 2.5 lc "#608DB6CD"
set style line 2 pt 5 ps 1.50 lt -1 lw 2.5 lc "#60EE6363"
set style line 3 pt 9 ps 1.00 lt -1 lw 2.5 lc "#FFB90F"
set style line 4 pt 5 ps 1.50 lt -1 lw 2.5 lc "#EEEEEE"
set xrange [0.10:1.65]
set yrange [0:41]
set ytics 5
set mytics 5
set xtics 0.25
set mxtics 5
set format x "%4.2f"
set ylabel "Intensity (kW . sr^{-1} . m^{-2} . {/Symbol m}m^{-1})"
set xlabel "Wavelength ({/Symbol m}m)"
set label "5000K" at 0.50,14.0 front font ",15"
set label "5500K" at 0.45,21.5 front font ",15"
set label "5778K (Sun)" at 0.40,27.0 front font ",15"
set label "6000K" at 0.45,32.5 front font ",15"
set label "8000K" at 0.22,39.0 front font ",15"
unset key
# length unit is micrometre
c=3e14 # speed of light
h=6.626e-22 # Planck constant
k=1.38e-11 # Boltzmann constant
# Planck curves
p1(x)=1e-6*2*h*c**2/(x**5*(exp(h*c/(x*k*5000))-1))
p2(x)=1e-6*2*h*c**2/(x**5*(exp(h*c/(x*k*5500))-1))
p3(x)=1e-6*2*h*c**2/(x**5*(exp(h*c/(x*k*5778))-1))
p4(x)=1e-6*2*h*c**2/(x**5*(exp(h*c/(x*k*6000))-1))
p5(x)=1e-6*2*h*c**2/(x**5*(exp(h*c/(x*k*8000))-1))
plot "rgb.tmp" u 1:2:9 not w imp lc rgb variable lw 0.5,\
p1(x) lw 6, p2(x) lw 6, p3(x) lw 6, p4(x) lw 6, p5(x) lw 6
GNU
rm rgb.tmp
You will get something like this:
If you run this bash script on your terminal, it should generate a pdf file and remove any temporary files that were created in the process. Note that you can change the awk script at the beginning, in case you want to move the color scale a bit. In this particular case, I limited the wavelength range to 4000:7000 angstroms by editing the seq command on line three.
Let me know if this solution works for you. If so, I'd suggest you add the awk tag to your question.

You can simply define a palette with the wavelengths and the approximate colors, like here.
Then use the colorbox as partial background of the plot.
Script: (works with gnuplot>=4.6.0, March 2012)
### Planck black-body radiation with visible spectrum (approximate)
reset
c = 299792458 # m/s
h = 6.62607015e-34 # Js
k = 1.380649e-23 # J/K
Planck(x,T) = 2*h*c**2/x**5/(exp(h*c/(x*k*T))-1)
set palette defined (380 "black", 400 "dark-violet", 440 "blue", 490 '#00b0c0', \
530 "green", 560 "yellow", 620 "red", 780 "black")
set style fill solid 0.4
unset cblabel
unset cbtics
set colorbox horizontal user origin first 380, graph 0 size first 400, graph 1 back
set key noautotitles
set grid x,y front
set xrange[100:2000]
plot for [T=4000:7000:1000] Planck(x/1e9,T) w l ti sprintf("%g K",T) lw 2, \
(NaN) w p palette # plot nothing just to get the colorbox
### end of script
Result: (created with gnuplot 4.6.0)

Its a bit of a manual solution, but how about a bunch of vertical lines. Here is a start. This linewidth is good for a plot window of 1200,900
set arrow 1 from 0.4,0 to 0.4, 50 nohead lc rgb '#8B008B#' lw 3.5
set arrow 2 from 0.405,0 to 0.405, 50 nohead lc rgb '#7000aF#' lw 3.5
set arrow 3 from 0.41,0 to 0.41, 50 nohead lc rgb '#4800FF#' lw 3.5
set arrow 4 from 0.415,0 to 0.415, 50 nohead lc rgb '#3000FF#' lw 3.5
set arrow 5 from 0.42,0 to 0.42, 50 nohead lc rgb '#1800FF#' lw 3.5
set arrow 6 from 0.425,0 to 0.425, 50 nohead lc rgb '#0000FF#' lw 3.5
set arrow 7 from 0.43,0 to 0.43, 50 nohead lc rgb '#0018FF#' lw 3.5
set arrow 8 from 0.435,0 to 0.435, 50 nohead lc rgb '#0030FF#' lw 3.5
set arrow 9 from 0.44,0 to 0.44, 50 nohead lc rgb '#0048FF#' lw 3.5
set arrow 10 from 0.445,0 to 0.445, 50 nohead lc rgb '#0060FF#' lw 3.5
set arrow 11 from 0.45,0 to 0.45, 50 nohead lc rgb '#0078FF#' lw 3.5
set arrow 12 from 0.455,0 to 0.455, 50 nohead lc rgb '#0088FF#' lw 3.5
set arrow 13 from 0.46,0 to 0.46, 50 nohead lc rgb '#0090FF#' lw 3.5
set arrow 14 from 0.465,0 to 0.465, 50 nohead lc rgb '#00a0FF#' lw 3.5
set arrow 15 from 0.47,0 to 0.47, 50 nohead lc rgb '#00b8FF#' lw 3.5
set arrow 16 from 0.475,0 to 0.475, 50 nohead lc rgb '#00d0FF#' lw 3.5
set arrow 17 from 0.48,0 to 0.48, 50 nohead lc rgb '#00fe8FF#' lw 3.5
set arrow 18 from 0.485,0 to 0.485, 50 nohead lc rgb '#00FFFF#' lw 3.5
set arrow 19 from 0.49,0 to 0.49, 50 nohead lc rgb '#7CFFa0#' lw 3.5
set arrow 20 from 0.495,0 to 0.495, 50 nohead lc rgb '#7CFF80#' lw 3.5
set arrow 21 from 0.5,0 to 0.5, 50 nohead lc rgb '#7CFF60#' lw 3.5
set arrow 22 from 0.505,0 to 0.505, 50 nohead lc rgb '#7CFF40#' lw 3.5
set arrow 23 from 0.51,0 to 0.51, 50 nohead lc rgb '#7CFF20#' lw 3.5
set arrow 24 from 0.515,0 to 0.515, 50 nohead lc rgb '#7CFF10#' lw 3.5
set arrow 25 from 0.52,0 to 0.52, 50 nohead lc rgb '#7CFF00#' lw 3.5
set arrow 26 from 0.525,0 to 0.525, 50 nohead lc rgb '#80FF00#' lw 3.5
set arrow 27 from 0.53,0 to 0.53, 50 nohead lc rgb '#88FF00#' lw 3.5
set arrow 28 from 0.535,0 to 0.535, 50 nohead lc rgb '#90FF00#' lw 3.5
set arrow 29 from 0.54,0 to 0.54, 50 nohead lc rgb '#98FF00#' lw 3.5
set arrow 30 from 0.545,0 to 0.545, 50 nohead lc rgb '#a0FF00#' lw 3.5
set arrow 31 from 0.55,0 to 0.55, 50 nohead lc rgb '#a8FF00#' lw 3.5
set arrow 32 from 0.555,0 to 0.555, 50 nohead lc rgb '#b0FF00#' lw 3.5
set arrow 33 from 0.56,0 to 0.56, 50 nohead lc rgb '#c0FF00#' lw 3.5
set arrow 34 from 0.565,0 to 0.565, 50 nohead lc rgb '#d0FF00#' lw 3.5
set arrow 35 from 0.57,0 to 0.57, 50 nohead lc rgb '#e0FF00#' lw 3.5
set arrow 36 from 0.575,0 to 0.575, 50 nohead lc rgb '#f0FF00#' lw 3.5
set arrow 37 from 0.58,0 to 0.58, 50 nohead lc rgb '#FFFF00#' lw 3.5
set arrow 38 from 0.585,0 to 0.585, 50 nohead lc rgb '#FFcF00#' lw 3.5
set arrow 39 from 0.59,0 to 0.59, 50 nohead lc rgb '#FFA500#' lw 3.5
set arrow 40 from 0.595,0 to 0.595, 50 nohead lc rgb '#FFA500#' lw 3.5
set arrow 41 from 0.6,0 to 0.6, 50 nohead lc rgb '#FF9800#' lw 3.5
set arrow 42 from 0.605,0 to 0.605, 50 nohead lc rgb '#FF8000#' lw 3.5
set arrow 43 from 0.61,0 to 0.61, 50 nohead lc rgb '#FF7000#' lw 3.5
set arrow 44 from 0.615,0 to 0.615, 50 nohead lc rgb '#FF6000#' lw 3.5
set arrow 45 from 0.62,0 to 0.62, 50 nohead lc rgb '#FF5000#' lw 3.5
set arrow 46 from 0.625,0 to 0.625, 50 nohead lc rgb '#FF4000#' lw 3.5
set arrow 47 from 0.63,0 to 0.63, 50 nohead lc rgb '#FF3800#' lw 3.5
set arrow 48 from 0.635,0 to 0.635, 50 nohead lc rgb '#FF3000#' lw 3.5
set arrow 49 from 0.64,0 to 0.64, 50 nohead lc rgb '#FF2000#' lw 3.5
set arrow 50 from 0.645,0 to 0.645, 50 nohead lc rgb '#FF1000#' lw 3.5
set arrow 51 from 0.65,0 to 0.65, 50 nohead lc rgb '#FF0000#' lw 3.5
set arrow 52 from 0.655,0 to 0.655, 50 nohead lc rgb '#FF0000#' lw 3.5
set arrow 53 from 0.66,0 to 0.66, 50 nohead lc rgb '#f00000#' lw 3.5
set arrow 54 from 0.665,0 to 0.665, 50 nohead lc rgb '#e00000#' lw 3.5
set arrow 55 from 0.67,0 to 0.67, 50 nohead lc rgb '#d00000#' lw 3.5
set arrow 56 from 0.675,0 to 0.675, 50 nohead lc rgb '#c00000#' lw 3.5
set arrow 57 from 0.68,0 to 0.68, 50 nohead lc rgb '#b00000#' lw 3.5
set arrow 58 from 0.685,0 to 0.685, 50 nohead lc rgb '#a00000#' lw 3.5
set arrow 59 from 0.69,0 to 0.69, 50 nohead lc rgb '#900000#' lw 3.5
set arrow 60 from 0.695,0 to 0.695, 50 nohead lc rgb '#800000#' lw 3.5
set arrow 61 from 0.7,0 to 0.7, 50 nohead lc rgb '#700000#' lw 3.5

Related

gnuplot: How to create colored grid regions, not just colored grid lines?

I am using "gnuplot 5.2 patchlevel 2". I am trying to create background grid colored-columns or colored-areas like the picture below. So far, I am only able to color the grid-lines. But I want to color the grid areas. What is the best way? Here is my code:
set terminal svg
set output 'out.svg'
set key off
set xlabel 'X'
set ylabel 'Y'
set title 'Data'
set grid
set grid xtics lw 0.25 lc rgb "#ff0000" # line only, but I want to color the whole area
#unset grid
#set grid ytics lt 0 lw 1 lc rgb "#0000ff"
set xrange [0:4]
set yrange [0:100]
set tics scale 0.5
set xtics nomirror
set ytics nomirror
set style fill solid noborder
set linetype 1 lc rgb 'red' lw 0.35
set linetype 2 lc rgb '#009900'
set linetype 3 lc rgb 'black' lw 0.5
set boxwidth 0.5 relative
set style fill solid border lc rgb "black"
plot "data.txt" using 1:2:4:3:5:($5 < $2 ? 1 : 2) linecolor variable with candlesticks, \
"data.txt" using 1:6 with lines lt 3, \
"data.txt" using 1:5:7 with filledcurves fs transparent solid 0.3 lc rgb "blue"
And here is my sample data.txt file for the plotting:
1 10 30 5 20 23 29
2 25 45 10 30 34 37
3 30 50 20 25 47 53
You could use a dummy function like [x=0:16:1] '+' us (x/2):(100/(int(x)%4!=1)) with filledcurves x1.
Every fourth point generates a NaN and interrupts the curve.
$data <<EOD
1 10 30 5 20 23 29
2 25 45 10 30 34 37
3 30 50 20 25 47 53
5 10 30 5 20 23 29
7 25 45 10 30 34 37
8 30 50 20 25 47 53
EOD
set style fill solid noborder
set linetype 1 lc rgb 'red' lw 0.35
set linetype 2 lc rgb '#009900'
set linetype 3 lc rgb 'black' lw 0.5
set boxwidth 0.5 relative
set style fill solid border lc rgb "black"
plot sample [x=0:16:1] '+' us (x/2):(100/(int(x)%4!=1)) with filledcurves x1 fc rgb "#EEEEEE",\
$data using 1:2:4:3:5:($5 < $2 ? 1 : 2) linecolor variable with candlesticks, \
$data using 1:6 with lines lt 3, \
$data using 1:5:7 with filledcurves fs transparent solid 0.3 lc rgb "blue"
This function could be also assigned to the y2 axis with fixed y2range, which might be more handy for interactive plots with zooming.

Gnuplot set background color of data label

I want to set the background of data labels to white! The considered plot is a data plot of the following data (gnuDC.dat):
4 1570.96 1571
8 770.63 771
12 530.33 530
16 385.13 385
24 261.87 262
48 137.71 138
96 81.42 81
The plot command reads:
plot "gnuDC.dat" using 1:2 title "DC: GNU Fortran 4.7.2 + Open MPI 1.6.3" w p ls 1, \
"gnuDC.dat" using 1:2:3 with labels center offset 2.,0.7 font "Helvetica,14" tc ls 4 notitle, \
"gnuDC.dat" using 1:3 notitle smooth csplines ls 14
Which gives me:
It looks ok but think one could read the lables better when the would have an white background. Is there an easy way to add the white background for all labels at once?
Here is the whole print file:
set terminal postscript eps size 14cm,10cm enhanced color \
font 'Helvetica,18' linewidth 2
set output 'test.eps'
# Line style for axes
set style line 80 lt 0
set style line 80 lt rgb "#808080"
# Line style for grid
set style line 81 lt 3 # dashed
set style line 81 lt rgb "#808080" lw 0.5 # grey
set grid back linestyle 81
set border 3 back linestyle 80
set xtics nomirror
set ytics nomirror
set style line 100 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1.5
set style line 200 lc rgb '#a2142f' lt 1 lw 2 pt 7 ps 1.5
set pointintervalbox 0
set style line 1 lc rgb '#0072bd' lt 1 lw 1 pt 9 pi -10 ps 2
set style line 2 lc rgb '#77ac30' lt 1 lw 1 pt 7 pi -10 ps 2
set style line 3 lc rgb '#d95319' lt 1 lw 1 pt 1 pi -10 ps 2
set bmargin 4
set lmargin 5
set rmargin 4
unset title
set size 1,1
#set origin 0,0.27
set xlabel "number of cores, -"
set ylabel "Computational time, s"
set key top right
set key spacing 1.5
set key width -12
set yrange [0:1710]
plot "gnuDC.dat" using 1:2 title "DC: GNU Fortran 4.7.2 + Open MPI 1.6.3" w p ls 1, \
"gnuDC.dat" using 1:2:3 with labels center offset 2.,0.7 font "Helvetica,14" tc ls 4 notitle, \
"gnuDC.dat" using 1:3 notitle smooth csplines ls 14
With gnuplot version 5 there is a boxed option which does exactly this: give labels a background and, if you want, also a border. The style is controlled with set style textbox, e.g.
set style textbox opaque noborder
plot ... with labels boxed ...
Applied to your script (with some minor changes due to the changed dash handling since 5.0):
# Line style for axes
set style line 80 lt rgb "#808080"
# Line style for grid
set style line 81 dt 3 # dashed
set style line 81 lt rgb "#808080" lw 0.5 # grey
set grid back linestyle 81
set border 3 back linestyle 80
set tics nomirror
set linetype 1 lc rgb '#0072bd' pt 9 pi -10 ps 2 dt 3
set bmargin 4
set lmargin 5
set rmargin 4
set xlabel "number of cores, -"
set ylabel "Computational time, s"
set key top right
set key spacing 1.5
set key width -12
set yrange [0:1710]
set style textbox opaque noborder
plot "gnuDC.dat" using 1:2 title "DC: GNU Fortran 4.7.2 + Open MPI 1.6.3" w p lt 1, \
"gnuDC.dat" using 1:2:3 with labels boxed center offset 2.,0.7 font "Helvetica,10" tc ls 1 notitle, \
"gnuDC.dat" using 1:3 notitle smooth csplines lt 1
No, for versions 4.6 and earlier there isn't an easy way to achieve this.

I don't have color on my .eps figure using gnuplot

I want to trace some curves with gnuplot. I recently change my computer and now I have a MacOSX 10.9.5
In my other computer, colors appeared in my .eps figure, but with my Mac not anymore, and an error message appears : "ยท Times not found, using Courier." which is an other problem I think.
My gnuplot version is Version 5.0 patchlevel 0.
Here is my trace.p that I load with gnuplot with
$ gnuplot
gnuplot > load "trace.p"
set autoscale
unset logscale
unset label
set term postscript enhanced 'Times'
set output"E_Hxc_bath_exact_weak.eps"
set title "E^{bath,exact}_{Hxc}/U, E^{bath,weak}_{Hxc}/U functions of U/t with potential more or equal to second-order set to 0"
set xlabel "U/t"
set ylabel "E^{bath}_{Hxc}/U"
set xrange [0.7:10]
set yrange [-7:10]
set key left top
set style line 1 lt 1 lc rgb "red" lw 3
set style line 2 lt 2 lc rgb "red" lw 3
set style line 3 lt 3 lc rgb "red" lw 3
set style line 4 lt 1 lc rgb "green" lw 3
set style line 5 lt 2 lc rgb "green" lw 3
set style line 6 lt 3 lc rgb "green" lw 3
set style line 7 lt 1 lc rgb "black" lw 3
set style line 8 lt 2 lc rgb "black" lw 3
set style line 9 lt 1 lc rgb "blue" lw 3
set style line 10 lt 2 lc rgb "blue" lw 3
set style line 11 lt 3 lc rgb "blue" lw 3
set style line 12 lt 1 lc rgb "violet" lw 3
set style line 13 lt 2 lc rgb "violet" lw 3
plot (0.25 - (3/4)/(sqrt(1+64/(x**2)) + 2*sqrt(1+16/(x**2)))) with lines ls 7 title "exact", (0.25 - 3*(x**2)/64 + (x**3)/(2*16*16)) with lines ls 1 title "weak";
Some lines are certainly useless, but I don't know why the result is colorless, maybe I miss an application or something...
Try adding the color flag to the set term line in your script.
For the dashed lines which didn't appear, it is because in version 5.0, the command change, it is no more linetype but dashtype.
See the response of Gnuplot line types for more precision.

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)

Drawing axis-aligned bounding box (AABB) in gnuplot

I have plotted two vectors (3,3,2), (2,2,1) and want to add an AABB to the first one
to show that the second vector is within the bounds of the first.
Adding set object rectangle from screen 0,0 to screen 3,3 front is not working.
So how can I draw the AABB (or a cuboid with a=3, b=3 and c=2)?
Also how can I put the the scale of the y-axis on the left side.
Still open:
Limit scale of y-axis to left side, and scale of x-axis to bottom front
Add arrows to x-, y- and z-axis
Delete the second x- and y- axis so that there is just one of each left
Gnuscript resource_vec_aabb
set xyplane 0
set grid
set xrange [0:5]
set yrange [0:5]
set zrange [0:5]
splot 'resource_vec_aabb.dat' with vectors filled head lw 2
Data resource_vec_aabb.dat
# Gnuplot .dat file for vectors
0 0 0 3 3 2
0 0 0 2 2 1
UPDATE
set termoption dashed #Needs to be set to draw dashed lines
set border 19 #To set axis (1 + 2 + 16). See: set border help
set xyplane 0
set grid
unset key
my_range = 4
set xrange [0:my_range]
set yrange [0:my_range]
set zrange [0:my_range]
set arrow from graph 0,0,0 to graph 1.05,0,0 size screen 0.025,15,60 \
filled ls 1 linecolor rgb "black"
set arrow from graph 0,0,0 to graph 0,1.05,0 size screen 0.025,15,60 \
filled ls 1 linecolor rgb "black"
set arrow from graph 0,0,0 to graph 0,0,1.05 size screen 0.025,15,60 \
filled ls 1 linecolor rgb "black"
set xtic 1
set ytic 1
set ztic 1
# AABB for 3,3,2
set arrow from 3,0,0 to 3,3,0 nohead linetype 2 linecolor rgb "green"
set arrow from 3,3,0 to 3,3,2 nohead linetype 2 linecolor rgb "green"
set arrow from 0,3,0 to 3,3,0 nohead linetype 2 linecolor rgb "green"
set arrow from 0,3,2 to 3,3,2 nohead linetype 2 linecolor rgb "green"
set arrow from 0,3,0 to 0,3,2 nohead linetype 2 linecolor rgb "green"
set arrow from 0,0,2 to 0,3,2 nohead linetype 2 linecolor rgb "green"
set arrow from 0,0,2 to 3,0,2 nohead linetype 2 linecolor rgb "green"
set arrow from 3,0,0 to 3,0,2 nohead linetype 2 linecolor rgb "green"
set arrow from 3,0,2 to 3,3,2 nohead linetype 2 linecolor rgb "green"
# 2,2,1
set arrow from 2,0,0 to 2,2,0 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,2,0 to 2,2,0 nohead linetype 2 linecolor rgb "blue"
set arrow from 2,2,0 to 2,2,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,0,1 to 2,0,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,0,1 to 0,2,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,2,1 to 2,2,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 2,0,1 to 2,2,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 2,0,0 to 2,0,1 nohead linetype 2 linecolor rgb "blue"
set arrow from 0,2,0 to 0,2,1 nohead linetype 2 linecolor rgb "blue"
set view 40,44,1
splot 'resource_vec_aabb.dat' with vectors filled head lw 2
new output
Here is a suggestion to draw your bounding box with vectors and tic labels at the back y-axis. The bounding boxes are plotted instead of tediously drawn by arrows. There is still room for fine tuning.
Data: SO17363018.dat
# Gnuplot .dat file for vectors
0 0 0 3 3 2
0 0 0 2 2 1
Script: (works for gnuplot>= 4.6.0, March 2012)
### draw bounding box of vectors
reset
FILE = "SO17363018.dat"
set termoption dashed # required for gnuplot4.6 for dashed lines
set xlabel "x-axis" rotate parallel
set ylabel "y-axis" rotate parallel
set xyplane 0
set grid x,y
set xrange [0:5]
set yrange [0:5]
set border 19
set xtics 1 nomirror
set ytics axis
set for [i=1:3] arrow i from graph 0,0,0 to \
graph (1.12*(i==1)), (1.12*(i==2)), (1.12*(i==3)) \
size screen 0.025,15,60 filled ls 1 lc rgb "black"
x0(i,j,k) = ((i==0 ? 0 : i==1 ? k : j)*3+1)
y0(i,j,k) = ((i==0 ? j : i==1 ? 0 : k)*3+2)
z0(i,j,k) = ((i==0 ? k : i==1 ? j : 0)*3+3)
set key noautotitle
set view 61,49, 1.1
splot FILE u 1:2:3:($4-$1):($5-$2):($6-$3) w vec lc rgb "red" lw 2, \
for [c=0:1] for [i=0:2] for [j=0:1] for [k=0:1] FILE \
u (column(x0(i,j,k))):(column(y0(i,j,k))):(column(z0(i,j,k))): \
((i==0)*($4-$1)):((i==1)*($5-$2)):((i==2)*($6-$3)) \
every ::c::c w vec nohead ls 2 lw 1.5 lc c+2
### end of script
For current gnuplot versions 5.x, the script needs to be adapted slightly and you can be shorten the plot command a bit:
dashtype can be specified directly via dt 2, not via ls 2
one for loop less because in 4.6 lc variable doesn't seem to work together with ls 2.
splot FILE u 1:2:3:($4-$1):($5-$2):($6-$3) w vec lc rgb "red" lw 2, \
for [i=0:2] for [j=0:1] for [k=0:1] FILE \
u (column(x0(i,j,k))):(column(y0(i,j,k))):(column(z0(i,j,k))): \
((i==0)*($4-$1)):((i==1)*($5-$2)):((i==2)*($6-$3)):($0+1) \
w vec nohead lw 1.5 dt 2 lc var
Result: (created with gnuplot 4.6.0)

Resources