gnuplot 中的循环绘图

Loop plotting in gnuplot

我正在尝试在 gnuplot 中循环绘制大约 105 个文件。 这就是我在终端中输入的内容。

i=0.0
n=0.052
set terminal jpeg 
load "plot.plt" 

这是我在 plot.plt

中的内容
filename = "mean_230_1.6_A".i.".dat"
plotfile = "mean_230_1.6_A".i."jpg"
print filename." ".plotfile
set output plotfile
plot "PSD_230.dat" u 2:3 w lines , filename using 1:2:3 w yerr pt 7
set output
i=i+0.0005
if (i <= n) reread 

然而,然后我执行命令。我收到此错误:

"plot.plt", line 1: internal error : STRING operator applied to non-STRING type

我不知道该怎么办。我之前使用过类似的代码,并且有效。

我猜你在将 i(浮点数)与字符串连接时遇到了问题?

尝试sprintf("myfilename_%f.jpg", i)创建文件名。

我会尝试使用 for 循环:

set terminal jpeg
files  = system("ls mean*.dat")                          #stores all filenames
output = system("ls mean*.dat | sed -e 's/.dat/.jpg/'")  #stores all outputs

do for [i=1:words(files)]{
   filename = word(files,i)     #select filename number i
   plotfile = word(output,i)    #select corresponding output
   print filename." ".plotfile

   set output plotfile
   plot "PSD_230.dat" u 2:3 w lines , filename using 1:2:3 w yerr pt 7
   set output
}

如果您的任何文件名包含空格,请将 system 函数更改为:

files  = system("ls mean*.dat | xargs -I line echo \\"line\\" ")
output = system("ls mean*.dat | sed -e 's/.dat/.jpg/'" | xargs -I line echo \\"line\\" ")

关于您获得的错误,您应该使用@allo 的回答,但如果文件名具有不同的小数位数,则效率不高:

i=0.0005
plotfile = sprintf("mean_%f.jpg", i)      # 6 decimals: mean_0.000500.jpg
plotfile = sprintf("mean_%.4f.jpg", i)    # 4 decimals: mean_0.0005.jpg

i=0.001
plotfile = sprintf("mean_%.4f.jpg", i)    # 4 decimals: mean_0.0010.jpg
plotfile = sprintf("mean_%.3f.jpg", i)    # 3 decimals: mean_0.001.jpg

我认为,如果文件名中的数字有 4 位或更少的小数,最好使用 %g 而不是 %f 格式说明符:

sprintf("mean_%g.jpg", 1)         # 0 decimals: mean_1.jpg
sprintf("mean_%g.jpg", 0.01)      # 2 decimals: mean_0.01.jpg
sprintf("mean_%g.jpg", 0.0001)    # 4 decimals: mean_0.0001.jpg
sprintf("mean_%g.jpg", 0.00001)   # 5 decimals, unwanted result: mean_1e-05.jpg