Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在gnuplot中使用动态变量名;加上;符号_Arrays_Loops_Gnuplot_Quotes - Fatal编程技术网

Arrays 如何在gnuplot中使用动态变量名;加上;符号

Arrays 如何在gnuplot中使用动态变量名;加上;符号,arrays,loops,gnuplot,quotes,Arrays,Loops,Gnuplot,Quotes,我想在plot命令中使用变量或数组,for循环如下 style[1]= "lines lt 4 lw 2" style[2] = "points lt 3 pt 5 ps 2" .... title[1]= "first title" title[2]= "second title " ... 或 绘图命令 plot for [i=1:1000] 'data.txt' u 1:2 title title[i] w

我想在plot命令中使用变量或数组,for循环如下

style[1]= "lines lt 4 lw 2"
style[2] = "points lt 3 pt 5 ps 2"
....

title[1]= "first title"
title[2]= "second title "
...

绘图命令

plot for [i=1:1000] 'data.txt' u 1:2 title title[i] with style[i]
plot for [i=1:1000] 'data.txt' u 1:2 title word(title,i) with word(style,i)
我在标题部分取得了成功,但在与部分却没有取得成功。我意识到问题是由引用引起的

i=1
plot 'data.txt' u 1:2 title "first title" with "lines lt 4 lw 2"
当我使用数组和word属性时,由于引号错误,我得到一个错误。我尝试了sprintf,但仍然没有成功

sprintf("with %s", style'.i.')

我希望我能正确地解释。知道我怎么解决这个问题吗?或者如何删除引号。非常感谢。

如果您要从中选择的样式仅由线条和/或点组成,则可以通过定义相应的
线条样式,然后按编号进行选择

set style line 1 lt 4 lw 2 pt 0               # lines only, no points
set style line 2 lt 3 lw 0.001 pt 5 ps 2      # points only, no lines
array Title[2] = ["style 1", "style 2"]

plot for [i=1:2] sin(x/i) with linespoints ls i title Title[i]
注意:通过将lw精确设置为0来抑制线可能不起作用,因为许多端子将其解释为“最细的线”,而不是“无线”。因此,我们将其设置为非常非常薄的值,希望它不可见


如果要从中选择的样式不包括直线+点,则不,我认为不可能使用此技巧。

您可以尝试将完整的绘图命令构建为字符串,然后
eval

max_i = 2
array title[max_i]
array style[max_i]

style[1]= "lines lt 4 lw 2"
style[2] = "points lt 3 pt 5 ps 2"

title[1]= "first title"
title[2]= "second title"


cmd="plot NaN notitle"  # dummy for starting iteration
do for [i=1:max_i] {
   # append the explicit plot command
   cmd = sprintf("%s, sin(x*%d) title \"%s\" with %s", cmd, i, title[i], style[i])
}
print cmd  # just for checking the final plot command

eval cmd   # run plot command


对于1000行来说,它可能很慢(甚至根本不起作用)。但无论如何,我都有点害怕在一个图表中包含1000行和标题。

欢迎来到StackOverflow!我想您不能简单地更改
循环的打印样式,但可以更改线型。也许这个或这个对你有帮助。谢谢@theozh。我会仔细检查你发布的链接。谢谢你的回答谢谢你的回答@Ethan。也许这个方法对我有效。我了解到,在plot命令下,我们无法轻松返回值。谢谢@maij。是的,这几乎正是我想要的。我猜解决方案与sprintf和eval有关。但我没能解决它。谢谢你的回答。
max_i = 2
array title[max_i]
array style[max_i]

style[1]= "lines lt 4 lw 2"
style[2] = "points lt 3 pt 5 ps 2"

title[1]= "first title"
title[2]= "second title"


cmd="plot NaN notitle"  # dummy for starting iteration
do for [i=1:max_i] {
   # append the explicit plot command
   cmd = sprintf("%s, sin(x*%d) title \"%s\" with %s", cmd, i, title[i], style[i])
}
print cmd  # just for checking the final plot command

eval cmd   # run plot command