gnuplot 使用

介绍


使用

运行

1
2
gnuplot                   # 交互式绘图
gnuplot script.gnu # 脚本运行;脚本后缀名不限

命令简写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ter                       # terminal
out # output

u # using
w # with

p # points
l # lines
lp # linespoints
ps # pointsize
lw # linewidth
lc # linecolor

xr # xrange

bor # border

rep # replot

plot 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 示例
plot "data.txt" u 1:2 w lp

# 参数
u # 指定数据列;如 u 1:2 含义为第 1 列为 x,第 2 列为 y
w # 指定绘图样式
every ::0::10 # 使用前 10 行数据

# 绘图样式
p # 点绘图
l # 线绘图
lp # 点线绘图
impulses

pt N # 点的样式;N 为编号
lt N # 线的样式;N 为编号
ps value # 点的大小
lw value # 线的宽度;value 为数值
lc # 指定颜色;rgb "red"

xerr # x 误差棒
yerr # y 误差棒
xyerrorbars # xy 误差棒
filledcurve
  • plot 命令示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 正弦函数;不依赖外部数据文件
set xrange [-10:10]
plot sin(x) w lp pt 7


# 使用部分行数据绘制多个曲线
plot "thermo.out" every ::0::199 u 1:3 w p pt 7 ps 0.2 lc rgb "red" title "heat", \
"thermo.out" every ::200::300 u 1:3 w p pt 7 ps 0.2 lc rgb "blue" title "cooling" \


# 使用循环绘制多个曲线
legend_titles = "x y z"
plot for [i=2:4] "mvac.out" u 1:i with l lw 7-i title word(legend_titles, i-1)


# 自定义 x 数据
set xrange [1:5067]
plot 'data.txt' u 0:4 w l


# 使用变量,且对列数据进行操作
l0 = 132.622
plot 'thermo.out' u (($10 - l0)/l0):(-($4)) w lp

set 命令

  • 可控制图表的布局、样式、标签、轴属性、刻度、图例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
terminal                  # 输出格式
output # 输出文件名
title # 标题
xrange # x 轴范围
yrange # y 轴范围
xlabel # x 轴标签
ylabel # x 轴标签
xtics # x 轴刻度
ytics # x 轴刻度
mxtics n # 在主刻度之间增加 n 个次刻度
mytics n # 同上
key # 图例
log x # x 轴使用 log 坐标
style # 绘图样式
logscale # 对数刻度坐标轴
multiplot # 多/子图;按顺序写绘制子图的命令
grid # 网格


# 其他
set border lw 2.0 # 设置坐标轴线宽

# 添加垂直线
set arrow 1 nohead from 0.1,0 to 0.1,1

# 设置 x 轴刻度标签
set xtics ("{/Symbol G}" 0, "X" 0.1)

图例

1
2
3
4
5
6
7
8
9
10
11
12
13
set key ...

on # 开启图例
off # 关闭图例;或者 unset key
box # 显示边框
nobox # 不显示边框

top/bottom/left/right # 图例放置位置;可组合 top left
at x,y # 图例放置在具体坐标
inside, outside # 绘图区域内/外部
font ... # 图例字符大小

set key samplen 3 # 设置图例 label 长度

输出格式

1
2
3
4
5
set terminal ...          # gnuplot 进入交互,输入 set terminal,查看支持的图片格式

png pdf jpeg # 图片格式
size 800 800 # 图片尺寸;没有 dpi 设置参数
enhanced font 'Arial,12' # 字体及大小

较美观的 gnuplot 设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# config.gnu

set terminal pngcairo size 1000,800 enhanced font "Times New Roman,25"

# 设置调色板
setpal="if(pal eq 'cls'){set colorsequence classic};if(pal ne 'def' && pal ne 'cls'){do for[i=1:words(value(pal))]{set style line i lw 4 lc rgb word(value(pal),i)}}"

fav="#1F77B4 #FF7400 #00A13B #D62728 #984EA3 #A65628 #EE0F84 #7F7F7F #BCBD22 #17BECF"
pal='fav'; @setpal

set border lw 5.0

set xtics nomirror
set ytics nomirror

# unset key

# 设置全局线宽(还可设置 点的大小)
set for [i=1:7] style line i lw 5
# 6.0 版本该命令弃用(不报错,会有 warning)
set style increment user


# 使用示例
# set loadpath "~/scripts/cms-scripts/plots"
# load "config.gnu"

# set output "test.png"

# plot for [i=1:7] sin(x)+i*0.1 w l

fit 参数拟合

1
2
3
4
5
6
7
# EOS 拟合
set fit errorvariables

EOS(x) = B0*x/Bp * ( (V0/x)**(Bp)/(Bp-1.) + 1.) - B0*V0/(Bp-1.) + E0

B0 = 1.; Bp = 4.; E0 = $E0; V0 = $V0
fit EOS(x) "${evf}" u (\$1):(\$2) via B0, Bp, V0, E0