如何在 Gnuplot 中以编程方式使用轴?

How to use axes programmatically in Gnuplot?

如果您需要针对 x1y1x1y2 轴绘制一些图形,具体取决于每个图形的最大 y 值,正确的语法是什么?

我的数据文件有几列。第一个包含 x 值,其他包含 y1、y2 等。 使用 stats 命令后,我可以轻松地为每个图形定义每个轴值:

stats '$data_file' u 1:2 nooutput;
y1max = STATS_max_y;
if (y1max > ymax) { y1axis = 'x1y2' } else { y1axis = 'x1y1' };
stats '$data_file' u 1:3 nooutput;
y2max = STATS_max_y;
if (y2max > ymax) { y2axis = 'x1y2' } else { y2axis = 'x1y1' };
...

之后,我可以用这个 plot cmd

绘制图表
plot '$data_file' using 1:2 axes x1y1 notitle with lines lc rgb 'black' lw 1,\
               '' using 1:3 axes x1y2 notitle with lines lc rgb 'green' lw 1;

这有效,但根本不是以编程方式。

但是这个有效

plot '$data_file' using 1:2 axes @y1axis notitle with lines lc rgb 'black' lw 1,\
               '' using 1:3 axes @y2axis notitle with lines lc rgb 'green' lw 1;

我在其中使用了 Substitution of string variables as macros(字符 @ 用于触发将字符串变量的当前值替换为 命令行...)写在 docs.

eval 命令也不适合我。

您能否为此提供一个有效的示例或任何好的建议。谢谢!

在 bash 脚本中使用上述代码作为单行代码时,我得到了两个 errors/warnings

echo "gnuplot cmds;plot ..." | gnuplot
  • 警告:y3axis 不是字符串变量
  • 轴必须是 x1y1、x1y2、x2y1 或 x2y2

文档里写的,宏定义的时候不能同时展开,写在同一行会怎样

为了避免上述错误,我们只需要使用

echo -e "gnuplot cmds;\nplot ..." | gnuplot

它工作正常。

感谢@Christoph 和@Ethan 让我走上正轨。