gnuplot绘图案例

案列1降水量:

datafile.dat

### 文件开始###

# 北京月平均降水量(mm)

#

# 月份降水量

# ==============

1    2.5

2    5.1

3    10.2

4    25.4

5    27.9

6    71.1

7    175.3

8    182.9

9    48.3

10   17.8

11    5.1

12    2.5

### 文件结束###

gnuplot> set xlabel "月份"  

gnuplot> set ylabel "降水量(毫米)"  

gnuplot> set title "北京市月平均降水量"  

gnuplot> unset key //不显示右上角图例,set key显示图例(默认)

gnuplot> set xrange [0.5:12.5]

gnuplot> set xtics 1,1,12

gnuplot> set ytics 0,20,200

gnuplot> plot "datafile.dat"

gnuplot> plot "datafile.dat" with lines

gnuplot> plot "datafile.dat" with linespoints linecolor 3 linewidth 2 pointtype 7 pointsize 2

gnuplot> plot "datafile.dat" w lp lc 3 lw 2 pt 7 ps 2  //上边的缩写形式

案列2:各城市降水量

precipitation.dat

### 文件开始###

# 各城市月平均降水量(mm)

#

# 月份北京上海

# ======================

1 2.5 38.1

2 5.1 58.4

3 10.2 81.3

4 25.4 101.6

5 27.9 114.3

6 71.1 152.4

7 175.3 129.5

8 182.9 132.1

9 48.3 154.9

10 17.8 61.0

11 5.1 50.8

12 2.5 35.6

### 文件结束###

gnuplot> set xlabel "月份"  

gnuplot> set ylabel "降水量(毫米)"  

gnuplot> set title "各城市月平均降水量"  

gnuplot> plot "precipitation.dat" u 1:2 w lp pt 5 title "北京",\

                   "precipitation.dat" u 1:3 w lp pt 7 title "上海"

案例3:生成eps文件

gnuplot> set terminal postscript eps

gnuplot> set xlabel "月份"  

gnuplot> set ylabel "降水量(毫米)"  

gnuplot> set title "各城市月平均降水量"  

gnuplot> set xrange [0.5:12.5]

gnuplot> set xtics 1,1,12

gnuplot> set ytics 0,20,200gnuplot> set output "precipitation.eps"    //设置输出文件 

gnuplot> plot "precipitation.dat" u 1:2 w lp pt 5 title "北京",\

                   "precipitation.dat" u 1:3 w lp pt 7 title "上海"

gnuplot> set output    //切回默认输出
gnuplot> set term wxt  //设置切回屏幕输出方式

==带颜色版

 

gnuplot> set terminal postscript eps color solid linewidth 2 "Helvetica" 20

 

gnuplot> set output "precipitation-color.eps"

 

gnuplot> replot

 

gnuplot> set output

gnuplot> set term wxt  

==生成pdf

gnuplot> set term pdfcairo lw 2 font "Times_New_Roman,8"

 

gnuplot> set output "precipitation.pdf"

 

gnuplot> replot

 

gnuplot> set output

gnuplot> set term wxt  

==生成png

gnuplot> set term pngcairo lw 2 font "AR_PL_UKai_CN,14" 

 

gnuplot> set output "precipitation.png"

 

gnuplot> replot

 

gnuplot> set output

gnuplot> set term wxt  

 

案例4:降水和气温同时显示

weather_beijing.dat:

### 文件开始###

# 北京月平均降水量(毫米)及气温(摄氏度)

#

# 月份降水量气温

# ===================

1 2.5 -4

2 5.1 -2

3 10.2 6

4 25.4 13

5 27.9 20

6 71.1 24

7 175.3 26

8 182.9 25

9 48.3 20

10 17.8 13

11 5.1 5

12 2.5 -2

### 文件结束### 

gnuplot> set xlabel "月份"

gnuplot> set ylabel "降水量(毫米)"

gnuplot> set y 2 l a b e l "气温(摄氏度)"

gnuplot> set t i t l e "北京市月平均降水量及气温"

gnuplot> set xrange [ 0 . 5 : 1 2 . 5 ]

gnuplot> set xtics 1 ,1 ,12

gnuplot> set ytics 0,20,200

gnuplot> plot "weather_beijing.dat " u 1:2 w lp pt 5lc rgbcolor "#2B60DE"\

> axis x1y1 t "降水量" , "weather_beijing.dat " u 1:3 w lp pt 7> lc rgbcolor "#F62817" axis x1y2 t "气温"

//要想右边y轴有效果,需要以下设置

gnuplot> set ytics nomirror

gnuplot> set y2tics

gnuplot> replot 

image2016-8-31%2014%3A11%3A42.png?version=1&modificationDate=1472624661000&api=v2

