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

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

Related

How to plot a parameterized curve with gnuplot?

I would like to plot a function f: R -> R^2 like f(t) = (cos(t), sin(t)), but I don't see how to do it.
f(t) = (cos(t), sin(t))
plot f(x) # doesn't work
splot f(x) # doesn't work either and is probably not what I want
Is there a way to plot such a function in gnuplot?
Please check help parametric, help special-filenames, help sampling and this: http://gnuplot.sourceforge.net/demo_5.4/param.html.
Here are two approaches:
Script:
### parametric curves
reset session
fx(t) = cos(t)
fy(t) = sin(t)
set size ratio -1
set xrange[-1.1:1.1]
set yrange[-1.1:1.1]
set multiplot layout 1,2
set parametric
plot [t=0:2*pi] fx(t), fy(t) w l lc "red"
unset parametric
plot sample [t=0:2*pi] '+' u (fx(t)):(fy(t)) w l lc "blue"
unset multiplot
### end of script
Result:

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?

Is there any way to use gnuplot to get a slice of plot from gnuplot 4D-plot?

I have a file with four columns. I have plotted 4D-plot using gnuplot tool as follows.
splot 'test.dat' u 1:2:3:($4<200.0?$4/4.184:1/0) w pm3d
Now I want to see a piece of the plot whose X-axis is some constant value. Let's say when the first column is 0.3, I want to see a 3D plot constructed out of 2,3,4 columns.
You don't show your data, so I assumed something.
Similar as you determine your color with the ternary operator you can "filter" a slice with constant x+dx.
Code:
### slice from 4D data
reset session
# create some test data
f(x,y) = x**2 + y**2
c(x,y) = x + y
set print $Data
do for [i=-10:10] {
do for [j=-10:10] {
print sprintf("%.3f %.3f %.3f %.3f", i, j, f(i,j), c(i,j))
}
print ""
}
set print
set xrange [-10:10]
set yrange [-10:10]
set zrange [0:200]
set cbrange [-20:20]
SliceX = 5
dx = 1
set multiplot layout 1,2
splot $Data u 1:2:3:4 w pm3d notitle
splot $Data u ($1>=SliceX && $1<=SliceX+dx?$1:NaN):2:3:4 w pm3d notitle
unset multiplot
### end of code
Result:

GnuPlot splot function with 2d points

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

gnuplot xtics disapper when using logscale

I'm pretty new to gnuplot, so I'm thankful for every advice.
Right now, I am trying to plot some data using the logscale command. But I don't know why all the xtics disappear when I use the logscale. This is the script I use:
#creates a plot of all the four different loops with a logscale. Fits the functions as well and saves the fitting data
#in a file named fitting.dat
set size 1,1
# set logscale
set logscale y 10
set logscale x 10
#set xlabel and y label
set xlabel "Dimension of Matrix"
set ylabel "time [s]"
#scale plot
set xrange [450:850]
set yrange[0.01:5]
#nothing displayed from fitting
set fit quiet
#position of legend
set key top right
set key horizontal
# guessing the parameters, the fit will be better and we know that the exponent should be \approx 3
b=3
d=3
f=3
h=3
#Define all th four different data fitting functions, asuming f(x) ~ a*x^b
f(x)= a*x**b
g(x)=c*x**d
h(x)=e*x**f
j(x)=g*x**h
#fit the different functions
fit f(x) 'matmul.txt' using 1:2 via a,b
fit g(x) 'matmul.txt' using 1:3 via c,d
fit h(x) 'matmul.txt' using 1:4 via e,f
fit j(x) 'matmul.txt' using 1:5 via g,h
# save the fitting parameters in an extra file
set print 'fitting.dat'
print 'function'
print a,'*x', '**', b , ' rows'
print c,'*x', '**', d , ' cols'
print e,'*x', '**', f , ' intrinsic function'
print g,'*x', '**', h , ' lapack routine'
# plot everything
plot "matmul.txt" u 1:2 t "rows" ,\
"matmul.txt" u 1:3 t "cols" ,\
"matmul.txt" u 1:4 t "intrinsic" ,\
"matmul.txt" u 1:5 t "lapack" ,\
f(x) t sprintf("row:%.2e*x^(%.2f)", a,b),\
g(x) t sprintf("col:%.2e*x^(%.2f)",c,d),\
h(x) t sprintf("int:%.2e*x^(%.2f)",e,f),\
j(x) t sprintf("lap:%.2e*x^(%.2f)",g,h)
#choose output format
set terminal png
set output "time.png"
replot
#now, non-logarithmic plot
#unset logscale
set yrange[0.01:1]
unset logscale
#plot again
plot "matmul.txt" u 1:2 t "rows" ,\
"matmul.txt" u 1:3 t "cols" ,\
"matmul.txt" u 1:4 t "intrinsic" ,\
"matmul.txt" u 1:5 t "lapack" ,\
f(x) t sprintf("col:%.2e*x^(%.2f)", a,b),\
g(x) t sprintf("row:%.2e*x^(%.2f)",c,d),\
h(x) t sprintf("int:%.2e*x^(%.2f)",e,f),\
j(x) t sprintf("lap%.2e*x^(%.2f)",g,h)
My Input file 'matmul.txt' looks like this:
#Dim rows cols intrinsic lapack
500 0.1320E+00 0.1040E+00 0.6800E-01 0.2000E-01
520 0.1400E+00 0.1320E+00 0.5600E-01 0.2000E-01
540 0.1480E+00 0.1400E+00 0.6000E-01 0.3200E-01
560 0.1680E+00 0.1480E+00 0.7200E-01 0.2400E-01
580 0.1800E+00 0.1680E+00 0.6800E-01 0.3200E-01
600 0.1920E+00 0.1960E+00 0.7200E-01 0.3600E-01
620 0.2080E+00 0.2040E+00 0.9600E-01 0.2000E-01
640 0.4000E+00 0.3520E+00 0.8400E-01 0.3200E-01
...
Now, If I run this file, I obtain the following output plot
I don't know why, but the range of the yscale is not correct and the xtics are not displayed. If I plot it without 'logscale', the plot is exactly what I want. Why doesn't this work?
Tics in logarithmic plots are not separated by a constant summand as in 1, 2, 3, ..., they are separated by a constant factor as in 1, 10, 100, ...
This means in your case for the y-axis: You have given the range [0.01:5], leading to tics at 0.01, 0.1, 1 as it is seen in the picture. Above 1, you have minor tics at 2, 3, 4, and 5. 5 is the upper boundary of the graph as specified in the range. To also have a label at this tic, just add it with:
set ytics add (5)
or change the yrange to one of
set yrange [0.01:1]
set yrange [0.01:10]
For your xtics: Labels would be at 1, 10, 100, 1000, ... But your range is from 450 to 850: no labeled xtic inside.
Again, you can set them manually:
set xtics (450, 550, 650, 750, 850)
Your x-axis spans less than a decade and the default major tic frequency is a decade. If you want labeled tics within this range use set xtics (400,500,600,700,800) or whatever you want.
This is all in the documentation, just search for "logscale"

Resources