如何在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命令喜欢为我工作。

您能否为此目的提供一个示例,该示例正在发挥作用或提供了很好的建议。 THX!

解决方法

当在bash脚本中以下列方式将以上代码用作oneliner时,出现两个错误/警告

AutoreleasingUnsafeMutablePointer<AnyObject?>
  • 警告:y3axis不是字符串变量
  • 轴必须为x1y1,x1y2,x2y1或x2y2

如文档中所述,宏不能在定义的同时扩展,如果写在同一行上会是什么情况。

为避免上述错误,我们仅需使用

echo "gnuplot cmds;plot ..." | gnuplot echo -e "gnuplot cmds; \n

它像应该的那样工作。

@Christoph和@Ethan的积分使我走上了正轨。