我基本上想要用gnuplot完成this (第一张图)。我没有找到任何与此完全相同的东西。我可以做一个很好的heaviside没有在两行的结尾和开始的小圆圈,但我似乎不能得到它与小圆圈。实际上,第二张图也很好知道。第三个也是,但我不贪心。
发布于 2020-11-19 22:52:39
只是为了记录和完整性。尽管您可以定义一个函数
H(x) = x<0 ? 0 : 1如果你绘制了
plot H(x) w l这条线将在零点连续,当然也不会有点。因此,另一个只有两列x,y和变量pointtype的建议如下所示。
代码:
### Heaviside function
reset session
$Heaviside <<EOD
-2 0
0 0
0 0.5
0 1
2 1
EOD
set yrange [-1:2]
set ytics 1
unset key
set multiplot layout 3,1
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:($0==1||$0==3?$2:NaN):($0==3?7:6) w p pt var lc 0
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:($0==1||$0==2||$0==3?$2:NaN):($0==2?7:6) w p pt var lc 0
set xrange [0:4]
a = 3
plot $Heaviside u ($1+a):2 w l lc 0, \
'' u ($1+a):($0==1||$0==3?$2:NaN):($0==3?7:6) w p pt var lc 0
unset multiplot
### end of code结果:

添加:
一个带有更短且不那么令人困惑的plot命令的变体,但使用了4列和变量pointtype。这将产生与上面相同的结果。
代码:
### Heaviside function
reset session
$Heaviside <<EOD
-2 0 NaN NaN
0 0 6 6
0 0.5 NaN 7
0 1 7 6
2 1 NaN NaN
EOD
set yrange [-1:2]
set ytics 1
unset key
set multiplot layout 3,1
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:2:3 w p pt var lc 0
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:2:4 w p pt var lc 0
set xrange [0:4]
a = 3
plot $Heaviside u ($1+a):2 w l lc 0, \
'' u ($1+a):2:3 w p pt var lc 0
unset multiplot
### end of code 添加2:
为了最终确定答案,这里提供了一种绘制包含Heaviside函数的函数的方法。它使用当前的x范围,而不是从具有固定x值的数据块(如上面的两个示例)绘制。注意,例如,语法plot '+' u 1:(sin($1))与plot sin(x)基本相同。
显然,通过lc rgb -1设置线条颜色不会绘制线条,这里可以使用线条来中断线条。您可能想要增加样本,例如set samples 300,以避免点和函数之间的间隙。
代码:
### plotting Heaviside function and functions containing Heaviside function
# including line interruption and inclusion/exclusion points
reset session
Heaviside(x,a) = x<a ? 0 : 1 # definition of Heaviside function
array Hpoints[2] = [6,7] # array for plotting "Heaviside points"
Hcolor(x) = (x0=x1, x1=x, x0<a && x1>=a ? -1 : 0xff0000) # set color -1 for line interruption
dx(n) = 1e-3*(2*n-1) # small dx to get y-value of points close to break
f(x,a) = 50/(x**2+2)*cos(4*x) * Heaviside(x,a)
unset key
set multiplot layout 2,1
a = 2.0
set yrange[-1:2]
plot x1=NaN '+' u 1:(Heaviside(x,a)):(Hcolor(x)) w l lc rgb var, \
Hpoints u (a):(Heaviside($1,a)):(Hpoints[$0+1]) w p pt var lc rgb Hcolor(NaN)
a= 0
set samples 300
set yrange[-25:35]
plot x1=NaN '+' u 1:(f(x,a)):(Hcolor(x)) w l lc rgb var, \
Hpoints u (a):(f(a+dx($0),a)):(Hpoints[$0+1]) w p pt var lc rgb Hcolor(NaN)
unset multiplot
### end of code结果:

发布于 2018-04-16 06:53:36
我创建了以下数据文件(请注意这两行空行):
-2 0 0 1
0 0 2 1
0 0 0 1并运行以下gnuplot命令:
set yrange [-2:2]
plot "file" using 1:2 with lines,\
"" using 3:4 with lines, \
"" index 1 using 1:2 with points pointtype 6, \
"" index 1 using 3:4 with points pointtype 7把颜色改成你喜欢的颜色。
https://stackoverflow.com/questions/49847539
复制相似问题