网站首页 > 开源技术 正文
Gnuplot 是一个小巧而强大,免费开源跨平台(Windows,Mac,Linux)的绘图工具。其特点在于绘图质量高且快,易学易用,仅需少量代码,就可得到用于发表的高质量图片,中英文参考文档丰富。Gnuplot可做简单的数据处理和分析,比如统计和多参数函数拟合。毕竟2,3维绘图才是它的强项。演示可以查看视频开源免费,小巧强大:一张图急速入门数据绘图。 先上图,后边贴代码。
用到的数据文件(data.txt)的内容如下
1.0 2.0 0.6
3.1 3.9 0.5
4.9 5.0 1.5
7.2 8.1 0.9
8.9 10.3 0.7
三种运行方式(假设绘图脚本存于“plot.gp”文件中):
(1)在当前目录下打开终端, 输入 gnuplot plot.gp 并回车
(2) 在当前目录下打开终端,先在终端输入gnuplot进去,然后load "plot.gp" 回车。
(3)Windows 下双击脚本。
代码
如下:
############ 几十行代码急速入门Gnuplot数据绘图 #############
### "#" 后边是注释
### 首先设置输出的格式,支持pdf,png,eps等常用格式
set terminal pngcairo size 1000,1000 font 'Times New Roman,10' ## 格式,大小和字体
set output "plot.png" ###输出的文件名
#set terminal pdfcairo size 20cm,20cm font 'Times New Roman,12' ##
#set output "plot.pdf"
#set terminal epscairo size 20cm,20cm font 'Times New Roman,12' ##
#set output "plot.eps"
### 可以定义变量和宏,便于后边重复使用
sx = "set xrange " ## 例如后边当成宏来引用:@sx ,而不是使用 "set xrange",你可缩减代码量
### 定义变量,用来设置上下左右的边缘和子图间距离
left=0.1
right=0.95
bottom=0.1
top="0.95"
hspace="0.1"
wspace="0.15"
###因为是要一张图里4个子图,所以启用了多图模式:
set multiplot layout 2,2 spacing @hspace,@wspace margins left,right,bottom,@top
########## 子图 (1): 绘制函数,设置基本的元素如:标题、坐标范围、图例等
set label "(1)" at graph 0.02,0.03 font ',20' textcolor rgb 'red'
set title "example"
set xlabel "This is xlabel with {/Symbol a}=0.1 to 100"
set ylabel "This is ylabel with X^2_3"
set key top right Left reverse font 'Times New Roman,15' ###设置图例格式:位置、字体等
f(x)=sin(x)/x ###定义函数
set xrange [0:100] ###设置x轴范围
set yrange [-0.5:1.0] ###设置y轴范围
set xtics scale 3 ###设置x轴的刻度的长度,是默认的3倍长
set mxtics 10 ###x轴子刻度的数目
set mytics 5 ###y轴子刻度的数目
set log x ###x轴设置成log
set style fill pattern 1
### plot命令开始绘图并设置参数:
plot f(x) w line linetype 1 pointtype 5 ps 1.0 lc 2 lw 4,\
f(x) w filledcurves y=0.5 lc rgb 'blue'
###上边用到了缩写ps=pointsize.其他也有缩写:如line=l, linetype=lt, pointtype=pt等等
######### 子图 (2): 数据文件绘图和拟合
reset ###Gnuplot会继承上边的命令,所以需要reset取消之前所有的设置
set title "fitting functions"
set label "(2)" at graph 0.02,0.03 font ',20' textcolor rgb 'red'
@sx [0:11] ###这里引用了宏
set yrange [0:11]
set key bottom right font ",14" spacing 2
g(x) = a*x**2 + b*x + c ###定义函数并拟合,参数为a,b,c
fit g(x) "data.txt" u 1:2 via a,b,c ###定义函数并拟合
key_g= sprintf("fits without yerror:\ng(x) = %5.3f*x^2 + %5.3f*x + %5.3f",a,b,c)
h(x) = d*x**2 + e*x + f
fit h(x) "data.txt" u 1:2:3 yerror via d,e,f
key_h= sprintf("fits with yerror:\nh(x) = %5.3f*x^2 + %5.3f*x + %5.3f",d,e,f)
set xlabel 'xxxx' rotate by 45
plot "data.txt" u 1:2:3 w yerror pt 5 ps 1.0 lc rgb 'blue' title "data",\
g(x) w line linecolor rgb 'red' title key_g,\
h(x) w l lc rgb 'green' title key_h
######### 子图 (3):统计和填充
reset
set key left top box
set label "(3)" at graph 0.02,0.03 font ',20' textcolor rgb 'red'
df='data.txt' ###这里使用文件里的数据绘图
stats df u 1:2 name "A" ###统计数据的1和2列,并将统计结果存入A中
print A_min_x, A_min_y ###可以打印出A中统计的x的最大和最小值
@sx [A_min_x-1: A_max_x + 1] ###根据统计数据设置x轴范围
set yr [A_min_y-1: A_max_y + 1]
set arrow from A_min_x,A_min_y+2 to A_min_x,A_min_y ###绘制箭头
set label 'Min.' at A_min_x,A_min_y+2.3 center
set arrow from A_max_x,A_max_y-2 to A_max_x,A_max_y
set label 'Max.' at A_max_x,A_max_y-2.3 center front
set xlabel 'xxxx' textcolor rgb 'red'
set style fill solid 0.5
#set style fill pattern 3
plot df u 1:2 w filledcurves y=2 lc rgb 'seagreen' title 'fill with y=2',\
df u 1:2 w lp pt 4 ps 0.9 lc rgb 'red' lw 2,\
[3:6] A_mean_y w l dt '--' lw 2 lc rgb 'black' title "mean"
######## 子图 (4):直方图
reset
set label "(4)" at graph 0.02,0.03 font ',20' textcolor rgb 'red'
set style data histogram
set style fill solid 1.0 border lt -1
set boxwidth 1.0
set key at 1.5,7 box font ',15' reverse
set yr [0:10]
set xtics ("A" 0, "B" 1, "C" 2, "{/Symbol b}" 3, "{/Symbol S}" 4)
set xlabel 'xxxx' offset 0,-1
plot "data.txt" u 1 title 'histogram' lc rgb 'seagreen',\
"data.txt" u 0:($1+0.5):1 w labels title ""
unset multiplot ###退出多图模式,完成绘图并保存
猜你喜欢
- 2024-09-12 我是怎么打开车库门的:ASK/OOK手动解码及重放
- 2024-09-12 程序员的推荐(程序员推荐什么轴的键盘)
- 2024-09-12 ubuntu vscode设置 c++ opencv 非contrib版本
- 2024-09-12 我是怎么打开车库门的(请勿非法模仿)
- 2024-09-12 Qt:QCustomPlot使用教程(qtdesigner使用教程)
- 2024-09-12 Python3 环境搭建(python 环境搭建教程)
- 2024-09-12 GitStats - Git 历史统计信息工具
- 2024-09-12 Linux服务器下Apache 压力测试工具AB
- 2024-09-12 毕业论文写作技巧与指南系列,升技在线
- 2024-09-12 「工具」 Putty、XShell 真的没有冤枉你们
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)