我可以计算一个for循环内的东西,然后在同一个图上绘制这些值吗? [英] Can I calculate something inside a for loop and then plot those values on the same graph?

查看:141
本文介绍了我可以计算一个for循环内的东西,然后在同一个图上绘制这些值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,绘制了4行:

$ p $ plot for [i = 1:4] \
path_to_file使用1 :(列(i)),\

我也想绘制在这张图上有8条水平线,其中的值来自mydata.txt。



我已经看到了,从,如何从数据文件加载和显示单个数值?我可以使用stats命令来访问我感兴趣的常量值。我想我可以像下面这样访问单元格(row,col):

  stats'mydata.txt'every :: row :: row使用col nooutput 
value = int(STATS_min)

但是他们的位置是i的函数。因此,在plot命令中,我想添加如下内容:对于[i = 1:4],

$ p $ $ b stats'mydata.txt'every::( 1 + i * 10)::( 1 + i * 10)using 1 nooutput
mean = int(STATS_min)
stats'mydata.txt' (1 + i * 10)::( 1 + i * 10)使用2无输出
SE = int(STATS_min)
上=平均值+ 2 * SE
下=均值 - 2 * SE

然后在上面的图上作为水平线上下绘制。 / p>

我想我可以通过键入绘图上部,下部分别绘制它们,但是如何在图形上绘制它们,以上,为我所有?

谢谢。

可以创建一个数组并存储其中的值,然后使用引用数组中的值的位置的索引,您可以在循环内访问它。



您可以创建数组如下:

  array =
for [i = 1:4] {
val = i / 9.
array = sprintf( %s%g,array,val)
}

:1/9,2/9,3/9和4/9。在你的情况下,你可以运行 stats 并存储你的 upper 和/或 lower 变量。您可以通过这种方式来检查数组的样子:

  gnuplot>打印数组
0.111111 0.222222 0.333333 0.444444

对于绘图,您可以访问数组使用 word(array,i),其中 i 指向位置。由于数组是一个字符串,所以需要将它转换为float,这可以乘以 1。



< pre $ plot for [i = 1:4] 1. * word(array,i)





如果您将值存储在数据文件中,则可以使用 awk 甚至使用gnuplot来处理:

  array =
plot [i = 1:4]dataevery :: i :: iu(array = sprintf(%s%g,数组,$ 1),1/0),\
for [i = 1:4] 1. * word(array,i)

第一个 plot 实例从第一列数据项创建数组而不绘制点( 1/0 选项告诉gnuplot忽略它们,因此需要警告消息),第二个 plot 实例使用存储在 array 作为变量(因此在这种情况下作为水平线)。请注意,每个都将0作为第一个条目,所以 [i = 1:4] 从第二个到第该文件的第五行。


I have the following code, which plots 4 lines:

plot for [i=1:4] \
path_to_file using 1:(column(i)) , \

I also want to plot 8 horizontal lines on this graph, the values of which come from mydata.txt.

I have seen, from the answer to Gnuplot: How to load and display single numeric value from data file, that I can use the stats command to access the constant values I am interested in. I think I can access the cell (row, col) as follows:

stats 'mydata.txt' every ::row::row using col nooutput
value = int(STATS_min)

But their location is a function of i. So, inside the plot command, I want to add something like:

for [i=1:4] \
stats 'mydata.txt' every ::(1+i*10)::(1+i*10) using 1 nooutput
mean = int(STATS_min)
stats 'mydata.txt' every ::(1+i*10)::(1+i*10) using 2 nooutput
SE = int(STATS_min)
upper = mean + 2 * SE
lower = mean - 2 * SE

and then plot upper and lower, as horizontal lines on the graph, above.

I think I can plot them separately by typing plot upper, lower but how do I plot them on the graph, above, for all i?

Thank you.

解决方案

You can create an array and store the values in it, then using an index that refers to the value's position in the array you can access it inside a loop.

You can create the array as follows:

array=""
do for [i=1:4] {
val = i / 9.
array = sprintf("%s %g",array,val)
}

where I have stored 4 values: 1/9, 2/9, 3/9 and 4/9. In your case you would run stats and store your upper and/or lower variables. You can check what the array looks like in this way:

gnuplot> print array
 0.111111 0.222222 0.333333 0.444444

For plotting, you can access the different elements in the array using word(array,i), where i refers to the position. Since the array is a string, you need to convert it to float, which can be done multiplying by 1.:

plot for [i=1:4] 1.*word(array,i)

If you have values stored in a data file, you can process it with awk or even with gnuplot:

array = ""
plot for [i=1:4] "data" every ::i::i u (array=sprintf("%s %g",array,$1), 1/0), \
for [i=1:4] 1.*word(array,i)

The first plot instance creates the array from the first column data entries without plotting the points (the 1/0 option tells gnuplot to ignore them, so expect warning messages) and the second plot instance uses the values stored in array as variables (hence as horizontal lines in this case). Note that every takes 0 as the first entry, so [i=1:4] runs from the second through to the fifth lines of the file.

这篇关于我可以计算一个for循环内的东西,然后在同一个图上绘制这些值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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