GnuPlot splot function with 2d points - gnuplot

I basically want to draw 2d color surface (or contour plot)
of rosenbrock function f(x,y) = (a-x)^2 + b * (y-x*x) ^2
and append some points (x,y) on this image.
Sample file with points looks as follows:
#x #y
15.00000 12.00000
8.00000 9.00000
The thing is, both graphs do not share the same coordinate system on output image:
coordinate systems do not overlap on each other
gnuplot code:
#!/usr/bin/env gnuplot
reset
set terminal png size 700,700
enhanced set output 'output.png'
set tmargin screen 1
set bmargin screen 0
set border 0 back
set size square
xr=20
yr=20
set xrange [-xr:xr]
set yrange [-yr:yr]
unset key #disablegraph name
unset colorbox
set surface
set multiplot
set view map
set cntrparam levels 10# contour tenderness
set style data pm3d
set pm3d
set contour
a=1 #rosenbrock parameter
b=1 #rosenbrock parameter
#set isosamples 50
splot (a-x) * (a-x) + b * (y-x*x) * (y-x*x) # 2d rosenbrock
unset view
unset pm3d
plot 'data.dat' pt 5, 'data.dat' using 1:2:($0+1) with labels offset 1 notitle

mixing 2d and surface plots with multiplot is usually a mess. I guess you probably don't need multiplot in this simple case. Maybe something like this is enough:
set size square
xr=20
yr=20
set xrange [-xr:xr]
set yrange [-yr:yr]
unset key
unset colorbox
set surface
set pm3d map
set contour
set cntrparam levels 10# contour tenderness
rosenbrock(x,y,a,b)= (a-x) * (a-x) + b * (y-x*x) * (y-x*x)
splot rosenbrock(x,y,1,1) w pm3d, 'data.dat' u 1:2:0 w p pt 5, 'data.dat' using 1:2:(1):($0+1) with labels offset 1,1 notitle

Related

Is there a mismatch between splot and arrow coordinates in gnuplot or am I missing something?

I'm trying to plot the following 3 intersecting planes:
x + 2y + 4z = 7
2x + 3y + 3z = 1
3x + 7y + 2z = -11
To add emphasis, I wanted to include some headless arrows along the intersection of each pair of planes and a small sphere to indicate the intersection point. But for some reason the planes themselves seem to be out of alignment with the axes. From the equations I can easily find the coordinate of the intersection point, find the coordinates of the edges of the lines that run along the intersection of each pair of planes and if needed find the parametric equation of the lines. But so far when I plot the lines as arrows from the edges of my plot or the intersection as a circle, the planes seem to be wrongly positioned within the coordinates. I can see that the circle or the lines are positioned correctly, but the planes themselves seem to have been shifted. What could be causing this? Am I entering the equations wrong? Maybe the shift of the xy plane moves things around? I think it must be something obvious I'm just not seeing. I find it quite puzzling.
reset
samps = 500
set samples samps,samps
set isosamples samps,samps
f(x,y) = 7/4 - x/4 - y/2
set table $Data01
splot f(x,y)
unset table
g(x,y) = 1/3 - 2/3*x - y
set table $Data02
splot g(x,y)
unset table
h(x,y) = -11/2 - 3/2*x - 7/2*y
set table $Data03
splot h(x,y)
unset table
Zmin = 1.0
Zmax = 3.5
set xrange [-1.2:0.5]
set yrange [-4:0]
set zrange [Zmin:Zmax]
set hidden3d
set xlabel 'x'
set ylabel 'y'
set zlabel 'z'
set xyplane at Zmin
unset xzeroaxis
unset yzeroaxis
unset zzeroaxis
set border 1023-128
set xtics out nomirror
set ytics out nomirror
set ztics out
#set xtics add ('' -4)
Frac(z) = (z - Zmin) / (Zmax - Zmin)
#MyPalette01
Red01(z) = 0
Green01(z) = 255*256
Blue01(z) = int(255*Frac(z))
MyPalette01(z) = Red01(z) + Green01(z) + Blue01(z)
#MyPalette02
Red02(z) = 255*256*256
Green02(z) = int(165*Frac(z))*256
Blue02(z) = 0
MyPalette02(z) = Red02(z) + Green02(z) + Blue02(z)
# MyPalette03
Red03(z) = int(-95*Frac(z)+255)*256*256
Green03(z) = int(32*Frac(z))*256
Blue03(z) = int(-15*Frac(z)+255)
MyPalette03(z) = Red03(z) + Green03(z) + Blue03(z)
#Red03(z) = int(255*Frac(z))*256*256
#Green03(z) = int(255*Frac(z))*256
#Blue03(z) = int(255*Frac(z))
set object circle at -1,-2,3 size 0.05 front
unset key
set pm3d
set pm3d lighting primary 0.5 specular 0.6
set pm3d ftriangles
set style fill transparent solid 0.75 noborder
set pm3d depthorder
unset colorbox
set view 68, 126
splot $Data01 u 1:2:3:(MyPalette01($3)) w l lc rgb var notitle, \
$Data02 u 1:2:3:(MyPalette02($3)) w l lc rgb var notitle, \
$Data03 u 1:2:3:(MyPalette03($3)) w l lc rgb var notitle
I found how to set multiple styles for each plane in here:
Gnuplot 5.2 splot: Multiple pm3d palette in one plot call
And this is how it looks:
Any Ideas?

