案列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
//添加栅格(需注意,左右两条y轴,两边的格数相等才可以)
gnuplot> set y2range [ -10:40]
gnuplot> set y2tics 5
gnuplot> set grid
gnuplot> replot
//添加特殊点说明
gnuplot> set label 1 "最大降水" at 8,180
gnuplot> set label 2 "最高气温" at second 7,25 center textcolor rgb "#FF9900"
//second表示第二坐标系
gnuplot> replot
//添加箭头
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
//图例添加边框
gnuplot> set key box
//改变图例的位置
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)
//网格大小可以通过isosamples 参数来调节
gnuplot> set isosamples 50
gnuplot> set xyplane 0.2 //xyplane 调节下平面在z轴的位置
gnuplot> replot
//影藏遮挡住的部分
gnuplot> set hidden3d
gnuplot> replot
//用色彩来表示不同的z 值
gnuplot> set pm3d
gnuplot> replot
//多图
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
//添加图标注释
plot "precipitation2.dat" using 2:xticlabels(1) title columnheader(2), '' using 3:xticlabels(1) title columnheader(3)
//using 2 表示用第二列做数据
//xticlabels(1) 表示使用第一列做x轴
//columnheader(2) 表示使用第一行的第二列做图例的名称
//重叠,x轴向右旋转45度
gnuplot> set style histogram rowstacked
gnuplot> set boxwidth 0.8 relative//相邻两列间隔的相对宽度
gnuplot> set xtics rotate by -45
gnuplot> replot
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