使用gnuplot:在单个图表中绘制来自多个输入文件的数据

93

我正在尝试使用 gnuplot 绘制图形。 我有六个文本文件,每个文本文件包含两列。 第一列表示时间(浮点数),第二列是序列号。 我想在单个图形中为所有六个文件绘制时间与序列号的图形。 我正在使用此文件完成操作。

set terminal png
set output 'akamai.png'

set xdata time
set timefmt "%S"
set xlabel "time"

set autoscale

set ylabel "highest seq number"
set format y "%s"

set title "seq number over time"
set key reverse Left outside
set grid

set style data linespoints

plot "print_1012720" using 1:2 title "Flow 1", \
plot "print_1058167" using 1:2 title "Flow 2", \
plot "print_193548"  using 1:2 title "Flow 3", \ 
plot "print_401125"  using 1:2 title "Flow 4", \
plot "print_401275"  using 1:2 title "Flow 5", \
plot "print_401276"  using 1:2 title "Flow 6"

我的文件在这里:

  • print_1012720
  • print_1058167
  • print_193548
  • print_401125
  • print_401275
  • print_401276

出现如下奇怪的错误:

"plot.plt",第24行: 变量未定义:plot

我做错了什么吗?是否可以将不同文件中的输入数据绘制在同一张图上?


聪明点,去这个主题 https://dev59.com/PmUp5IYBdhLWcg3w665C - Marcos Alex
3个回答

145
你离成功很近了!
更改
plot "print_1012720" using 1:2 title "Flow 1", \
plot "print_1058167" using 1:2 title "Flow 2", \
plot "print_193548"  using 1:2 title "Flow 3", \ 
plot "print_401125"  using 1:2 title "Flow 4", \
plot "print_401275"  using 1:2 title "Flow 5", \
plot "print_401276"  using 1:2 title "Flow 6"

to

plot "print_1012720" using 1:2 title "Flow 1", \
     "print_1058167" using 1:2 title "Flow 2", \
     "print_193548"  using 1:2 title "Flow 3", \ 
     "print_401125"  using 1:2 title "Flow 4", \
     "print_401275"  using 1:2 title "Flow 5", \
     "print_401276"  using 1:2 title "Flow 6"

错误出现的原因是gnuplot试图将单词"plot"解释为要绘制的文件名,但您还没有将任何字符串分配给名为"plot"的变量(这是好的 - 这样做会非常令人困惑)。

81
你可能会发现,在此情况下,gnuplot 的 for 循环非常有用,只要你适当地调整文件名或图表标题即可。
例如:
filenames = "first second third fourth fifth"
plot for [file in filenames] file."dat" using 1:2 with lines

filename(n) = sprintf("file_%d", n)
plot for [i=1:10] filename(i) using 1:2 with lines

6
我知道这已经有些陈旧了,但感谢您提供那个备选解决方案。我之前不知道 gnuplot 中还有循环语句,这是一个不错的功能。 - Amit
假设使用正常的文件名约定(name.dat),我认为应该是文件“.dat”。第一个“.”将文件名连接到“dat”,但实际上并未包含在绘图命令中使用的文件名中。 - jake
如何在不显式写出文件名的情况下绘制目录中的所有文件? (https://dev59.com/S10a5IYBdhLWcg3wxbHm) - a06e
我已经回答了那个问题,@becko。 - Richard

32

replot

这是一种同时获取多个绘图的另一种方法:

plot file1.data
replot file2.data

2
最佳答案是第一个没有起作用。输出为:“格式必须具有1-7个双精度类型转换(%lf)”。而其他选项只允许您在所有数据列都相同时执行此操作。 - RSM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接