【问题标题】:How to get the distance between automatic generated gnuplot tics?如何获得自动生成的 gnuplot 抽动之间的距离?
【发布时间】:2014-09-22 20:18:42
【问题描述】:

我正在使用 gnuplot 进行大量绘图。由于每个图的数据范围(对于 x 和 y 轴)都是可变的,我需要让 gnuplot 自动设置范围和抽动。但是,我需要在绘图下方放置一个已定义的网格,水平线每个 1/8 单位,垂直线 1/4 单位。当我让 gnuplot 决定在哪里放置 tic 时,我不知道两个 tic 之间的距离(以单位为单位),因此,我不知道我应该在 m{x|y}tics 中进行的细分数量所需的输出。

例如:如果我每两个单位有一个 ytic,我需要“设置 mytics 8”。如果我每个单位都有一个 ytic,我需要“设置 mytics 4”。

那么,有办法获得自动放置的抽动之间的距离吗?甚至是绘制的抽动次数?

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    要获取自动放置的抽动之间的距离,请使用以下代码(另存为ticstep.gp):

    xr = abs(max_value - min_value)
    power = 10.0 ** floor(log10(xr))
    xnorm = xr / power # approximate number of decades
    posns = 20.0 / xnorm;
    
    if (posns > 40) {
      tics = 0.05
    } else {
      if (posns > 20) {
        tics = 0.1
      } else {
        if (posns > 10) {
          tics = 0.2
        } else {
          if (posns > 4) {
            tics = 0.5
          } else {
            if (posns > 2) {
              tics = 1
            } else {
              if (posns > 0.5) {
                tics = 2
              } else {
                tics = ceil(xnorm)
              }
            }
          }
        }
      }
    }
    ticstep = tics * power
    

    这应该等同于确定 ticstep 的内部 gnuplot 代码(参见 axis.c, line 677

    仅获取ticstep,您可以使用stats获取相应的数据值:

    stats 'file.txt' using 1 noutput
    max_value = STATS_max
    min_value = STATS_min
    load 'ticstep.gp'
    print ticstep
    

    要获得绘制的抽动数,您需要自动扩展轴限制(除非您使用set autoscale fix)。为此,您可以使用unknown 终端进行绘图以获取例如GPVAL_Y_MAXGPVAL_Y_MIN:

    set terminal push # save current terminal
    set terminal unknown
    plot 'file.txt' using 1
    set terminal pop # restore terminal
    max_value = GPVAL_Y_MAX
    min_value = GPVAL_Y_MIN
    load 'ticstep.gp'
    
    print sprintf('ticstep = %f', ticstep)
    numtics = int((xr / ticstep) + 1)
    print sprintf('numtics = %d', numtics)
    

    【讨论】:

    • 有了 ticstep.gn,你拯救了我的一天。再次 :) 它帮助我生成自定义 ytics
    • @taiko 太好了,对你有帮助
    • 是的。作为提问者,我需要生成更多具有不同数据范围的图。 Autoscale 的工作令我不满意。使用 STATS_min、STATS_max 和您的 ticstep 函数的一些微调,我能够做到“set ytics STATS_min,ticstep, STATS_max”。
    • @Christoph,为了完整起见,请注意floor(log10(xr)) 并不总是给您预期的数量级(请参阅stackoverflow.com/q/55130265/7295599)。好吧,这不是悲剧,在最坏的情况下你会得到太多的抽动标签;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 2010-10-22
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多