plot - 在 Gnuplot 中,如何在 for 循环中的同一个图上多次绘制函数

标签 plot gnuplot

在 gnuplot 中,我尝试绘制一个具有 5 个参数的函数,其值存储在外部文件中,在同一个图形上绘制 8 次。我想绘制 8 种物质的 Vapor 压与温度的关系图; Vapor 压由 5 个变量参数化。我尝试过使用 do-for 循环,但这只绘制了一种物种。如何使用 8 组参数在同一个图上绘制该函数 8 次?下面的代码基于this answer ,并且有效,除了给出的答案将打印 8 个 png,但我想要 1 个,并在尝试中对其进行了修改。

parameters.txt

A   B   C   D   E
33.634  -3647.9 -8.6428 -9.69E-11   1.19E-06
19.419  -5869.9 -0.4428 -1.26E-02   5.22E-06
-15.077 -4870.2 14.501  -3.16E-02   1.35E-05
76.1    -5030   -25.078 9.76E-03    -2.58E-13
2.1667  -2631.8 4.035   -1.18E-02   6.10E-06
39.917  -4132   -10.78  1.97E-10    2.04E-06
29.89   -3953.5 -7.2253 2.11E-11    8.96E-07
99.109  -7533.3 -32.251 1.05E-02    1.23E-12

vapor.plt

reset
datafile = "parameters.txt"

set terminal pngcairo
set xrange [273.15:493.15]
set logscale y
set output "vapor.png"
do for [step=1:8] {
    # read parameters from file, where the first line is the header, thus the +1
    a=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $1}' " . datafile)
    b=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $2}' " . datafile)
    c=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $3}' " . datafile)
    d=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $4}' " . datafile)
    e=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $5}' " . datafile)

    # convert parameters to numeric format
    a=a+0.
    b=b+0.
    c=c+0.
    d=d+0.
    e=e+0.

    plot 10**(a + b/x + c*log10(x) + d*x + e*x**2) title ''
}
set output

最佳答案

要将多个函数绘制到一张图中,您必须仅使用一个 plot 命令,并用逗号分隔函数:

plot f(x), g(x), h(x)

这会将所有三个函数绘制在一张图中。对于您的情况,您需要首先提取参数以获取 a1a2、...a8 等。这将具有优势,您可以拥有参数集的键(图例)。

第二个选项更适合您现有的脚本。您需要将 plot 调用放入 multiplot 中:

reset
datafile = "parameters.txt"

set terminal pngcairo
set xrange [273.15:493.15]
set logscale y
set output "vapor.png"

set lmargin at screen 0.1
set rmargin at screen 0.9
set bmargin at screen 0.1
set tmargin at screen 0.9

set multiplot
do for [step=1:8] {
    # read parameters from file, where the first line is the header, thus the +1
    a=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $1}' " . datafile)
    b=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $2}' " . datafile)
    c=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $3}' " . datafile)
    d=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $4}' " . datafile)
    e=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $5}' " . datafile)

    # convert parameters to numeric format
    a=a+0.
    b=b+0.
    c=c+0.
    d=d+0.
    e=e+0.

    plot 10**(a + b/x + c*log10(x) + d*x + e*x**2) lt step title ''

    if (step == 1) {
        unset border
        unset xtics
        unset ytics
    }
}
unset multiplot
set output

使用multiplot,每次都会重新绘制边框和抽动,这看起来很丑(更粗)。为此,我在第一个图之后取消设置边框、xtics 和 ytics。但为了使所有图具有相同的边距,我在开始时设置了固定的绝对边距。可以保留使用第一个图计算的自动边距,但这有点冗长(请参阅文档中的主题“Gnuplot 定义的变量”)。

我还为每个绘图使用了不同的线型。上面的脚本给出了输出:

enter image description here

关于plot - 在 Gnuplot 中,如何在 for 循环中的同一个图上多次绘制函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19057515/

相关文章:

linux - 使用 Gnuplot 损坏的 Eps 图像/绘图

r - 在 R 中以数据无关的方式添加文本注释

使用 Latex 解释器的 Matlab 图例文本溢出

Gnuplot:绘制两个矩阵之间的差异

gnuplot - 使用 gnuplot 绘制灰度直方图

gnuplot - 如何绘制相同的值直到它改变?

Gnuplot - 在热图上书写文本

matlab - 如何将 pcolor 图与使用不同颜色图的等高线图叠加?

plot - gnuplot:带有误差条的行堆积条形图

matlab - 对于具有相同轴 handle 的两个绘图 handle ,按住一键不起作用