是可能将数据从文件读入到gnuplot数组中吗?

huangapple go评论110阅读模式
英文:

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 &#39;&lt;datafile&gt;&#39; using 1:($2/1000.) nooutput
array myarray[STATS_records]
do for [i=STATS_records:0:-1] {
  stats &#39;&lt;datafile&gt;&#39; 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 = &quot;SO76218289.dat&quot;

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&gt;0. This can be done simpler. Using the ternary operator (check help ternary) the x-values will be set to NaN if y&lt;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&gt;0
reset session

$Data &lt;&lt;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&gt;0 ? $1 : NaN):2 w lp pt 7 lc &quot;red&quot; notitle
### end of script

Result:

是可能将数据从文件读入到gnuplot数组中吗?

Addition 2:

Just to illustrate, you can also limit the data to an x-range where y&gt;0 and including the first sequence of N values y&lt;=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&gt;0 and N values y&lt;=0
reset session

$Data &lt;&lt;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 &lt;0 in a sequence
n0 = n1 = NaN
c  = 0
stats $Data u ($2&gt;0 &amp;&amp; n0!=n0 ? n0=$0 : $2&lt;=0 &amp;&amp; n0==n0 &amp;&amp; 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 &quot;red&quot;
### end of script

Some explanations:

  • first n0 and n1 are initialized to NaN and c=0.

What is the meaning of:

($2&gt;0 &amp;&amp; n0!=n0 ? n0=$0 : $2&lt;=0 &amp;&amp; n0==n0 &amp;&amp; n1!=n1 ? c=c+1 : c=0, c==N ? n1=$0 : 0)
  • if y&gt;0 and n0 is NaN (see https://stackoverflow.com/q/54126142/7295599) then set n0 to the current line index $0 which will be the first time y&gt;0.
  • else if y&lt;=0 and n0 is not NaN and n1 is NaN, increase the counter c by 1, else reset the counter to c=0.
  • if c is equal to N set n1 to the current line index $0.

In summary: n0 contains the line index with the first time y&gt;0 and n1 contains the line index when y was N-times &lt;=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:

是可能将数据从文件读入到gnuplot数组中吗?

答案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 = &#39;&lt;datafile&gt;&#39;

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]) &gt; 0) {
     xoffset = rowCount - i
     break
  }
}

stats &#39;&lt;datafile&gt;&#39; u 1:($2/1000) nooutput
xmin = STATS_min_x
xmax = STATS_min_x + xoffset

# Set ranges and ticks
set xrange [xmin:xmax]

plot &#39;&lt;datafile&gt;&#39; 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.

huangapple
  • 本文由 发表于 2023年5月10日 20:14:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定