- plot sin(x)
产生图1结果 ——以曲线绘出三角函数 sin(x)。
图 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
我们刚才使用了 replot 命令,它执行先前的plot 命令。当您绘制曲线图且需要不断对该图进行修改以添加想要的特征时,此命令会非常有用。另外,replot 使您可以添加更多的图。尝试输入 replot cos(x)。依照语法,该命令等同于 plot sin(x), cos(x)。replot 就是获取先前的绘图字符串,添加必要的逗号,然后附加输入给它的其余部分。
例 3 将数据文件中的数据画出
- 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
- ……
例 4 命名图和坐标轴
- set title 'My first graph'
- set xlabel 'Angle, in degrees'
- set ylabel 'sin(angle)'
- plot sin(x)
现在,我们注意到 x 轴实际没有标记为度数,看起来不是很好。要修改此问题,可以通过调整 x 轴上的 tic 标记。
例 5 改变轴上 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)
gnuplot 还允许您指定绘图的样式,以便获得进一步的控制。
例 6 多条曲线
- plot sin(x) with linespoints pointtype 5, cos(x) w boxes lt 4
with 子句使您可以详细而精确地指定线的样式。在本例中,我们说明两种有用的样式。第一种样式linespoints 通常在对数据绘图时非常有用,它在涉及的每个示例或数据点处标记一个点,并使用线性插值法连接连续的点。这里我们另外指定点类型为5,它选择终端允许的第五种点。第二种样式boxes 更适合绘制直方图数据。注意我们如何在cos(x) 曲线中将with 缩写成w。类似地,lt 是linetype 的缩写,是另一个特定于终端的设置,它选择终端可以绘制的四种线。不必说,您可以使用 pt 代替冗长的pointtype。如果想在多条线中使用相同的绘图样式(在一个plot 命令中或在多个plot 命令中),可以使用set 命令设置绘图样式:set style function linespoints。要更改用于绘制与函数相对的数据集合的样式,使用set style data linespoints。
当绘制两条或多条曲线时,我们需要关键字或图例来对它们进行区分。默认情况下,关键字在右上角;但是如果它妨碍了图,可以将关键字放到其他位置 。如果愿意,甚至可以放到图外。
例 7 定制图的关键字或图例
- 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
上面,我们在同一图中绘制了正弦和余弦曲线。gnuplot 使您还可以绘制多个图,这样它们可以并排显示在同一输出屏幕或文件中。在某些排版系统中,以一个文件的形式包含两个图形比分别包含两个图形要更容易。
例 8 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