Plotting two parallel charged plate configuration in Gnuplot

I want to plot a parallel plate capacitor setup with plates at x = -1 and x = +1 lying in the yz plane. I have to then show the potential varying in between them and the vector plot of electric field.
How can I generate the solid plates in 3D?
I am not sure if Gnuplot is the best tool for this, nevertheless an approximation could be perhaps achieved with parametric plotting, where the x-coordinate is fixed and y/z are directly mapped to the u/v parameters:
set terminal pngcairo rounded font ",16"
set xr [-4:4]
set yr [-4:4]
set zr [-4:4]
set palette defined ( 0 "black", 1 "#666666" )
set pm3d at s
unset surface
unset colorbox
set isosamples 100
unset key
set parametric
set ur [-2:2]
set vr [-2:2]
splot \
-1,u,v w l lc rgb '#333333', \
+1,u,v w l lc rgb '#333333'
#or set larger ur/vr and use, e.g.,
# -1,(u>-2&&u<2?u:1/0),(v>-2&&v<2?v:1/0) w l lc rgb '#333333', \
# +1,(u>-2&&u<2?u:1/0),(v>-2&&v<2?v:1/0) w l lc rgb '#333333'
This would give you:

Gnuplot: 3D Surface Outline

I am plotting a simple 3D surface in GNUPlot for the following function:
f(x,y)=x**2-y**2
This works fine. However I would like to only display the outline of the surface. Without the colors or grid lines along it. Is there a way to achieve this?
Here is an example of what I am looking to create:
Outline of the surface
Thank you for your help.
In this specific case you could also adjust the number of isolines drawn by gnuplot:
f(x,y) = x**2 - y**2
set xr [-10:10]
set yr [-10:10]
unset key
set isosamples 2,3
splot f(x,y)
I'm not aware of a general solution. In your special case I would consider just drawing each single line using parametric mode like in this script:
f(x,y) = x**2 - y**2
set parametric
set urange [-10:10]
set vrange [-10:10]
set nokey
#set border 0 # uncomment to remove the axes
#unset xtics
#unset ytics
#unset ztics
set arrow 1 from 0,0,0 to 0,0,100
set arrow 1 head lw 2
splot u,-10,f( u,-10) lc 0, \
u, 10,f( u, 10) lc 0, \
-10, v,f(-10, v) lc 0, \
10, v,f( 10, v) lc 0, \
u, 0,f( u, 0) lc 0
This is the result:

Gnuplot Surface plot combined with heatmap on the same plot in polar coordinates

