在 gnuplot 中的圆圈上剪切向量

Clip vectors on circle in gnuplot

我尝试在圆形区域中绘制一些矢量场。考虑以下 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(,,0,3.832)/50):(Ey(,,0,3.832)/50) w vectors
unset table
set samples 250
set isosamples 250
plot '++' u 1:2:(mag(,,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,1.841)/20):(Ey(,,1,1.841)/20) w vectors
unset table
set samples 250
set isosamples 250
plot '++' u 1:2:(mag(,,1,1.841)) w image notitle, \
     'tmp.dat' u 1:2:3:4 w vectors head filled lc black lw 1 notitle

unset multiplot

绘制了矢量场及其在直径为 d 的圆内的大小。结果是

左图(TE01)完全没问题,但右图(TE11)看起来很难看,因为有一些矢量被画在圆圈外面。我实际想要的结果是这个

我在黑色圆圈之外没有矢量。我怎样才能做到这一点? 我知道 gnuplot 中有 clip 函数,但这不允许指定用于裁剪的形状。

您可以尝试以下方法。定义您自己的剪辑功能,例如一个圆圈。 首先你需要检查一个数据点是否在你的圈子之外。 Clip(x,y) returns NaN 如果在外面, 0 如果在里面。 现在,当您绘制时,只需将 clip 函数的值添加到您的值即可。您的数据将被剪裁在一个圆圈内,因为某些 +0 保持不变,而某些 +NaN 将是 NaN 并且不会被绘制。如果您只为 x(矢量开始)和 x + delta x(矢量结束)执行此操作就足够了。

代码:

### 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*/sqrt(**2+**2)): \
         (Scaling*/sqrt(**2+**2)) : (sqrt(**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 (+Clip(,)):2:(+Clip(+,+)):4:5 w vec lc pal not

    CenterX = 1
    CenterY = 1
    plot $Data u (+Clip(,)):2:(+Clip(+,+)):4:5 w vec lc pal not

unset multiplot
### end of code

结果: