首页 > 解决方案 > 在 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($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

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

上述代码的输出

这对于左图(TE01)来说完全没问题,但右图(TE11)看起来很难看,因为有一些向量被绘制在圆外。我真正想要的结果是这个

期望的结果

我在黑色圆圈之外没有向量。我怎样才能做到这一点?我知道clipgnuplot 中有这个功能,但这不允许指定用于剪切的形状。

标签: gnuplot

解决方案


这是您可以尝试的。定义您自己的剪辑函数,例如一个圆圈。首先,您需要检查数据点是否在您的圈子之外。 如果它在外面,如果它在里面,则Clip(x,y)返回。现在,当您绘图时,只需将剪辑函数的值添加到您的值中。您的数据将被剪裁在一个圆圈内,因为有些东西保持不变,有些东西会被绘制出来,也不会被绘制出来。如果您只为(vector start) 和(vector end) 这样做就足够了。NaN0+0+NaNNaNxx + 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*$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

结果: 在此处输入图像描述


推荐阅读