使用gnuplot命令而不提示

时间:2017-01-22 20:48:34

标签: linux gnuplot

要通过指定参数直接使用Gnuplot(在Linux Ubuntu 16.04上)命令而不使用其提示符,我输入以下代码:

gnuplot -e "filename='DataFile.txt'" SettingsFile

SettingsFile看起来像这样:

plot filename with lines
set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","
pause -1

DataFile.txt看起来像这样:

数据包,时间(以秒为单位):

13392,120
24607,240
23867,360
21764,480
20727,600
20004,720
19719,840
19758,960
19728,1080
20168,1200
19737,1320
19729,1440
20135,1560
20006,1680
21301,1800
19923,1920
20002,2040
19761,2160
20918,2280
22756,2400
22820,2520
23370,2640
22987,2760
22956,2880
24427,3000
23527,3120
24009,3240
23832,3360
23464,3480
23652,3600
11212,3654

第一个问题:

有没有办法设置SettingsFile一个png OutputFile?所以我可以像使用DataFile一样输入它作为Gnuplot命令的参数。 (我想以这种方式使用它,因为我想从外部代码调用它)

我想实现这样的目标:

gnuplot -e "filename='DataFile.txt'" SettingsFile OutputFile.png 

第二个问题:

我从Gnuplot获得的屏幕输出显示xtics与预期不同:

enter image description here

另请注意,轴标题未显示!

现在,如果我尝试调整窗口大小,我会得到这个:

enter image description here

图表被奇怪地翻转,标题设置和抽搐根据需要进行更新。

我应该如何修复这两个问题,首先在SettingsFile中提及一个输出文件,然后在屏幕输出中第三个没有正确显示的xtics和第三个奇怪的行为?

1 个答案:

答案 0 :(得分:3)

可以通过分号向gnuplot -e添加多个命令,例如:

gnuplot -p -e 'filename="data.txt"; fileout="image.png"' SettingsFile

您的SettingsFile应该已经有一行配置终端类型:

set terminal png
set output fileout

set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","

plot filename using 2:1 with lines

如果您想要更好地控制代码,请尝试使用此脚本(gnuplot 5.0 +):

filename=(ARGC>0 ? ARG1 : 'DataFile.txt' )  # By default filename=DataFile.txt
                                            # If called with one argument (ARGC=1)
                                            # then `filename=ARG1`

if(ARGC>1){
  # if called with two arguments (ARGC=2), then configure a png output
  set terminal png
  set output ARG2
}

set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","

# the plot command ALWAYS at the end of the script after the settings
plot filename using 2:1 with lines
  • 如果您想以交互方式绘制'DataFile.txt'(默认情况下):

    gnuplot -p SettingsFile
    
  • 如果您想绘制另一个文件,例如AnotherData.txt

    gnuplot -p -c SettingsFile AnotherData.txt
    
  • 如果您想绘制另一个文件并将其另存为PNG:

    gnuplot -p -c SettingsFile AnotherData.txt Output.png
    

-p参数允许绘制窗口在主gnuplot程序退出后存活。 Thw -c参数使用gnuplot的“ca​​ll”机制加载脚本,并将其余的命令行作为参数传递给它。见How to pass command line argument to gnuplot?

请注意,您的脚本首先绘制数据文件,然后配置标签,标题和datafile separator。这就是为什么你会看到奇怪的抽动。