英文:
Is it possible to read in the data from a file to a gnuplot array?
问题
以下是您要翻译的内容:
有没有一种方法可以将数据从文件加载到Gnuplot数组中?
我正在使用一个程序,该程序提供数据并在内部调用gnuplot来创建图表。现在我正在尝试设置xrange,使得第二列中的数据最终接近零的上限被设置。
我已经搜索了很多并尝试了不同的方法。不幸的是,没有成功。对我来说最有前途但仍然不起作用的方法似乎是导入数据并将其存储在数组中。以下是代码的一部分:
array myarray[STATS_records]
do for [i=STATS_records:0:-1] {
stats '<datafile>' using 1:($2/1000.) index i nooutput
myarray[i] = STATS_min_y
}
循环中的stats表达式会导致错误,因为找不到任何值。
提前感谢您的建议或替代方案。
英文:
Is there a way to load data from a file into a Gnuplot array?
I am working with a program that provides the data and calls gnuplot internally to create a plot. Now I am trying to set the xrange that the upper limit is set where the data in the second column finally approaches zero.
I have googled a lot and tried different methods. Unfortunately without success. The most promising but still not working approach seems to me to import the data and store it in an array. An excerpt from the code is provied below:
stats '<datafile>' using 1:($2/1000.) nooutput
array myarray[STATS_records]
do for [i=STATS_records:0:-1] {
stats '<datafile>' using 1:($2/1000.) index i nooutput
myarray[i] = STATS_min_y
}
The stats expression in the loop causes an error because no values are found.
Thank you in advance for your suggestions or alternative solutions.
答案1
得分: 0
从您当前的问题标题和代码片段中,我猜您想将数据反转读入数组中。
以下是代码部分的翻译:
### 从文件中读取数据并反转到数组中
重置会话
文件 = "SO76218289.dat"
统计 文件 u 0 nooutput # 获取行数
行数 = STATS_records
数组 A[行数] # 设置数组大小
统计 文件 u (A[int(行数-$0)] = $2) nooutput
打印 A
### 脚本结束
结果:
[10.9,9.8,8.7,7.6,6.5,5.4,4.3,3.2,2.1,1.0]
请注意,这只是代码的翻译部分,不包括其他内容。如果您需要对代码进行调试或者有其他问题,请随时告诉我。
英文:
From your current question title and code snippet I assume you want to read data reversed into an array.
> Now I am trying to set the xrange that the upper limit is set where the data in the second column finally approaches zero.
This sentence makes me think that maybe there is more behind which you don't show and which can maybe solved also without arrays. Maybe we are running into a xy-problem?
The following script reads a column of a data file reversed into an array.
Data: SO76218289.dat
1 1.0
2 2.1
3 3.2
4 4.3
5 5.4
6 6.5
7 7.6
8 8.7
9 9.8
10 10.9
Script: (requires gnuplot>=5.2.0)
### read data from file reversed into array
reset session
FILE = "SO76218289.dat"
stats FILE u 0 nooutput # get number of rows
rowCount = STATS_records
array A[rowCount] # set array size
stats FILE u (A[int(rowCount-$0)] = $2) nooutput
print A
### end of script
Result:
[10.9,9.8,8.7,7.6,6.5,5.4,4.3,3.2,2.1,1.0]
Addition:
Actually, it was a bit of a xy-problem: You asked for putting data into an array, but the actual goal was to limit the xrange to the range where y>0
. This can be done simpler. Using the ternary operator (check help ternary
) the x-values will be set to NaN
if y<0
. A value of NaN
will not be plotted and considered for the x-autorange. Hence, in the example below the x-range is automatically limited to [3:7]
. If the y-values might get below zero within the limited range and you nevertheless want lines drawn down to zero, the script needs to be adapted.
Script:
### limit xrange to data y>0
reset session
$Data <<EOD
1 -1.0
2 -0.5
3 0.5
4 3.0
5 2.0
6 1.0
7 0.5
8 -0.1
9 -0.5
10 -1.0
EOD
plot $Data u ($2>0 ? $1 : NaN):2 w lp pt 7 lc "red" notitle
### end of script
Result:
Addition 2:
Just to illustrate, you can also limit the data to an x-range where y>0
and including the first sequence of N
values y<=0
. This requires stats
and a more difficult statement to understand using ternary operators and serial evaluation (check help operators binary
). The final limitation will be done by every
(check help every
).
Script:
### limit x-range to values with y>0 and N values y<=0
reset session
$Data <<EOD
1 -1.0
2 -0.5
3 0.5
4 3.0
5 2.0
6 1.0
7 0.5
8 0.0
9 0.0
10 0.0
11 -1.0
12 -2.0
EOD
N = 3 # max. number of y-values <0 in a sequence
n0 = n1 = NaN
c = 0
stats $Data u ($2>0 && n0!=n0 ? n0=$0 : $2<=0 && n0==n0 && n1!=n1 ? c=c+1 : c=0, c==N ? n1=$0 : 0) nooutput
print n0,n1
plot $Data u 1:2 every ::n0::n1 w lp pt 7 lc "red"
### end of script
Some explanations:
- first
n0
andn1
are initialized toNaN
andc=0
.
What is the meaning of:
($2>0 && n0!=n0 ? n0=$0 : $2<=0 && n0==n0 && n1!=n1 ? c=c+1 : c=0, c==N ? n1=$0 : 0)
- if
y>0
andn0
isNaN
(see https://stackoverflow.com/q/54126142/7295599) then setn0
to the current line index$0
which will be the first timey>0
. - else if
y<=0
andn0
is notNaN
andn1
isNaN
, increase the counterc
by 1, else reset the counter toc=0
. - if
c
is equal toN
setn1
to the current line index$0
.
In summary: n0
contains the line index with the first time y>0
and n1
contains the line index when y
was N
-times <=0
(but only after n0
was set to some value).
When you plot the data, you simply limit it via every ::n0::n1
.
The example will limit the x-range to [3:10] because the last N=3 y-values are y<=0.
Result:
答案2
得分: 0
感谢你非常!这正是我正在寻找的。但也许当我发布一个更详细的代码片段以阐明我的尝试时,对其他人也有帮助:
FILE = '<datafile>'
stats FILE u 0 nooutput
rowCount = STATS_records
array A[rowCount]
stats FILE u (A[int(rowCount-$0)] = $2) nooutput
do for [i=1:rowCount-1] {
if (int(A[i]) > 0) {
xoffset = rowCount - i
break
}
}
stats '<datafile>' u 1:($2/1000) nooutput
xmin = STATS_min_x
xmax = STATS_min_x + xoffset
# 设置范围和刻度
set xrange [xmin:xmax]
plot '<datafile>' using 1:($2/1000.)
我的代码尝试找到数据列中仅包含零值的点。它相应地调整x范围。这对我有用。但如果你知道更好的版本,请告诉我。我很高兴向你学习并提高我的gnuplot技能。
英文:
Thank you very much! This is exactly what I was looking for. But maybe it is helpful for others when I publish a more extended code snippet to clarify what I was trying to do:
FILE = '<datafile>'
stats FILE u 0 nooutput
rowCount = STATS_records
array A[rowCount]
stats FILE u (A[int(rowCount-$0)] = $2) nooutput
do for [i=1:rowCount-1] {
if (int(A[i]) > 0) {
xoffset = rowCount - i
break
}
}
stats '<datafile>' u 1:($2/1000) nooutput
xmin = STATS_min_x
xmax = STATS_min_x + xoffset
# Set ranges and ticks
set xrange [xmin:xmax]
plot '<datafile>' using 1:($2/1000.)
With my code I try to find the point where my data column just contains zero values. It adjusts the xrange accordingly.
This works for me. But if you know a better version please let me know. I would be glad to learn from you and improve my gnuplot skills.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论