I'd like to plot 3D surface plot combined with heatmap plot of the same function (from file data) at the bottom (like in the chapter "Combining surfaces with images" in Gnuplot Cookbook).
The wanted picture looks like this and this.
Contour plot is plotted with the script.
Is it possible to draw two graphs (contour and 3D) in the polar coordinates in one plot via Gnuplot?
Output is nothing, so there is an error in the code:
reset
set terminal pngcairo size 800,800 enhanced font 'Verdana,20'
set output '3d-polar_AbsPsiSquared.png'
set lmargin at screen 0.05
set rmargin at screen 0.85
set bmargin at screen 0.1
set tmargin at screen 0.9
set pm3d
unset key
set multiplot
stats '_FullWV.dat' using ($3*$3+$4*$4)
max(a,b) = (a>b) ? a : b
round2(x) = x - floor(x) < 0.5 ? floor(x) : ceil(x)
round(x, n) = round2(x*10**n)*10.0**(-n)
Z_MAX = max(-STATS_min, STATS_max)
Z_MAX = round(Z_MAX,1)
set parametric
set grid xtics ytics
set angles degree
#set zrange[-1,1]
#set cbrange [-1:1]
set autoscale fix
#set autoscale xfix
#set autoscale yfix
set autoscale zfix
#set autoscale
set cbrange [-Z_MAX : Z_MAX]
set palette model RGB defined ( 0"#6495ED", 1"blue", 2"#00008B",3"white",\
4"green",5"yellow",6"red")
#splot '_FullWV.dat' u ($2*cos($1)):($2*sin($1)):($3*$3+$4*$4)
set size ratio -1
set hidden front
set xyplane at -1
splot '_FullWV.dat' u (abs(($3*$3+$4*$4))<1e-3 ? NaN : $2*cos($1)):($2*sin($1)):($3*$3+$4*$4) with pm3d at b, '_FullWV.dat' u (abs(($3*$3+$4*$4))<1e-3 ? NaN : $2*cos($1)):($2*sin($1)):($3*$3+$4*$4) with pm3d
unset multiplot
Seems, the solution is depthorder option of pm3d, so the right script is:
reset
set terminal postscript eps size 5,6 enhanced color font 'Helvetica,20' linewidth 2
set output "3d.eps"
set tics scale 1.25
set xlabel '{/Helvetica-Oblique x, a.u.}'
set ylabel '{/Helvetica-Oblique y, a.u.}'
set zlabel '{/Helvetica-Oblique E, a.u.}'
set pm3d depthorder
unset key
set multiplot
unset colorbox
stats '_FullWV.dat' using ($3*$3+$4*$4)
max(a,b) = (a>b) ? a : b
round2(x) = x - floor(x) < 0.5 ? floor(x) : ceil(x)
round(x, n) = round2(x*10**n)*10.0**(-n)
Z_MAX = max(-STATS_min, STATS_max)
Z_MAX = round(Z_MAX,1)
set parametric
set grid xtics ytics ztics
set mxtics 2
set mytics 2
set tics out
set decimalsign ","
set format x "%.1f"
set format y "%.1f"
set format z "%.0f"
set angles degree
set view 61,33
set ztics floor(Z_MAX/3)
set autoscale fix
set autoscale zfix
set cbrange [0 : Z_MAX]
set palette model RGB defined ( 0"#168de2", 1"#0201db", 2"#00008B", 3"#00dcf5",\
4"#09df67",5"yellow",6"red")
set size ratio 1
set hidden3d front
set xyplane at -10
splot '_FullWV.dat' u (abs(($3*$3+$4*$4))<1e-3 ? NaN : $2*cos($1)):($2*sin($1)):($3*$3+$4*$4) with pm3d at b, '_FullWV.dat' u (abs(($3*$3+$4*$4))<1e-3 ? NaN : $2*cos($1)):($2*sin($1)):($3*$3+$4*$4) with pm3d
unset multiplot

Splot (contour, view map) and plot on same graph

I am trying to plot two overlaying graphs.
The first is a simple x-y points plot:
plot myfile u 1:2
The second is a contour plot at level 0, which I can plot with the
following commands:
set contour
unset surface
set view map
set cntrparam levels discrete 0
splot a0 + a1*x + a2*y + a3*x**2 + a4*x*y + a5*y**2 + a6*x**3 + a7*x**2*y + a8*x*y**2 + a9*y**3
a0-a9 are constants
Both use the same xrange and yrange.
How can I plot them both in the same graph?
This is a somewhat tricky one. You can write the contours to a file using
untested
set table 'datafile'
set contour
#contour options
splot ... with contours
unset table
Then you can plot that data with lines:
set term ...
set output ...
set view map
splot myfile u 1:2:(0.0), 'datafile' u 1:2:3 w lines

Resources