从csv中读取gnuplot图例

sas*_*llo 6 gnuplot

我有一个data.csv文件,结构如下:

n    John Smith stats     Sam Williams stats
1                23.4                   44.1
2                32.1                   33.5
3                42.0                   42.1
Run Code Online (Sandbox Code Playgroud)

目前我正在使用gnuplot中的以下命令进行绘图:

plot 'data.csv' using 1:2 title 'John' with lines, '' using 1:3 title 'Sam' with lines
Run Code Online (Sandbox Code Playgroud)

问题是如何从.csv的第一行检索名字而不是手动输入它们?

另外,如果我在表格中添加一列,是否可以调整它,所以它会自动添加另一行具有相应标题的行?

Tho*_*hor 9

你说你有一个csv文件,所以我假设你的数据文件是这样的(并保存在infile.csv中):

n,John Smith stats,Sam Williams stats
1,23.4,44.1
2,32.1,33.5
3,42.0,42.1
Run Code Online (Sandbox Code Playgroud)

如果您的Gnuplot版本足够新,您可以使用columnhead以下title参数:

echo "
  set datafile separator ','
  plot 'infile.csv' using 1:2 with lines title columnhead
" | gnuplot --persist
Run Code Online (Sandbox Code Playgroud)

或使用key选项:

echo "
  set datafile separator ','
  set key autotitle columnhead
  plot 'infile.csv' using 1:2 with lines, '' using 1:3 with lines
" | gnuplot --persist
Run Code Online (Sandbox Code Playgroud)

编辑 - 缩短标题

echo "
  set datafile separator ','
  set key autotitle columnhead
  plot '< sed -r \"1 s/,([^ ]+)[^,]+/,\1/g\" infile.csv' using 1:2 with lines, '' using 1:3 with lines
" | gnuplot --persist
Run Code Online (Sandbox Code Playgroud)

输出:

infile.csv的第1列:2和1:3的图

注意这个答案的后续问题也可能是相关的.