> 文章列表 > Gnuplot绘图入门2——根据多列文本数据绘制图形

Gnuplot绘图入门2——根据多列文本数据绘制图形

Gnuplot绘图入门2——根据多列文本数据绘制图形

Gnuplot绘图入门2——根据多列文本数据绘制图形

Gnuplot绘图入门1以绘制sin(x)的函数图形为例,对Gnuplot进行了简要介绍。这个教程将介绍如何使用Gnuplot对保存在文本文件(.txt、.dat文件)中的数据进行可视化。

将下面的数据复制下了,保存到文本文件中,并命名为myData.dat以供后续使用。

  # x              y                 error0.000000000      8.52344856E-02    160.000000    0.322214633      0.315360963       160.000000    0.644429266      0.660322666       160.000000    0.966643929      0.810025394       160.000000    1.28885853      0.895393252       160.000000    1.61107314       1.07085896       160.000000    1.93328786       1.16645670       160.000000    2.25550246      0.890256882       160.000000    2.57771707      0.467355251       160.000000    2.89993167      0.335340381       160.000000    3.22214627      -4.83360216E-02   160.000000    3.54436088     -0.234565780       160.000000    3.86657572     -0.573700428       160.000000    4.18879032     -0.930545390       160.000000    4.51100492      -1.04824042       160.000000    4.83321953     -0.968299508       160.000000    5.15543413     -0.829686761       160.000000    5.47764874     -0.713711143       160.000000    5.79986334     -0.537595809       160.000000    6.12207794     -0.256095201       160.000000    6.44429255      0.227042556       160.000000    6.76650715      0.379589826       160.000000    7.08872175      0.726616621       160.000000    7.41093636      0.922940135       160.000000    7.73315144      0.934689820       160.000000    8.05536556      0.938522339       160.000000    8.37758064      0.751247168       160.000000    8.69979477      0.695001900       160.000000    9.02200985      0.440352410       160.000000    9.34422398      0.115799539       160.000000    9.66643906     -0.290482402       160.000000    9.98865318     -0.574512541       160.000000    10.3108683     -0.716142476       160.000000    10.6330833     -0.979006410       160.000000    10.9552975     -0.952903807       160.000000    11.2775126     -0.896204770       160.000000    11.5997267     -0.819699407       160.000000    11.9219418     -0.601184964       160.000000    12.2441559     -0.320398450       160.000000    12.5663710      8.72830823E-02    160.000000    

先绘制前两列数据。打开Gnuplot,输入

plot "myData.dat" linetype 7 linecolor -1

在这里插入图片描述

“linetype”表示线型,“7”是线型编号,输入文本数据(这里是“myData.dat”)对应红色点线,输入函数名称(如“sin(x)”对应红色实线,可以简写为lt,“linecolor”表示颜色可以简写为lc,颜色编号-1对应黑色,编号7表示红色。对于多列数据,plot命令默认使用前两列绘制图形。绘制的结果如下

在这里插入图片描述

这个x和y的关系很像正弦函数,接下来绘制在图上绘制正弦函数,命令如下:

plot "myData.dat" title "My Data" lt 7 lc -1, sin(x) lc 7 title "Model"

在这里插入图片描述

plot一幅图中绘制多个图形时,用英文逗号隔开每个图形的设置语句就可以绘制多组数据了。

接下来,设置x坐标范围、x和y轴的标签,命令如下:

set xrange [-1:13]  # 设置x坐标范围
set xlabel "t/s"    # 设置x轴标签
set ylabel "Water level in meters"  # 设置y轴标签
replot  # 应用设置

在这里插入图片描述

下面将上图保存为png格式,分别输入如下命令

set terminal png size 800,600  # 将terminal类型改为png,使用“size 800,600”设置图片大小
set output "dataImg.png"  # 设置图片名称
replot  # 将图像保存在dataImg.png中

在这里插入图片描述

得到的dataImg.png如下

在这里插入图片描述

下面开始绘制误差条。先将terminal类型改为原本默认的类型qt(windows默认的好像是wxt),然后使用三列数据绘制误差条。

set terminal qt
replotplot "myData.dat" using 1:2:($3/1000) title "My Data" lt 7 lc -1 with errorbars, sin(x) lc 7 title "Model"

“using 1:2:($3/1000)”表示使用第1、2和3列的数据绘制误差条,绘制前先将第三列数据除以1000,绘制结果如下

。如果将“using 1:2:($3/1000)”换成“using 1:2:3”,则第三列数据使用原始数据绘图。

在这里插入图片描述

这里给出绘制误差条并保持为png图片的完整程序脚本

set term png size 800,600  # 为了导出png格式图片,设置终端类型为pngcairo(可以简写为png)
set output "b.png" # 绘图后导出名为a.png的图片,这一行和上面一行都要放在plot之前set xrange [-1:13]  # 设置x坐标范围
set xlabel "t/s"    # 设置x轴标签
set ylabel "Water level in meters"  # 设置y轴标签plot "myData.dat" using 1:2:($3/1000) title "My Data" lt 7 lc -1 with errorbars, sin(x) lc 7 title "Model" #使用第1、2和3列的数据绘制误差条,绘制前先将第三列数据除以1000

将以上程序保存为b.plt,在命令行运行gnuplot b.plt,就可以得到本文最后绘制的误差条图。

注意,需要在系统终端中运行gnuplot b.plt,而非gnuplot的终端中运行。
在这里插入图片描述

参考

  1. Gnuplot教程1: 基本的绘图技巧,误差条,png输出