在Gnuplot中,如何填充数据曲线下的几个区域?

5
我有一组定义曲线的点“data”,我想用贝塞尔平滑绘制它。因此,我想在某些x值对之间填充该曲线下方的区域。如果我只有一个x值对,那就不那么难,因为我可以定义一组新的数据并用filledcu绘制它。例如: example of what I want to do 问题是我想在同一图中多次执行此操作。
编辑:最小工作示例:
#!/usr/bin/gnuplot
set terminal wxt enhanced font 'Verdana,12'

set style fill transparent solid 0.35 noborder
plot 'data' using 1:2 smooth sbezier with lines ls 1
pause -1

数据的结构如下:

x_point y_point

我意识到我的问题是实际上我连一条曲线都无法填满,因为斜率在那里几乎是恒定的,所以看起来曲线已经被填满了。


你能否发布一下你用来制作那个示例的脚本(或其最小版本)?这可能只需要进行简单修改就可以得到你想要的结果。 - andyras
1个回答

14

要填充曲线下方的部分,您必须使用filledcurves样式。使用 x1 选项可以填充曲线和 x 轴之间的部分。

如果您只想填充曲线的某些部分,则必须过滤您的数据,即如果x值在所需范围之外则将其赋值为1/0(无效数据点),否则从数据文件中获取正确的值。最后,您绘制曲线本身:

set style fill transparent solid 0.35 noborder
filter(x,min,max) = (x > min && x < max) ? x : 1/0
plot 'data' using (filter($1, -1, -0.5)):2 with filledcurves x1 lt 1 notitle,\
     ''  using (filter($1, 0.2, 0.8)):2 with filledcurves x1 lt 1 notitle,\
     ''  using 1:2 with lines lw 3 lt 1 title 'curve'

这填充了范围[-1:0.5][0.2:0.8]

为了举个例子,我使用特殊的文件名+:

set samples 100
set xrange [-2:2]
f(x) = -x**2 + 4

set linetype 1 lc rgb '#A3001E'

set style fill transparent solid 0.35 noborder
filter(x,min,max) = (x > min && x < max) ? x : 1/0
plot '+' using (filter($1, -1, -0.5)):(f($1)) with filledcurves x1 lt 1 notitle,\
     ''  using (filter($1, 0.2, 0.8)):(f($1)) with filledcurves x1 lt 1 notitle,\
     ''  using 1:(f($1)) with lines lw 3 lt 1 title 'curve'

使用结果 (使用4.6.4版本):

enter image description here

如果必须使用某种平滑方法,滤波器可能会对数据曲线产生不同的影响,具体取决于被滤波的部分。您可以先将平滑后的数据写入临时文件,然后将其用于“正常”绘图:

set table 'data-smoothed'
plot 'data' using 1:2 smooth bezier
unset table

set style fill transparent solid 0.35 noborder
filter(x,min,max) = (x > min && x < max) ? x : 1/0
plot 'data-smoothed' using (filter($1, -1, -0.5)):2 with filledcurves x1 lt 1 notitle,\
     ''  using (filter($1, 0.2, 0.8)):2 with filledcurves x1 lt 1 notitle,\
     ''  using 1:2 with lines lw 3 lt 1 title 'curve'

太好了,这很有效,但还有一件事,我想用平滑的sbezier绘制我的数据,但是填充部分与曲线不匹配。例如,在绘图的最后一部分进行替换:'' using 1:2 smooth sbezier with lines lw 3 lt 1 title 'curve' 图片 - texdditor
我也能看到这个问题,但我不知道为什么会发生。如果你在plot命令的每一行中使用smooth csplines,那么对我来说它就可以正常工作。 - Christoph
我无法在第二个绘图命令中使用“平滑样条”,因为它会抛出错误“无法计算样条,至少需要3个点”。无论如何,csplines不能以我想要的方式逼近曲线 - texdditor
1
请查看我的编辑答案。我知道的最好方法是将平滑数据写入临时文件,然后使用建议的过滤和绘图。 - Christoph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接