从x计算单个标签的y [英] Calculate y from x for a single label

查看:62
本文介绍了从x计算单个标签的y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过仅输入x并从数据点计算y来在图形中的某个x位置处手动添加标签.

I want to manually add a label in the graph at a certain x position by only entering x and calculate y from the datapoint.

例如:

 1    set terminal png
 2    set output 'test2.png'
 3    
 4    x = 3
 5    y = 33
 6    set label "test" at x,y
 7    
 8    plot '-' using 1:2 with lines
 9      1 11
10      2 22
11      3 33
12      4 22
13      5 33

因此,我不想在第5行中对"33"进行硬编码.

So, I don't want to hardcode "33" in line 5.

有可能吗?

(我用Google搜索这个问题,并在输入标题后检查了Stackoverflow提供的可能重复项.Nada.)

(I have googled this question and inspected the possible duplicates offered by Stackoverflow after I entered the title. Nada.)

推荐答案

方法1(数据扫描)

对于不太大的输入,可以将x/y数据加载到两个单独的数组中,然后找到与给定x值相对应的y值:

For not too-large inputs, one could load the x/y data into two separate arrays and then locate the y-value corresponding to a given x-value:

$DATA <<EOD
1 11
2 22
3 33
4 22
5 33
EOD

stat $DATA nooutput
N = STATS_records

array data_x[N]
array data_y[N]
stat $DATA u (data_x[1+$0]=$1,data_y[1+$0]=$2,$1):2 nooutput

x0 = 3
y0 = NaN
do for [i=1:N] {
  if (data_x[i] == x0) {
    y0 = data_y[i]
  }
}

set label "test" at x0,y0

plot $DATA using 1:2 with lines t ''

这里的技巧是第二个stat命令,该命令仅用于填充数组data_xdata_y.表达式(data_x[1+$0]=$1,data_y[1+$0]=$2,$1):2基本上对1:2的数据中的每一行求值.但是,由于逗号运算符,它具有设置data_xdata_y.

The trick here is the second stat command which is used solely in order to populate the arrays data_x, and data_y. The expression (data_x[1+$0]=$1,data_y[1+$0]=$2,$1):2 basically evaluates for each row in the data to 1:2. However, due to the comma operator, it has the side effect of setting the corresponding elements of data_x, and data_y.

方法2(拟合)

作为一种近似的替代方法,可以将以x0 = 3为中心的窄高斯拟合到输入数据集.如果此高斯的特征宽度sigma小于数据点之间的特征间隔,则振幅应与输入的y值相匹配:

As an approximate alternative, one might fit a narrow Gaussian centered at x0 = 3 to the input data set. If the characteristic width sigma of this Gaussian is smaller than the characteristic spacing between data points, then the amplitude should match the input y-value:

$DATA <<EOD
1 11
2 22
3 33
4 22
5 33
EOD

x0 = 3
sigma = 0.1

f(x) = a*exp(-(x - x0)**2 / (2*sigma**2))
fit f(x) $DATA using 1:2 via a
print a

y0 = a

set label "test" at x0,y0
plot $DATA using 1:2 with lines t ''

方法3(外部工具)

作为一种更强大的替代方法,可以将此任务委托给通过system调用在Gnuplot中调用的外部实用程序.该示例使用gawk扫描输入数据文件.如果它在第一列中找到匹配的值,则将打印相应的y值并退出(以便仅报告第一次出现的情况):

As a more robust alternative, one could delegate this task to an external utility invoked in Gnuplot via the system call. The example is using gawk to scan the input data file. If it finds a matching value in the first column, it prints the corresponding y value and exits (so that only the first occurence is reported):

inputFileName = 'data.txt'

x0 = 3

getY(fName, x) = system(sprintf("gawk '$1==%d{print $2;exit}' '%s'", x, fName))

y0 = getY(inputFileName, 3)

set label "test" at x0,y0
plot inputFileName using 1:2 with lines t ''

这篇关于从x计算单个标签的y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