Plotting multiple pm3d surfaces each having its own palettes in gnuplot - gnuplot

What I basically want to do is basically the same as the solution to this question
Gnuplot 5.2 splot: Multiple pm3d palette in one plot call
, but which works with pm3d. If you read the comment to that answer, the answerer said that the solution does not work if he used pm3d. Also, would it be possible to define the palette in a simpler manner, such as set palette defined ()?

The development branch of gnuplot supports multiple named palettes. The method shown here, however, works on earlier versions of gnuplot also. It uses the fill style to provide a color (rather than the pm3d palette), and shows how to define the fill colors so that they mimic set palette defined(). This demo constructs only one mapping, but you could define several mappings each with its own array of colors and mapping function to use them.
This demo is extracted from the full demo for named palettes in the development branch. If you are interested you can find the full demo here:
Version 5.5 named palette demo
#
# Demonstrate construction and use of a separate palette
# Ethan A Merritt - May 2020
#
# Method 1:
# The first method works also in 5.2 but requires "lc rgb variable"
# rather than the more natural "fillcolor rgb variable".
# "set pm3d interpolate" breaks the color mapping of this method
#
# This creates a palette equivalent to
# set palette defined (0 "dark-blue", 1 "white")
#
array blues[256]
do for [i=1:256] {
blues[i] = int( (0x7f + (i-1)/(255.) * 0xffff80) );
}
#
# This is the equivalent of
# set cbrange [-1:1]
blues_min = -1
blues_max = 1
#
# This function maps z onto a palette color
#
blues(z) = (z <= blues_min) ? blues[1] \
: (z >= blues_max) ? blues[256] \
: blues[ floor(255. * (z-blues_min)/(blues_max-blues_min)) + 1]
foo(x,y) = sin(x*y)
set samples 41
set isosamples 41
unset colorbox
set cbrange [-1:1]
set xrange [0:5]; set urange [0:5]
set yrange [0:5]; set vrange [0:5]
set title "Use hand-constructed 'blues' palette via rgb variable"
splot '++' using 1:2:(foo($1,$2)):(blues(foo($1,$2))) with pm3d fillcolor rgb variable \
title "pm3d using 1:2:3:4 with pm3d fillcolor rgb variable"

