How to plot a parameterized curve with gnuplot? - 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:

Related

Gnuplot 5: color gradient shading between curves

This was created with Matplotlib. Is it possible to make the same type of shading in Gnuplot 5?
I'm not aware that gnuplot has a gradient fill option, but I could be wrong.
The following is a bit of an "ugly" workaround. You basically create 3 plots on top of each other. You might want to adjust the palette to get the desired colors and a smooth transition.
a dummy plot to get the palette as background (i.e. the colorbox as large as the graph)
cover the part above y>f(x) and y>0 to x2-axis as well as below y<f(x) and y<0 to x1-axis.
plot again f(x) to see f(x) and the axes tics again
Edit:
The earlier version of the code used multiplot. It's not necessary, just use set colorbox back. But then set xzeroaxis ls -1 is not visible anymore, add plot 0 w l ls -1 instead.
Code:
### filled curve with gradient
reset session
f(x) = sin(x)/(1+x)
fabove(x) = f(x)<0 ? 0 : f(x)
fbelow(x) = f(x)>0 ? 0 : f(x)
set samples 200
set palette defined (0 "white", 1 "red", 2 "black")
set colorbox back user origin graph 0, graph 0 size graph 1, graph 1
unset cbtics
set xrange[0:15]
set xzeroaxis ls -1
set yrange[-0.2:0.5]
plot fabove(x) w filledcurves x2 fc rgb "white" not, \
fbelow(x) w filledcurves x1 fc rgb "white" not, \
f(x) w l lw 2 lc rgb "black", \
NaN palette, \
0 w l ls -1
### end of code
Result:

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:

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 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

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