使用CSV文件绘图

68

我有一个csv文件,每行有5个条目。每个条目都表示是否触发了网络数据包。每行的最后一个条目是数据包的大小。每行表示消耗的时间(毫秒)。

例如,一行如下:

1 , 0 , 1 , 2 , 117

我该如何绘制一个图表,例如X轴是行号,Y轴是每行的第一个值?

2个回答

96
这是一个好的开端:
set datafile separator ","
plot 'infile' using 0:1

@mgilson: 是的,","更好,尽管Gnuplot 4.7也支持“逗号”。 - Thor
1
就是这样。根据我所使用的机器,我通常使用4.6甚至4.4,因为4.7仍在开发中。但有趣的是他们添加了这个。不过我仍然保留它作为参考或特殊用途 :). 我想知道","comma之间是否有任何细微差别。 - mgilson
1
@mgilson:我认为没有任何区别,src/set.c:2386 读取 df_separator = ',';src/set.c:2392 读取 df_separator = sep[0]; 在我看来应该是相同的。 - Thor
根据你的评论,我也觉得它看起来一样。我从未深入研究过gnuplot源代码(也许我应该有时间去研究一下)。它是否组织良好? - mgilson
1
除了一些空格的烦恼,它似乎相当易读,尽管我还没有深入挖掘。我通过在 src/ 中使用 grep 查找 "comma" 来找到上述行 :)。 - Thor
显示剩余4条评论

17

您也可以使用免费的gnuplot将内容绘制到png文件中:

终端命令

gnuplot> set title '<title>'
gnuplot> set ylabel '<yLabel>'
gnuplot> set xlabel '<xLabel>'
gnuplot> set grid
gnuplot> set term png
gnuplot> set output '<Output file name>.png'
gnuplot> plot '<fromfile.csv>'

注意:在set output中你总是需要提供正确的扩展名(这里是.png)。

如果你的数据不连续,那么输出的结果可能也不是线性的。要解决这个问题,只需将'plot'行更改为:

plot '<Fromfile.csv>' with line lt -1 lw 2

更多线条编辑选项(破折号和线条颜色等)请查看:http://gnuplot.sourceforge.net/demo_canvas/dashcolor.html

  • 大多数Linux发行版都可以通过软件包管理器获取gnuplot(例如,在基于apt的发行版上运行apt-get install gnuplot
  • 在Windows上,可以通过Cygwin获取gnuplot
  • 在macOS上,可以通过homebrew获取gnuplot(运行brew install gnuplot

2
Gnuplot可以在Windows上不需要Cygwin的情况下使用。 - Stefan
您可以在 https://sourceforge.net/projects/gnuplot/files/gnuplot/ 下载本机 Windows 安装程序或便携式存档。 - bart

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