gnuplot入门教程之2D绘图

  1. plot sin(x)  

产生图1结果 ——以曲线绘出三角函数 sin(x)。

图 1 Plotting sin(x)

        假设我们只想看到一个正弦曲线周期。我们通过限制图的默认 x 范围来完成此操作。使用表示法 [min:max] 来指定范围。要仅指定最小值,使用 [min:];要仅指定最大值,使用 [:max]。数学上称此为所谓的“闭”区间表示法。


例 2 从 -pi 到 +pi  的 sin(x) 和 cos(x)。

  1. set xrange [-pi:pi]  
  2. replot cos(x) with points pointtype 2  

或者:

  1. plot [-pi:pi] sin(x), cos(x) with points pointtype 2  


        我们刚才使用了 replot 命令,它执行先前的plot 命令。当您绘制曲线图且需要不断对该图进行修改以添加想要的特征时,此命令会非常有用。另外,replot 使您可以添加更多的图。尝试输入 replot cos(x)。依照语法,该命令等同于 plot sin(x), cos(x)。replot  就是获取先前的绘图字符串,添加必要的逗号,然后附加输入给它的其余部分。


例 3 将数据文件中的数据画出

  1. plot sin(x), ‘1.dat’  

其中1.dat 为一数据文件,每一行描述一点坐标位置。 内容如下,其中 # 后面的内容为注释:

  1. # $Id: 1.dat,v 1.1.1.1 1998/04/15 19:16:40 lhecking Exp $  
  2. -20.000000 -3.041676  
  3. -19.000000 -3.036427  
  4. -18.000000 -3.030596  
  5. -17.000000 -3.024081  
  6. -16.000000 -3.016755  
  7. -15.000000 -3.008456  
  8. ……  

例 4 命名图和坐标轴

  1. set title 'My first graph'  
  2. set xlabel 'Angle, in degrees'  
  3. set ylabel 'sin(angle)'  
  4. plot sin(x)  

现在,我们注意到 x 轴实际没有标记为度数,看起来不是很好。要修改此问题,可以通过调整 x 轴上的 tic 标记。


例 5 改变轴上 tic 并设置网格

  1. set title "My first graph"  
  2. set xrange [-pi:pi]  # we want only one cycle  
  3. set xtics ('0' 0, '90' pi/2, '-90' -pi/2, '45' pi/4,'-45' -pi/4,'135' 3*pi/4,'-135' -3*pi/4)  
  4. set grid  
  5. set xlabel 'Angle, in degrees'  
  6. set ylabel 'sin(angle)'  
  7. plot sin(x)   

gnuplot 还允许您指定绘图的样式,以便获得进一步的控制。


例 6 多条曲线

  1. 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 定制图的关键字或图例

  1. set key top left  
  2. set key box  
  3. plot [-pi:pi] sin(x) title 'sinusoid' with linespoints pointtype 5, cos(x) t 'cosine' w boxes lt 4  

        上面,我们在同一图中绘制了正弦和余弦曲线。gnuplot 使您还可以绘制多个图,这样它们可以并排显示在同一输出屏幕或文件中。在某些排版系统中,以一个文件的形式包含两个图形比分别包含两个图形要更容易。


例 8 Multiplot 示例

  1. set xrange [-pi:pi]  
  2. # gnuplot recommends setting the size and origin before going to multiplot mode  
  3. # This sets up bounding boxes and may be required on some terminals  
  4. set size 1,1  
  5. set origin 0,0  
  6.   
  7. # Done interactively, this takes gnuplot into multiplot mode  
  8. set multiplot  
  9.   
  10. # plot the first graph so that it takes a quarter of the screen  
  11. set size 0.5,0.5  
  12. set origin 0,0.5  
  13. plot sin(x)  
  14.   
  15. # plot the second graph so that it takes a quarter of the screen  
  16. set size 0.5,0.5  
  17. set origin 0,0  
  18. plot 1/sin(x)  
  19.   
  20. # plot the third graph so that it takes a quarter of the screen  
  21. set size 0.5,0.5  
  22. set origin 0.5,0.5  
  23. plot cos(x)  
  24.   
  25. # plot the fourth graph so that it takes a quarter of the screen  
  26. set size 0.5,0.5  
  27. set origin 0.5,0  
  28. plot 1/cos(x)  
  29.   
  30. # On some terminals, nothing gets plotted until this command is issued  
  31. unset multiplot  
  32. # remove all customization  
  33. reset  
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值