如何根据测试条件使用文件中的 x 坐标和 y 坐标在 gnuplot 中绘制一个点?

How to plot a point in gnuplot with x-coordinate from file and y coordinate based on a test condition?

我有一个包含 3 列数据的文件。第一列包含 x 坐标,第三列包含 y 坐标。第二列包含值(例如,假设它具有集合 {0, 1, 2} 中的值),这取决于绘制点的颜色和类型。我正在使用以下命令进行绘图:

plot "< awk '{if( == \"0\") print}' out.txt" using 1:3 title "Label 0" with points pointtype 1 lc rgb '#990000'  
     "< awk '{if( == \"1\") print}' out.txt" using 1:3 title "Label 1" with points pointtype 2 lc rgb '#990055'  
     "< awk '{if( == \"2\") print}' out.txt" using 1:3 title "Label 2" with points pointtype 3 lc rgb '#990099'

这很好用。但是现在,我想实现以下目标:

有人可以帮我解决这个问题吗?谢谢。
PS: 我是 gnuplot 的初学者。我不确定这是否是一个非常简单的问题。 提前致谢。

这是样本数据的示例和我的期望。

Sample Data:                                  Expected point:

0 0 34                                        (0, 30) Red   
0 1 10                                        (0, 10) Green  
0 2 44                                        (0, 40) Blue
1 0 50                                        (1, 50) Red
1 1 49                                        (1, 50) Green
1 2 48                                        (1, 50) Blue
2 0 46                                        (2, 50) Red
2 1 49                                        (2, 50) Green
2 2 46                                        (2, 50) Blue
3 0 45                                        (3, 50) Red
3 1 46                                        (3, 50) Green
3 2 48                                        (3, 50) Blue
4 0 68                                        (4, 70) Red
4 1 44                                        (4, 40) Green
4 2 46                                        (4, 50) Blue
5 0 43                                        (5, 40) Red
5 1 44                                        (5, 40) Green
5 2 44                                        (5, 40) Blue
6 0 43                                        (6, 40) Red
6 1 42                                        (6, 40) Green
6 2 46                                        (6, 50) Blue

首先,我认为您不需要在当前代码中使用 awk。在 gnuplot 中跳过行的一个常见技巧是使用像这样的三元运算符:

plot 'out.txt' using ( == 0 ?  : 1/0):3 title "Label 0" with points pointtype 1 lc rgb '#990000'

x坐标设置为1/0(即inf)除非第二列的值为0,这意味着数据点被跳过.

如果你想为给定的 (x, y) 对绘制一个完全不同的 y 坐标,你可以使用这样的东西:

x = 4
y = 2
Y = 10
plot 'out.txt' using ( == 0 ?  : 1/0):( == x &&  == y ? Y : ) title "Label 0" with points pointtype 1 lc rgb '#990000'

和以前一样,当第二列与所需值不匹配时,将跳过该行。我还添加了一个条件,即当第一列和第三列匹配变量 xy 时,使用 Y 的值而不是第三列。

要根据 y 的当前值执行计算,您可以使用函数 f(y) 而不是变量 Y。例如,要舍入到最接近的值 10,您可以声明此函数:

f(y) = round(y / 10) * 10

然后将 Y 替换为 f()

顺便说一句,awk 程序的结构是 condition { action },默认操作是 { print },所以如果你打算使用 awk,你可以简化为 awk ' == 0'(也没有必要引用 0).