【问题标题】:why does gnuplot label min/max values incorrectly?为什么 gnuplot 错误地标记最小/最大值?
【发布时间】:2016-04-14 05:49:59
【问题描述】:

我正在创建一个图,在其中标记双时间序列的最小值和最大值。

我通过定义 minmax 函数来做到这一点,并绘制每个系列以及每个时间点的较高和较低值的标签。标签上下偏移,因此不会与线条重叠。

请注意,无论第一列还是第二列中的值更高,标签始终是第二列中的值。

主要问题似乎是 min/max 函数只能用于绘制点,但不能用于选择标签。

为什么会这样?

这是我的 MWE:

set terminal pdfcairo size 3,2
set output 'output.pdf'

# min/max functions
min(x, y) = x > y ? y : x
max(x, y) = x > y ? x : y

set style data lines

# nice ranges
set xr [-1:5]
set yr [-1:3]

plot 'data.dat' u 0:1 title 'col 1', \
  '' u 0:2 title 'col 2', \
  '' u 0:(min($1,$2)) title 'min', \
  '' u 0:(max($1,$2)) title 'max', \
  '' u 0:(min($1,$2) - 0.2):(min($1,$2)) notitle with labels, \
  '' u 0:(max($1,$2) + 0.2):(max($1,$2)) notitle with labels

和数据文件:

1 2
2 0
1 2
1 0
0 1

【问题讨论】:

  • 我认为您必须明确使用sprintf 来格式化标签:plot 'data.dat' u 0:(min($1,$2) - 0.2):(sprintf('%.0f', min($1,$2))) notitle with label
  • 如果您将评论作为答案,我会接受。那行得通!我曾怀疑它与字符串/数字比较有关,而我与 int 之间进行转换的实验没有成功。
  • 我不确定,但现在我还找到了一个旧的、被拒绝的错误报告的链接。

标签: gnuplot


【解决方案1】:

您必须明确使用sprintf 来格式化标签:

plot 'data.dat' u 0:(min($1,$2) - 0.2):(sprintf('%.0f', min($1,$2))) notitle with label

对于某些特殊情况,转换为字符串是自动完成的,但不保证能正常工作。只有sprintf 才能确保您的安全。也可以看看 https://sourceforge.net/p/gnuplot/bugs/1368/ 对此行为进行更长时间的讨论。

【讨论】:

    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 2019-03-19
    • 2020-09-06
    • 2015-07-19
    • 2015-05-19
    • 2021-07-01
    相关资源
    最近更新 更多