//添加栅格(需注意,左右两条y轴,两边的格数相等才可以)

gnuplot> set y2range [ -10:40]

gnuplot> set y2tics 5

gnuplot> set grid

gnuplot> replot  

image2016-8-31%2014%3A12%3A6.png?version=1&modificationDate=1472624661000&api=v2

//添加特殊点说明

gnuplot> set label 1 "最大降水" at 8,180  

gnuplot> set label 2 "最高气温" at second 7,25 center textcolor rgb "#FF9900"   

//second表示第二坐标系

gnuplot> replot

image2016-8-31%2014%3A12%3A25.png?version=1&modificationDate=1472624661000&api=v2

//添加箭头

gnuplot> set arrow 1 from 7.8,145 to 7.1,135 filled size 0.2,15,60 lw 2 

gnuplot> set label 2 "最高温" at 7.8,147 left 

image2016-8-31%2014%3A12%3A43.png?version=1&modificationDate=1472624661000&api=v2

//图例添加边框

gnuplot> set key box  

image2016-8-31%2014%3A13%3A2.png?version=1&modificationDate=1472624661000&api=v2

//改变图例的位置

gnuplot> set key center at 10, 0.7

//调换图例和文字的位置

gnuplot> set key reverse

//其他

help set key

//设置导出图片大小(png是像素,pdf,eps是英寸的矢量图)

gnuplot> set term pngcairo size 800 ,600  

//设置画布大小(长宽比例)

gnuplot> set size ratio 0.5

//

gnuplot> set size square

案例4:3D绘图

gnuplot> f(x,y)=sin(sqrt(x*x+y*y))/sqrt(x*x+y*y)

gnuplot> set xlabel "X"                         

gnuplot> set ylabel "Y"                         

gnuplot> set zlabel "Z"                         

gnuplot> unset key                              

gnuplot> splot f(x,y) 

image2016-8-31%2014%3A13%3A26.png?version=1&modificationDate=1472624661000&api=v2

//网格大小可以通过isosamples 参数来调节

gnuplot> set isosamples 50

gnuplot> set xyplane 0.2 //xyplane 调节下平面在z轴的位置

gnuplot> replot

image2016-8-31%2014%3A13%3A49.png?version=1&modificationDate=1472624661000&api=v2

//影藏遮挡住的部分

gnuplot> set hidden3d

gnuplot> replot  

//用色彩来表示不同的z 值

gnuplot> set pm3d

gnuplot> replot

image2016-8-31%2014%3A14%3A23.png?version=1&modificationDate=1472624661000&api=v2

//多图

gnuplot> set multiplot layout 2,2  

gnuplot> unset multiplot  //取消多图

案例5:柱状图

gnuplot> set style data histogram                             

gnuplot> set style histogram clustered gap 1

gnuplot> set style fill solid 0.4 border

gnuplot> plot "precipitation.dat" using 2, '' using 3

image2016-8-31%2014%3A15%3A0.png?version=1&modificationDate=1472624661000&api=v2

//添加图标注释

plot "precipitation2.dat" using 2:xticlabels(1) title columnheader(2), '' using 3:xticlabels(1) title columnheader(3)

//using 2 表示用第二列做数据

//xticlabels(1) 表示使用第一列做x轴

//columnheader(2) 表示使用第一行的第二列做图例的名称

image2016-8-31%2014%3A15%3A13.png?version=1&modificationDate=1472624661000&api=v2

//重叠,x轴向右旋转45度

gnuplot> set style histogram rowstacked

gnuplot> set boxwidth 0.8 relative//相邻两列间隔的相对宽度

gnuplot> set xtics rotate by -45

gnuplot> replot

image2016-8-31%2014%3A15%3A29.png?version=1&modificationDate=1472624661000&api=v2

gnuplot编程

//用load命令来加载写好的gnu命令集合文件

gnuplot> load ’ commands.gnu ’  

//for 命令的基本结构是: for [ i = begin : end : step ]  

gnuplot> f(x,s)= exp(-x*x / ( 2.*s ) ) /( sqrt (2 *pi * s ) )

gnuplot> set term wxt enhanced

gnuplot> set t i t l e "Normal␣ Di s t r i b u t i o n "

gnuplot> set samples 1000

gnuplot> set style fill transparent solid 0.3 noborder

gnuplot> plot for [ i =1 : 10 : 3 ] f (x , i )  w  filledcurves title "^2 = " . i 

//for 写法2

gnuplot> plot for [ filename in " file1.dat file2.dat  file3.dat " ] filename with lines  

//reread 循环,回到文档开始处,重新执行

plot sin ( x+i  pi /20) lw 3 l c rgb " green " n o t i t l e

i=i+1

i f ( i <40) reread  

转载于:https://my.oschina.net/jumpLee/blog/740588

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值