如何在gnuplot中使用列标题设置坐标轴标签?

16

我的问题非常简单。假设我有一个带有列标题的数据文件,如下所示:

first second
1 1 
2 1
3 6
4 9
在gnuplot中,如何使数据文件的列标题作为轴标签绘制?例如,通过调用什么命令来实现?
plot datafile using 1:2

我如何将x轴标记为first,y轴标记为second

编辑:我知道可以使用列标题作为关键条目通过set key auto title column head,但这不完全是我要找的。

4个回答

8

为了更详细地说明@andyras的建议,以下是您可以实现的方法:

datafile = 'filename.txt'
firstrow = system('head -1 '.datafile)
set xlabel word(firstrow, 1)
set ylabel word(firstrow, 2)
plot datafile using 1:2

你必须使用显式的 using 语句进行绘图,否则 gnuplot 会抱怨 bad data on line 1

1
我认为gnuplot没有内置此功能;您可能需要使用类似awk的实用程序从数据文件中提取这些标签。 您可以尝试在gnuplot的sourceforge网站上提交一个功能请求,并从开发人员那里获得反馈。

1
awk + head -1 就可以了。 - mgilson

0

另一个人们认为无法使用gnuplot完成的问题。这里有一个简单的仅使用gnuplot解决方案,不需要外部工具,因此跨平台。

数据:SO16089301.dat

first second
1 1 
2 1
3 6
4 9
脚本:(适用于gnuplot 4.6.0,2012年3月)
### extracting axes labels from datafile
reset

FILE = 'SO16089301.dat'

stats FILE u (myX=strcol(1), myY=strcol(2)) every ::0::0 nooutput

set xlabel myX
set ylabel myY

plot FILE u 1:2 w lp pt 7 lc rgb "red"
### end of script

结果:

enter image description here


-2

我认为它是被支持的。你只需要能够使用双引号:

plot 'file' using "first":"second"

不过,如果你想在using规范中进行数学计算,你也需要使用column("")函数

plot 'file' using "first":(column("second")-(column("thrid"))

(对于我来说,仅使用带引号的标题名称进行计算并不起作用)


1
使用您的方法,可以通过标题字符串选择列,而不是使用1和2,但这些标题不用作轴标签。 - Christoph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接