【问题标题】:Make GNUPLOT save a plot from shell让 GNUPLOT 从 shell 中保存绘图
【发布时间】:2015-12-03 15:17:41
【问题描述】:

使用某个程序(PersistenceLandscapes 工具箱)我正在生成大量脚本,我从中使用 gnuplot 生成图。我遍历文件并使用命令gnuplot gnuplotCommand.txt -p 使 gnuplot 显示绘图。如何让 gnuplot 将绘图保存为 PNG 或(最好是)EPS 格式? (我想避免干预gnuplotCommand-type 脚本。)

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    你可以试试类似的 bash 脚本

    gnuplot <<- EOF
        set term png
        set output 'gnuplotCommand.txt.png'
        load 'gnuplotCommand.txt'
    EOF
    

    或者,.eps 版本

    gnuplot <<- EOF
        set terminal postscript eps
        set output 'gnuplotCommand.txt.eps'
        load 'gnuplotCommand.txt'
    EOF
    

    【讨论】:

    • plot 'gnuplotCommand.txt'更改为load 'gnuplotCommand.txt',因为该文件是其他程序自动生成的脚本。
    • 我想你刚刚回答了我在上面评论中提出的问题。我需要使用 system("date +F").png 作为我的文件名。我知道如何用两行代码做到这一点。 TNX。
    【解决方案2】:

    最简单的解决方案是通过-e 选项添加终端设置,并将标准输出通过管道传输到所需的输出文件:

    gnuplot -e 'set term pngcairo' gnuplotCommand.txt > output.png
    

    【讨论】:

    • 这很方便。我只使用同一个绘图程序的两个版本。一个用于 pngcairo,一个用于 X11,带有暂停和重读屏幕。但这很高兴知道。也不要尝试通过管道传输到这样的 png 文件。不过,我的输出文件需要按日期命名,所以我需要以某种方式将文件名设置为 --system("date +%F:).png --我是否只需在设置的术语后使用 \n 以便添加更多行,或者命名可以在 bash 中完成?我向你致敬,@Christoph。
    【解决方案3】:

    如果您有 gnuplot 5.0 版,您可以将参数传递给您的脚本。例如,

    # script.gp 
    if (ARGC > 1) {
        set terminal ARG2
        set output ARG3
    
        print 'output file : ', ARG3, ' (', ARG2 , ')'
    }
    
    # load the script needed 
    load ARG1
    

    此脚本必须使用选项-c 调用

    gnuplot -c script.gp gnuplotCommand.txt pngcairo output.png
    

    在本例中,我们设置了变量ARG1=gnuplotCommand.txtARG2=pncairoARG3=output.png。参数数量为ARCG=3。此外,它已被设置为ARG0=script.gp 作为主脚本的名称。

    如果您只想查看输出,而不将其保存到文件中,则可以将此脚本称为:

    gnuplot -p script.gp gnuplotCommand.txt
    

    您可能需要检查用户是否为输出文件指定了名称。如果没有,我们可以使用默认名称:

    if (ARGC > 1) {
        if (ARGC < 3) { ARG3="default" }    # without extension... it doesn't matter in linux :)
        set terminal ARG2
        set output ARG3
    
        print 'output file : ', ARG3, ' (', ARG2 , ')'
    }
    

    【讨论】:

    • 参数也可以在早期版本中传递,但使用不同的语法。
    • 这看起来不错,但如果参数需要是今天的日期或者可能是任意日期怎么办?在程序中,我会使用 date=system("date +%F") 然后设置输出日期。".png" 来获取今天的日期。
    • @SDsolar 尝试使用gnuplot -c script.gp "$(date +%F)"。这会将ARG1 设置为日期(在脚本中将其用作ARG1.".png"
    • 谢谢你。我明白了,感谢您的指导。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2014-08-26
    • 2019-02-19
    相关资源
    最近更新 更多