gnuplot 用不同的线条颜色和图例绘制每个命令

gnuplot every command with different lines color and legend

假设我有这个示例数据文件

1 2
2 3
3 4

1 5
2 6
3 7

1 8
2 9
3 10

现在在 gnuplot 中,如果我 运行 这个命令

pl 'test.dat' u 1:2 every :::0::2 w l

它为数据文件中的每个块绘制了三行,但是无法区分哪一行来自哪个数据块。我希望这三行具有三种不同的颜色和不同的图例标签。我可以在 every 命令之外执行此操作吗?

当然,有多种方法可以实现。如果您坚持在块之间有一个空行并使用 every,您可以迭代绘制:

plot for [i=0:*] 'test.dat' u 1:2 every :::i::i w l lc i

或者,如果您用两个空行分隔数据块,则可以使用索引:

1 2
2 3
3 4


1 5
2 6
3 7


1 8
2 9
3 10

plot for [i=0:*] 'test.dat' index i u 1:2 w l lc i (也允许使用 i i 而不是 index i 的快捷方式,但难以阅读)

或者没有迭代,但使用伪列 -2(它给你索引号)。请注意,gnuplot 不会在用空线分隔的点之间绘制连续线,因此不需要 every 命令。

plot 'test.dat' u 1:2:-2 w l lc variable

自动生成,不同标签可以通过以下方式生成:

plot for [i=0:*] 'test.dat' i i u 1:2 w l lc i t sprintf('This is block %d', i)