gnuplot 用不同的符号绘制每个系列
gnuplot ploting each series with different symbol
我有一个不同系列的文件,例如:
x0_1 y0_1 A_0
x0_2 y0_2 B_0
...
x0_n y0_n N_0
x1_1 y1_1 A_1
x1_2 y1_2 B_1
...
x1_n y1_n N_1
如果我这样做 plot "data.txt" u 1:2:3 w lp lc palette
我可以用不同的线条绘制所有系列。但是如何为每一行放置不同的符号?
您的数据被两个空行分成不同的系列。如果你给每个系列一个索引号0、1、2,你可以通过using
规范中的column(-2)
访问索引号。 column(-2)
在 gnuplot 中称为 'pseudocolumns'。请参阅 help psuedocolumns
了解详情。
这是考虑到这一点的示例脚本。
set key noautotitle
set xrange [0:5]
set yrange [0:7]
plot "test.dat" using 1:2:(column(-2)+1):(column(-2)+1)
with linespoints ps 3 pt variable lc variable
在这个例子中,点类型和线条颜色是按顺序分配的。但是,如果您的 gnuplot 是支持数组的版本,您也可以根据需要设置点类型和线条颜色,如下所示。
array pointtype[3] = [5, 7, 9]
array linecolor[3] = [1, 4, 6]
plot "test.dat" using 1:2:(pointtype[column(-2)+1]):(linecolor[column(-2)+1])
with linespoints ps 3 pt variable lc variable
我使用了这样的示例数据,
1 1 5
2 1 6
3 2 4
4 3 5
1 2 7
2 3 5
3 3 4
4 4 2
1 3 6
2 4 4
3 4 3
4 6 2
加法:图例
在此图中,无法以简单的方式显示单行的图例(或键)。如果您使用的是 5.2.6 或更高版本,则可以使用 keyentry 样式。
set key noautotitle
array pointtype[3] = [5, 7, 9]
array linecolor[3] = [1, 4, 6]
plot "test.dat" using 1:2:(pointtype[column(-2)+1]):(linecolor[column(-2)+1]) \
with linespoints ps 3 pt variable lc variable, \
for [i=0:2] keyentry with linespoints ps 3 pt pointtype[i+1] lc linecolor[i+1] \
title sprintf("series %i", i)
为了完整起见,这里有一个版本甚至适用于 gnuplot 4.6,并且即使在 gnuplot 4.4 中也有一些小的改编。您可以使用 index
来寻址块(参见 help index
)。
代码:(使用@binzo的test.dat
)
### plot different sets (works with gnuplot 4.6)
reset
myColor(i) = word("1 2 3",i+1)
myPts(i) = word("5 7 9", i+1)
set key top left
stats 'test.dat' u 1 nooutput # get the number of blocks
plot for [i=0:int(STATS_blocks)-1] 'test.dat' u 1:2 index i \
w lp ps 3 pt myPts(i) lc myColor(i) title sprintf("series %i", i)
### end of code
结果: (gnuplot 4.6)
答案已经被接受,但请允许我在这里放另一个(稍微简单一些的)答案。
您可以通过在 plot 命令中指定 index N
来成功访问每个系列的数据(我在之前的回答中忘记了这一点)。这使您可以将数据视为单个文件中有多个数据集。
脚本看起来像这样,
set key noautotitle
array pointtype[3] = [5, 7, 9]
array linecolor[3] = [1, 4, 6]
plot for [i=0:2] "test.dat" index i using 1:2 \
with linespoints ps 3 pt pointtype[i+1] lc linecolor[i+1] \
title sprintf("series %i", i)
使用您的 gnuplot 版本,您仍然能够以这种方式显示包括图例在内的情节。
我有一个不同系列的文件,例如:
x0_1 y0_1 A_0
x0_2 y0_2 B_0
...
x0_n y0_n N_0
x1_1 y1_1 A_1
x1_2 y1_2 B_1
...
x1_n y1_n N_1
如果我这样做 plot "data.txt" u 1:2:3 w lp lc palette
我可以用不同的线条绘制所有系列。但是如何为每一行放置不同的符号?
您的数据被两个空行分成不同的系列。如果你给每个系列一个索引号0、1、2,你可以通过using
规范中的column(-2)
访问索引号。 column(-2)
在 gnuplot 中称为 'pseudocolumns'。请参阅 help psuedocolumns
了解详情。
这是考虑到这一点的示例脚本。
set key noautotitle
set xrange [0:5]
set yrange [0:7]
plot "test.dat" using 1:2:(column(-2)+1):(column(-2)+1)
with linespoints ps 3 pt variable lc variable
在这个例子中,点类型和线条颜色是按顺序分配的。但是,如果您的 gnuplot 是支持数组的版本,您也可以根据需要设置点类型和线条颜色,如下所示。
array pointtype[3] = [5, 7, 9]
array linecolor[3] = [1, 4, 6]
plot "test.dat" using 1:2:(pointtype[column(-2)+1]):(linecolor[column(-2)+1])
with linespoints ps 3 pt variable lc variable
我使用了这样的示例数据,
1 1 5
2 1 6
3 2 4
4 3 5
1 2 7
2 3 5
3 3 4
4 4 2
1 3 6
2 4 4
3 4 3
4 6 2
加法:图例
在此图中,无法以简单的方式显示单行的图例(或键)。如果您使用的是 5.2.6 或更高版本,则可以使用 keyentry 样式。
set key noautotitle
array pointtype[3] = [5, 7, 9]
array linecolor[3] = [1, 4, 6]
plot "test.dat" using 1:2:(pointtype[column(-2)+1]):(linecolor[column(-2)+1]) \
with linespoints ps 3 pt variable lc variable, \
for [i=0:2] keyentry with linespoints ps 3 pt pointtype[i+1] lc linecolor[i+1] \
title sprintf("series %i", i)
为了完整起见,这里有一个版本甚至适用于 gnuplot 4.6,并且即使在 gnuplot 4.4 中也有一些小的改编。您可以使用 index
来寻址块(参见 help index
)。
代码:(使用@binzo的test.dat
)
### plot different sets (works with gnuplot 4.6)
reset
myColor(i) = word("1 2 3",i+1)
myPts(i) = word("5 7 9", i+1)
set key top left
stats 'test.dat' u 1 nooutput # get the number of blocks
plot for [i=0:int(STATS_blocks)-1] 'test.dat' u 1:2 index i \
w lp ps 3 pt myPts(i) lc myColor(i) title sprintf("series %i", i)
### end of code
结果: (gnuplot 4.6)
答案已经被接受,但请允许我在这里放另一个(稍微简单一些的)答案。
您可以通过在 plot 命令中指定 index N
来成功访问每个系列的数据(我在之前的回答中忘记了这一点)。这使您可以将数据视为单个文件中有多个数据集。
脚本看起来像这样,
set key noautotitle
array pointtype[3] = [5, 7, 9]
array linecolor[3] = [1, 4, 6]
plot for [i=0:2] "test.dat" index i using 1:2 \
with linespoints ps 3 pt pointtype[i+1] lc linecolor[i+1] \
title sprintf("series %i", i)
使用您的 gnuplot 版本,您仍然能够以这种方式显示包括图例在内的情节。