用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
几年前写的一篇短文,今天找东西时翻出来了,感觉写的还是比较实用的。放在这里供有需要的人参考。 现在在网上可以找的科学作图软件有不少,其中不乏优秀者,Win平台上有大名鼎鼎的Origin、Tecplot、SigmaPlot等,类UNIX上有LabPlot、gnuplot等。其他的常见的计算软件如Matlab、Mathematica、Maple、Scilab、IDL、Maxima等也都对科学作图有很好的支持,甚至于只要稍加努力使用MS的Excel也可以做出不错的图来。但是笔者最喜爱的工具还是gnuplot。gnuplot 是典型的UNIX 哲学的产物,小巧,灵活,擅长于与其他工具协同工作。本人使用gnuplot 已经有1年有余,积累了些经验,下面的内容主要是我1年多来使用gnuplot所做笔记的一个总结,当然,在成稿过程中也将gnuplot 的手册又来回翻了几遍。 gnuplot是一个命令行驱动的科学绘图工具,可将数学函数或数值资料以平面图或立体图的形式画在不同种类终端机或绘图输出装置上。它是由Colin Kelley 和 Thomas Williams于1986年开发的绘图程序发展而来的,可以在多个平台下使用。gnuplot既支持命令行交互模式,也支持脚本。 gnuplot 的主要作者包括:Thomas Williams, Colin Kelley, Russell Lang, Dave Kotz, John Campbell, Gershon Elber, Alexander Woo 和许多其他人。 gnuplot 与自由软件基金会和GNU 项目没有任何直接的关系,因此将 gnuplot 称为 GNUplot 是不对的。gnuplot 是完全独立的设计和开发的,命名为 gnuplot 只是种妥协,当时Thomas 希望他们开发的程序叫做 "llamaplot" 而Colin 更喜欢 "nplot" 这个名字。后来他们都同意命名他们的程序为 "newplot",可是很快他们发现有个Pascal 语言写的程序叫这个名字,而且这个程序偶尔会被提到。最后 Thomas 想出了"gnuplot" 这个双方都可以接受的名字。  gnuplot 在互联网上的主页地址是:[www.](http://www.gnuplot.info/)gnuplot.info , 源代码和可执行程序也可以到[http://sourceforge.net/projects/](http://sourceforge.net/projects/gnuplot)gnuplot 上下载。 gnuplot有详细的使用手册,如果用户使用的是GNU/Linux操作系统,可以察看 /usr/share/doc/gnuplot目录下的gnuplot.html文件。需要PDF版本的使用手册可以到[http://sourceforge.net/projects/](http://sourceforge.net/projects/gnuplot)gnuplot 下载。 本文中所引用范例皆整理自 gnuplot demo。文中的图例亦引用自子目录 demo 中的范例。 # 快速入门 本文全文都以GNU/Linux 下的 gnuplot 4.0版本为例,如果读者使用的是其他版本的操作系统,个别操作细节请做相应调整。 在命令行模式下输入 gnuplot,即可看到如下的信息,其中省略号处省略了部分信息: ~~~ G N U P L O T Version 4.0 patchlevel 0 last modified Thu Apr 15 14:44:22 CEST 2004 System: Linux 32 bit …… Terminal type set to 'x11' gnuplot> ~~~ ‘gnuplot>’即是 gnuplot 的命令行提示符,gnuplot 的所有命令都在这个提示符下输入。 ### 2D 绘图 请看例 1: ~~~ plot sin(x) ~~~ 产生图 2结果 ——以曲线绘出三角函数 sin(x)。  ![](https://box.kancloud.cn/2016-01-24_56a4234051801.PNG) 图 1 Plotting sin(x) 假设我们只想看到一个正弦曲线周期。我们通过限制图的默认 x 范围来完成此操作。使用表示法 [min:max] 来指定范围。要仅指定最小值,使用 [min:];要仅指定最大值,使用 [:max]。数学上称此为所谓的“闭”区间表示法。 例 2从 -pi 到 +pi 的 sin(x) 和 cos(x) ~~~ set xrange [-pi:pi] replot cos(x) with points pointtype 2 ~~~ 或者: ~~~ plot [-pi:pi] sin(x), cos(x) with points pointtype 2 ~~~ ![](https://box.kancloud.cn/2016-01-24_56a42340637ce.PNG) 我们刚才使用了 replot 命令,它执行先前的 plot 命令。当您绘制曲线图且需要不断对该图进行修改以添加想要的特征时,此命令会非常有用。另外,replot 使您可以添加更多的图。尝试输入 replot cos(x)。依照语法,该命令等同于 plot sin(x), cos(x)。replot 就是获取先前的绘图字符串,添加必要的逗号,然后附加输入给它的其余部分。 例 5 将数据文件中的数据画出: ~~~ plot sin(x), ‘1.dat’ ~~~ 其中1.dat 为一数据文件,每一行描述一点坐标位置。 内容如下,其中 # 后面的内容为注释:  ~~~ # $Id: 1.dat,v 1.1.1.1 1998/04/15 19:16:40 lhecking Exp $ -20.000000 -3.041676 -19.000000 -3.036427 -18.000000 -3.030596 -17.000000 -3.024081 -16.000000 -3.016755 -15.000000 -3.008456 …… ~~~ ![](https://box.kancloud.cn/2016-01-24_56a42340742ea.PNG) 图 2 Plotting sin(x), data file – 1.dat 例 6命名图和坐标轴: ~~~ set title 'My first graph' set xlabel 'Angle, in degrees' set ylabel 'sin(angle)' plot sin(x) ~~~ ![](https://box.kancloud.cn/2016-01-24_56a4234082bf5.PNG) 现在,我们注意到 x 轴实际没有标记为度数,看起来不是很好。要修改此问题,可以通过调整 x 轴上的 tic 标记。 例 7 改变轴上 tic 并设置网格: ~~~ set title "My first graph" set xrange [-pi:pi]  # we want only one cycle set xtics ('0' 0, '90' pi/2, '-90' -pi/2, '45' pi/4,'-45' -pi/4,'135' 3*pi/4,'-135' -3*pi/4) set grid set xlabel 'Angle, in degrees' set ylabel 'sin(angle)' plot sin(x) ~~~ ![](https://box.kancloud.cn/2016-01-24_56a42340904f3.PNG) gnuplot 还允许您指定绘图的样式,以便获得进一步的控制。 例 8 多条曲线 ~~~ plot sin(x) with linespoints pointtype 5, cos(x) w boxes lt 4 ~~~ ![](https://box.kancloud.cn/2016-01-24_56a423409ed72.PNG) with 子句使您可以详细而精确地指定线的样式。在本例中,我们说明两种有用的样式。第一种样式 linespoints 通常在对数据绘图时非常有用,它在涉及的每个示例或数据点处标记一个点,并使用线性插值法连接连续的点。这里我们另外指定点类型为 5,它选择终端允许的第五种点。第二种样式 boxes 更适合绘制直方图数据。注意我们如何在 cos(x) 曲线中将 with 缩写成 w。类似地,lt 是 linetype 的缩写,是另一个特定于终端的设置,它选择终端可以绘制的四种线。不必说,您可以使用 pt 代替冗长的 pointtype。如果想在多条线中使用相同的绘图样式(在一个 plot 命令中或在多个 plot 命令中),可以使用 set 命令设置绘图样式:set style function linespoints。要更改用于绘制与函数相对的数据集合的样式,使用 set style data linespoints。 当绘制两条或多条曲线时,我们需要关键字或图例来对它们进行区分。默认情况下,关键字在右上角;但是如果它妨碍了图,可以将关键字放到其他位置 。如果愿意,甚至可以放到图外。 例 9 定制图的关键字或图例 ~~~ set key top left set key box plot [-pi:pi] sin(x) title 'sinusoid' with linespoints pointtype 5, cos(x) t 'cosine' w boxes lt 4 ~~~ ![](https://box.kancloud.cn/2016-01-24_56a42340af711.PNG) 上面,我们在同一图中绘制了正弦和余弦曲线。gnuplot 使您还可以绘制多个图,这样它们可以并排显示在同一输出屏幕或文件中。在某些排版系统中,以一个文件的形式包含两个图形比分别包含两个图形要更容易。 例 10 Multiplot 示例: ~~~ set xrange [-pi:pi] # gnuplot recommends setting the size and origin before going to multiplot mode # This sets up bounding boxes and may be required on some terminals set size 1,1 set origin 0,0 # Done interactively, this takes gnuplot into multiplot mode set multiplot # plot the first graph so that it takes a quarter of the screen set size 0.5,0.5 set origin 0,0.5 plot sin(x) # plot the second graph so that it takes a quarter of the screen set size 0.5,0.5 set origin 0,0 plot 1/sin(x) # plot the third graph so that it takes a quarter of the screen set size 0.5,0.5 set origin 0.5,0.5 plot cos(x) # plot the fourth graph so that it takes a quarter of the screen set size 0.5,0.5 set origin 0.5,0 plot 1/cos(x) # On some terminals, nothing gets plotted until this command is issued unset multiplot # remove all customization reset ~~~ ![](https://box.kancloud.cn/2016-01-24_56a42340c009a.PNG)