【问题标题】:Use gnuplot command without prompt在没有提示的情况下使用 gnuplot 命令
【发布时间】:2017-01-22 20:48:33
【问题描述】:

要通过指定参数直接使用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 ?因此,我可以将它作为 Gnuplot 命令的参数输入,就像对 DataFile 所做的那样。 (我想这样使用它,因为我想从外部代码中调用它)

我想实现这样的目标:

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

第二个问题:

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

还要注意轴标题没有显示!

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

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

我应该如何解决这两个问题,首先是在 SettingsFile 中提到一个输出文件,其次是 xtics 没有正确显示,第三是屏幕输出中出现这种奇怪的行为?

【问题讨论】:

  • 还有哪个操作系统?可以(?)有所作为。
  • 我会在另一篇文章中问第二个问题。另外,您使用的是哪个版本的 gnuplot 和哪个终端(wxt、qt、x11)?注意set datafile separator "," 应该在plot 命令之前(xlabelylabeltitle 命令相同)。第一个问题,试试passing command-line arguments to gnuplot
  • @kebs : linux 标签使用,我用完整的操作系统信息更新了我的问题。
  • @vagoberto 您可能是正确的将帖子拆分为问题,但是......但是。我使用qt gnuplot,thx作为链接,我会重新检查它。

标签: linux gnuplot


【解决方案1】:

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 的“调用”机制加载脚本并将命令行的其余部分作为参数传递给它。见How to pass command line argument to gnuplot?

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

【讨论】:

  • 正如我在问题中提到的,我想从外部程序/代码(例如 java)执行命令。没有该脚本可以输入 output.png 参数吗?
  • 我在 C++ 和 Fortran 中执行类似的命令。我不明白为什么它在你的情况下不起作用。无论如何,如果您坚持自己的方法,请在托盘上写上gnuplot -e "filename='DataFile.txt'; output='image.png'" SettingsFile
  • 我知道它会起作用,但是我的解决方案将被部署,所以我想避免强迫“他们”运行其他任何东西。但是,您的解决方案工作正常,请考虑将其添加到答案中。
猜你喜欢
  • 2023-02-11
  • 2017-03-20
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 2017-11-07
  • 2019-03-05
相关资源
最近更新 更多