如何在Gnuplot中使用自定义函数将数值标签替换为字符串?
Gnuplot:用自定义函数将数值标签替换为字符串
原脚本
set datafile separator "," set datafile commentschar '#' set key autotitle columnhead # use the first line as title for data set key noenhanced function $labellookup(x) << EOF temp="null" if(x==2000){ temp="two" } if(x==1000){ temp="one" } if(x==3000){ temp="three"} return temp EOF set yrange[*:*] $DATA << EOD t, v1, v2 0, 10, 3000 10, 11, 1000 15, 10, 4000 30, 11, 5000 38, 20, 6000 46, 11, 8000 47, 30, 1000 48, 31, 1000 49, 30, 4000 50, 31, 6000 51, 30, 9000 52, 30, 2000 61, 21, 3000 65, 20, 3000 70, 21, 4000 75, 10, 5000 EOD # use column 1, and column 3 is the label; values # are (x,y)=(column1, 2) # column 2 is ignored plot $DATA using 1:(2):3 with labels rotate by 90 left
修改方案
只需修改最后一行的plot命令,把直接引用第三列数值的部分,替换为调用$labellookup函数并传入第三列数值作为参数:
plot $DATA using 1:(2):(@$labellookup($3)) with labels rotate by 90 left
关键说明
$3代表取当前行第三列的数值,作为参数传入$labellookup函数@$labellookup(...)是Gnuplot调用字符串函数的语法,用来展开函数返回的字符串作为标签内容- 函数未匹配到的数值(比如4000、5000等),会返回预设的"null"作为标签
内容的提问来源于stack exchange,提问作者asylumax
相关产品推荐
相关产品推荐