Maybe you could define the palette differently with set palette defined but then you probably would have to combine your 3 palettes into 1 palette and you would lose "color resolution", as far as I know a palette has 256 color steps. To be honest, I haven't thought about this in detail.
I checked again the code you referenced... apparently an additional line will do the "trick". Then you can plot with pm3d.
set pm3d depthorder
Code: (slightly modified code from here: https://stackoverflow.com/a/57501649/7295599)
### multiple "palettes" within one splot command
reset session
set samples 101,101
set isosamples 101,101
f(x,y) = sin(1.3*x)*cos(0.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)
set table $Data01
splot f(x,y)
unset table
g(x,y) = y
set table $Data02
splot g(x,y)
unset table
h(x,y) = 0.5*x
set table $Data03
splot h(x,y)
unset table
Zmin = -3
Zmax= 3
set xrange[-5:5]
set yrange[-5:5]
set zrange[Zmin:Zmax]
set hidden3d
set angle degree
Frac(z) = (z-Zmin)/(Zmax-Zmin)
# MyPalette01
Red01(z) = 65536 * ( Frac(z) > 0.75 ? 255 : int(255*abs(2*Frac(z)-0.5)))
Green01(z) = int(255*sin(180*Frac(z)))*256
Blue01(z) = int(255*cos(90*Frac(z)))
MyPalette01(z) = Red01(z) + Green01(z) + Blue01(z)
# MyPalette02
Red02(z) = 65536 * int(255*Frac(z))
Green02(z) = 256 * (Frac(z) > 0.333 ? 255 : int(255*Frac(z)*3))
Blue02(z) = (Frac(z) > 0.5 ? 255 : int(255*Frac(z)*2))
MyPalette02(z) = Red02(z) + Green02(z) + Blue02(z)
# MyPalette03
Red03(z) = 65536 * (Frac(z) > 0.5 ? 255 : int(255*Frac(z)*2))
Green03(z) = 256 * (Frac(z) > 0.333 ? 255 : int(255*Frac(z)*3))
Blue03(z) = int(255*Frac(z))
MyPalette03(z) = Red03(z) + Green03(z) + Blue03(z)
set pm3d depthorder
unset colorbox
set view 44,316
splot $Data01 u 1:2:3:(MyPalette01($3)) w pm3d lc rgb var notitle, \
$Data02 u 1:2:3:(MyPalette02($3)) w pm3d lc rgb var notitle, \
$Data03 u 1:2:3:(MyPalette03($3)) w pm3d lc rgb var notitle
### end of code
Result:

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?

Clip vectors on circle in gnuplot

I try to draw some vector fields in a circular region. Consider the following MWE
unset grid
unset tics
unset colorbox
unset border
set size square
besselj(n, x) = n > 1 ? 2*(n-1)/x*besselj(n-1,x) - besselj(n-2,x) : (n == 1 ? besj1(x) : besj0(x))
dbesselj(n, x) = n/x*besselj(n,x) - besselj(n+1,x)
rho(x,y) = sqrt(x**2+y**2)
phi(x,y) = atan2(y,x)
d = 1.0
l = 1.0
z = l/2
q = 1
set xrange [-d/2*1.1:d/2*1.1]
set yrange [-d/2*1.1:d/2*1.1]
Erho(x,y,n,ynp) = (-1/rho(x,y)) * besselj(n, (ynp*2/d)*rho(x,y)) * (-n*sin(n*phi(x,y))) * sin(q*pi*z/l)
Ephi(x,y,n,ynp) = (ynp*2/d) * dbesselj(n, (ynp*2/d)*rho(x,y)) * (cos(n*phi(x,y))) * sin(q*pi*z/l)
Ex(x,y,n,ynp) = rho(x,y) > d/2 ? NaN : cos(phi(x,y))*Erho(x,y,n,ynp) - sin(phi(x,y))*Ephi(x,y,n,ynp)
Ey(x,y,n,ynp) = rho(x,y) > d/2 ? NaN : sin(phi(x,y))*Erho(x,y,n,ynp) + cos(phi(x,y))*Ephi(x,y,n,ynp)
mag(x,y,n,ynp) = sqrt(Ex(x,y,n,ynp)**2 + Ey(x,y,n,ynp)**2)
set object circle at 0,0 size 0.5 fc black lw 3 front
set multiplot layout 1,2
set title 'TE_{01}'
set table 'tmp.dat'
set samples 16
set isosamples 16
plot '++' u 1:2:(Ex($1,$2,0,3.832)/50):(Ey($1,$2,0,3.832)/50) w vectors
unset table
set samples 250
set isosamples 250
plot '++' u 1:2:(mag($1,$2,0,3.832)) w image notitle, \
'tmp.dat' u 1:2:3:4 w vectors head filled lc black lw 1 notitle
set title 'TE_{11}'
set table 'tmp.dat'
set samples 16
set isosamples 16
plot '++' u 1:2:(Ex($1,$2,1,1.841)/20):(Ey($1,$2,1,1.841)/20) w vectors
unset table
set samples 250
set isosamples 250
plot '++' u 1:2:(mag($1,$2,1,1.841)) w image notitle, \
'tmp.dat' u 1:2:3:4 w vectors head filled lc black lw 1 notitle
unset multiplot
which plots the vector field as well as its magnitude inside the circle with diameter d. The result from this is
which is totally okay for the left image (TE01), but the right image (TE11) looks ugly because there are some vectors which are drawn outside the circle. My actually desired result is this
where I have no vectors outside of the black circle. How can I achieve that?
I know there is the clip function in gnuplot, but this does not allow to specify the shape to be used for clipping.
Here is what you can try. Define your own clip function, e.g. a circle.
First you need to check whether a data point is outside of your circle or not.
Clip(x,y) returns NaN if it is outside and 0 if it is inside.
Now, when you plot simply add the value of the clip function to your value. Your data will be clipped within a circle because something +0 remains unchanged and something +NaN will be NaN and will not be plotted. It is sufficient if you do this just for x (vector start) and x + delta x (vector end).
Code:
### clip function in circle form
reset session
set size square
# create some test data
set samples 25
Scaling = 0.5
set table $Data
plot [-5:5] '++' u 1:2:(Scaling*$1/sqrt($1**2+$2**2)): \
(Scaling*$2/sqrt($1**2+$2**2)) : (sqrt($1**2+$2**2)) with table
unset table
set palette rgb 33,13,10
CenterX = 0
CenterY = 0
Radius = 3.5
Clip(x,y) = sqrt((x-CenterX)**2 + (y-CenterY)**2) > Radius ? NaN : 0
set xrange[-6:6]
set yrange[-6:6]
set multiplot layout 1,3
plot $Data u 1:2:3:4:5 w vec lc pal not
plot $Data u ($1+Clip($1,$2)):2:($3+Clip($1+$3,$2+$4)):4:5 w vec lc pal not
CenterX = 1
CenterY = 1
plot $Data u ($1+Clip($1,$2)):2:($3+Clip($1+$3,$2+$4)):4:5 w vec lc pal not
unset multiplot
### end of code
Result:

Gnuplot 5.2 splot: Multiple pm3d palette in one plot call

I wonder, can I used two different pm3d palettes in splot? I use Gnuplot 5.2 and
want to show two 3D surfaces, like ocean and land with different color palettes. Can I define this in some way like:
splot "file" u 1:2:3 with pm3d palette 1, "file" 1:2:4 with pm3d palette 2
The data in column 1 and 2 are integers that extend from 0 to 100, they define a grid. The boundaries may be different for different maps. The z axis values are real numbers representing relative changes on logarithmic scale and extend from -10 to 10.
The following might be solution for you. I do not see how to change the palette within a splot command. So, the basic idea of the workaround is to set the linecolor via formulae which can be set differently for each splot-(sub)command. I hope you can adapt the example below to your needs.
Also check help rgbformulae and type show palette rgbformulae which will show you the formulae behind the palettes.
Code:
### multiple "palettes" within one splot command
reset session
set samples 101,101
set isosamples 101,101
f(x,y) = sin(1.3*x)*cos(0.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)
set table $Data01
splot f(x,y)
unset table
g(x,y) = y
set table $Data02
splot g(x,y)
unset table
h(x,y) = 0.5*x
set table $Data03
splot h(x,y)
unset table
Zmin = -3
Zmax= 3
set xrange[-5:5]
set yrange[-5:5]
set zrange[Zmin:Zmax]
set hidden3d
set angle degree
Frac(z) = (z-Zmin)/(Zmax-Zmin)
# MyPalette01
Red01(z) = 65536 * ( Frac(z) > 0.75 ? 255 : int(255*abs(2*Frac(z)-0.5)))
Green01(z) = int(255*sin(180*Frac(z)))*256
Blue01(z) = int(255*cos(90*Frac(z)))
MyPalette01(z) = Red01(z) + Green01(z) + Blue01(z)
# MyPalette02
Red02(z) = 65536 * int(255*Frac(z))
Green02(z) = 256 * (Frac(z) > 0.333 ? 255 : int(255*Frac(z)*3))
Blue02(z) = (Frac(z) > 0.5 ? 255 : int(255*Frac(z)*2))
MyPalette02(z) = Red02(z) + Green02(z) + Blue02(z)
# MyPalette03
Red03(z) = 65536 * (Frac(z) > 0.5 ? 255 : int(255*Frac(z)*2))
Green03(z) = 256 * (Frac(z) > 0.333 ? 255 : int(255*Frac(z)*3))
Blue03(z) = int(255*Frac(z))
MyPalette03(z) = Red03(z) + Green03(z) + Blue03(z)
set view 44,316
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
### 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

Resources