首页 > 解决方案 > 如何在 gnuplot 中使用带有“with”表示法的动态变量名称?

问题描述

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

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

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

或者

style="'lines lt 4 lw 2' 'points lt 3 pt 5 ps 2'"
title="'first title' '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)

我在标题部分成功了,但在with部分没有成功。我意识到问题是由引号引起的。

i=1
plot 'data.txt' u 1:2 title "first title" with "lines lt 4 lw 2"

当我使用数组和单词属性时,由于引用错误而出现错误。我尝试了sprintf但仍然没有成功。

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

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

标签: arraysloopsgnuplotquotes

解决方案


如果您要从中选择的样式仅包含线和/或点,那么您可以通过定义相应的样式linestyle然后按数字选择它来完成此操作。

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 来抑制线条可能不起作用,因为许多终端将其解释为“最细的可能线条”而不是“无线条”。因此,我们将其设置为非常薄的东西,希望不会被看到。

在此处输入图像描述

如果您要从中选择的样式包括线+点以外的其他内容,那么不,我认为不可能使用此技巧。


推荐阅读