从csv读取gnuplot图例 [英] Reading gnuplot legend from csv

查看:47
本文介绍了从csv读取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

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

plot 'data.csv' using 1:2 title 'John' with lines, '' using 1:3 title 'Sam' with lines

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

此外,如果我在表中添加一列,是否可以使其可调,因此它会自动添加另一行并带有适当的标题?

解决方案

您说您有一个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

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

echo "
  set datafile separator ','
  plot 'infile.csv' using 1:2 with lines title columnhead
" | gnuplot --persist

或使用key选项:

echo "
  set datafile separator ','
  set key autotitle columnhead
  plot 'infile.csv' using 1:2 with lines, '' using 1:3 with lines
" | gnuplot --persist

编辑-缩短标题

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

输出:

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

I've got a data.csv file which is structured like this:

n    John Smith stats     Sam Williams stats
1                23.4                   44.1
2                32.1                   33.5
3                42.0                   42.1

Currently I'm plotting with the following command in gnuplot:

plot 'data.csv' using 1:2 title 'John' with lines, '' using 1:3 title 'Sam' with lines

The question is how to retrieve first names from the first line of .csv rather than entering them manually?

In addition, is it possible to make it adjustable in case I add a column to the table, so it automatically adds another line with the appropriate title?

解决方案

You say you have a csv file, so I assume your data file looks like this (and is saved in infile.csv):

n,John Smith stats,Sam Williams stats
1,23.4,44.1
2,32.1,33.5
3,42.0,42.1

If your version of Gnuplot is recent enough, you can use columnhead as the title argument:

echo "
  set datafile separator ','
  plot 'infile.csv' using 1:2 with lines title columnhead
" | gnuplot --persist

Or use the key option:

echo "
  set datafile separator ','
  set key autotitle columnhead
  plot 'infile.csv' using 1:2 with lines, '' using 1:3 with lines
" | gnuplot --persist

Edit - shorten headings

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

Output:

Note this answer to a follow-up question may also be relevant.

这篇关于从csv读取gnuplot图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