有一种样式可以填充 x 的两个函数之间的空间。 此类图的示例可以找到,例如在 http://gnuplot.sourceforge.net/demo/fill Between.html 有什么方法可以制作类似的图,但 x 轴和 y 轴翻转? 这是所需的曲线形状(当然没有旋转/镜像标签、标题和图例)...
可以使用闭合轮廓来完成(就像这里的最后一个示例http://www.gnuplot.info/demo_svg_cvs/fillcrvs.html),但这需要重新整理数据文件。还有其他选择吗?
谢谢!
您不能直接执行此操作。来自
help filledcurves
:
第三个变体填充在同一组采样的两条曲线之间的区域 x 坐标。它需要三列输入数据(x、y1、y2)。
我认为你不能直接指定 (y, x1, x2) 。作为解决方法,您可以将 y 轴和较大函数之间的区域设置为某种颜色,然后将 y 轴和较小函数之间的区域填充为白色:
x1(y) = cos(y)+1
x2(y) = cos(y)+2
xmax(y) = (x1(y) > x2(y) ? x1(y) : x2(y))
xmin(y) = (x1(y) < x2(y) ? x1(y) : x2(y))
plot '+' using (xmax($1)):1 with filledcurve y1, \
'+' using (xmin($1)):1 with filledcurve y1 fillcolor rgb "white"
如果两个函数之一或两者都可以为负,则可能需要稍微调整。
使用 gnuplot >=5.2 可以进一步调整,因为它允许数组。 以下代码显示了如何实现垂直方向曲线之间的填充曲线的解决方法。您甚至可以使用透明度。如果您下载附件 PNG,您会发现它实际上具有透明背景。此解决方法背后的基本思想是创建封闭区域并填充它们。为此,您需要反转一个边框,连接边框并将其填充。不幸的是,gnuplot 没有反转列中数据点的功能,因此您必须自己通过特殊过程来完成。
代码:
### "Vertical" filledcurves
reset session
# create some dummy data
N = 50
set samples N
set xrange [-5:5]
set table $Data
plot '+' u (sin($1)):1:(rand(0)*0.3+1) with table
unset table
# put Borders into arrays
stats $Data nooutput
RowCount = STATS_records
array BorderX1[RowCount]
array BorderX2[RowCount]
array BorderY[RowCount]
set table $Dummy
plot $Data u (BorderX1[$0+1]=$1-$3):(BorderX2[$0+1]=$1+$3):(BorderY[$0+1]=$2) with table
unset table
# reverse BorderX2 and merge borders
set samples RowCount
set table $Border
plot '+' u (BorderX1[$0+1]):(BorderY[$0+1]) with table
plot '+' u (BorderX2[RowCount-$0]):(BorderY[RowCount-$0]) with table
unset table
# make the plot
set object 1 rect at 0,-3 size 10,0.5 fs solid 1.0 fc rgb "black" back
set yrange[-5:5]
plot \
$Border u 1:2 w filledcurves fc rgb "#AA00FF00" not,\
$Border u ($1*1.5):2 w filledcurves fc rgb "#AAFFFF00" not,\
$Data u ($1+2.5):2 w filledcurves y2 fc rgb "brown" not,\
$Data u 1:2 w l lw 8 lc rgb "blue" not,\
'+' u 1:(cos($1)-0.5):(cos($1)+0.5) w filledcurves lc rgb "grey" not,\
'+' u 1:(cos($1)):(1) w l lw 3 dt 2 lc rgb "white" not
### end of code
结果:
与我的第一个答案相比,这里有两种替代且更简单的方法。第一个甚至可以使用 gnuplot 5.0。
绘图样式
filledcurves
(到目前为止)只能在具有相同 x 值的两条 y 曲线之间填充。但是,gnuplot can 可以填充闭合曲线。因此,使曲线闭合。就像我的第一个答案一样,如果反转一条曲线并将其添加到另一条曲线,则可以做到这一点。
两个脚本的假设是数据有一个共同的 y 列,即按 3 列组织,例如这里:
y x1 x2
数据:
SO50676753.dat
(与OP的数据相同,来自gnuplot演示目录中的silver.dat
)
# y x1 x2
10 280 16.7332
20 191 13.8203
30 152 12.3288
40 150 12.2474
50 104 10.1980
60 77 8.7750
70 69 8.3066
80 60 7.7460
90 60 7.7460
100 51 7.1414
110 41 6.4031
120 34 5.8310
130 35 5.9161
140 34 5.8310
150 24 4.8990
160 24 4.8990
170 19 4.3589
180 21 4.5826
190 20 4.4721
200 18 4.2426
210 21 4.5826
220 15 3.8730
230 19 4.3589
240 12 3.4641
250 20 4.4721
260 20 4.4721
270 18 4.2426
280 18 4.2426
290 20 4.4721
300 12 3.4641
310 26 5.0990
320 17 4.1231
330 8 2.8284
340 6 2.4495
350 8 2.8284
360 10 3.1623
370 20 4.4721
380 14 3.7417
390 8 2.8284
400 10 3.1623
410 9 3.0000
420 8 2.8284
430 10 3.1623
440 13 3.6056
450 9 3.0000
460 5 2.2361
470 7 2.6458
480 11 3.3166
500 7 2.6458
510 9 3.0000
520 12 3.4641
530 4 2.0000
540 7 2.6458
550 10 3.1623
560 9 3.0000
580 8 2.8284
590 9 3.0000
600 5 2.2361
脚本 1:(适用于 gnuplot>=5.0.0)
这里假设您有 单调 和 唯一 y 值。这样,您可以使用选项
smooth unique
(至少在 gnuplot 4.x 版本中可用)来反转一条曲线。然而,由于这里的解决方案使用数据块和绘图风格with table
,它至少需要 gnuplot 5.0.0。也许通过一些解决方法和临时文件,您也可以让它与某些 4.6 版本一起工作。
### fill between vertical curves
reset session
FILE = "SO50676753.dat"
set table $Temp
plot FILE u 1:2, \
'' u (-$1):3 smooth unique # sort data reverse by changing sign
set table $Data
plot $Temp u 2:1 index 0 w table, \
'' u 2:(-$1) index 1 w table # change sign again
unset table
set style fill solid 0.3
set grid x,y
plot $Data u 1:2 w filledcurves
### end of script
脚本 2:(适用于 gnuplot>=5.2.0)
使用此解决方案时,对数据没有特殊假设,但由于它使用数据块索引,因此需要 gnuplot>=5.2.0。
### fill between vertical curves
reset session
FILE = "SO50676753.dat"
set table $Temp1
plot FILE u 2:1 w table
set table $Temp2
plot FILE u 3:1 w table
unset table
set print $Data
do for [i=|$Temp2|:1:-1] { print $Temp2[i] } # reverse data
print $Temp1
set print
set style fill solid 0.3
set grid x,y
plot $Data u 1:2 w filledcurves
### end of script
结果:(两个脚本相同):