程序員的繪圖利器 — Gnuplot


 

介紹

 

Gnuplot is a command-line program that can generate two- and three-dimensional plots.

It is frequently used for publication-quality graphics as well as education.

The program can be used both interactively and in batch mode using scripts.

gnuplot is programmed in C.

For script-driven graphics, gnuplot is by far the most popular program.

 

先上一張效果圖:

 

用法

 

(1)數據文件格式

Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes)

on the plot or splot command line. Data files should have the data arranged in columns of numbers. Columns

should be separated by white space (tabs or spaces) only, (no commas). Lines beginning with a # character

are treated as comments and are ignored by gnuplot. A blank line in the data file results in a break in the line

connecting data points.

 

(2)風格定制

一般需要指明:

ranges of the axes

labels of the x and y axes

style of data point

style of the lines connecting the data points

title of the entire plot

 

Plots may be displayed in one of styles:

lines

points

linespoints

impulses

dots

steps

fsteps

histeps

errorbars

xerrorbars

yerrorbars

xyerrorbars

boxes

boxerrorbars

boxxyerrorbars

financebars

candlesticks

vector

 

實例

 

(1)基礎

set title "Some math functions" // 圖片標題

set xrange [-10:10] // 橫坐標范圍

set yrange [-2:2] // 縱坐標范圍

set zeroaxis // 零坐標線

plot (x/4)*2, sin(x), 1/x // 畫函數

 

(2)三維曲線

splot sin(x), tan(x) // 畫sin(x)和tan(x)的三維曲線

 

(3)多條曲線

plot sin(x) title 'Sine', tan(x) title 'Tangent' // 畫多條曲線,並且分別指定名稱

 

從不同數據文件中提取信息來畫圖:

plot "fileA.dat" using 1:2 title 'data A', \

        "fileB.dat" using 1:3 title 'data B'

 

(4)點和線風格

plot "fileA.dat" using 1:2 title 'data A' with lines, \

        "fileB.dat" using 1:3 title 'data B' with linespoints

可以使用縮寫:

using title and with can be abbreviated as u t and w.

 

plot sin(x) with line linetype 3 linewidth 2

或plot sin(x) w l lt 3 lw 2

plot sin(x) with point pointtype 3 pointsize 2

或plot sin(x) w p pt 3 ps 2

 

顏色列表大全:

http://www.uni-hamburg.de/Wiss/FB/15/Sustainability/schneider/gnuplot/colors.htm

用法:lc grb "greenyellow"

 

線條顏色,點的為pointtype

linetype 1 // 紅色

linetype 2 // 綠色

linetype 3 // 藍色

linetype 4 // 粉色

linetype 5 // 比較淺的藍色

linetype 6 // 褐色

linetype 7 // 橘黃色

linetype 8 // 淺紅色

 

線條粗細,點的為大小pointsize

linewidth 1 // 普通的線

linewidth 2 // 比較粗

linewidth 3 // 很粗

 

(5)常用

replot // 重繪

set autoscale // scale axes automatically

unset label // remove any previous labels

set xtic auto // set xtics automatically

set ytic auto // set ytics automatically

set xtics 2 // x軸每隔2設一個標點

set xlabel "x axe label"

set ylabel 'y axe label"

set label 'Yield Point" at 0.003,260 // 對一個點進行注釋

set arrow from 0.0028,250 to 0.003,280 // 兩點之間添加箭頭

set grid // 添加網格

reset // gnuplot沒有縮小,放大后只能reset后重繪,remove all customization

set key box // 曲線名稱加框

set key top left // 改變曲線名稱位置

set format xy "%3.2f" // x軸和y軸坐標格式,至少有3位,精確到小數點后兩位

quit、q、exit // 退出程序

 

(6)繪制多個圖

set size 1,1 // 總的大小

set origin 0,0 // 總的起點

set multiplot // 進入多圖模式

set size 0.5,0.5 // 第一幅圖大小

set origin 0,0.5 // 第一幅圖起點

plot sin(x)

set size 0.5,0.5

set origin 0,0

plot 1/sin(x)

set size 0.5,0.5

set orgin 0.5,0.5

plot cos(x)

set size 0.5,0.5

set origin 0.5,0

plot 1/cos(x)

unset multiplot

 

(7)輸出文件

set terminal png size 1024, 768 // 保存文件格式和大小

set output "file.png" // 保存文件名稱

set terminal X11 // 重新輸出到屏幕

 

(8)腳本

a.plt //后綴名為plt

gnuplot> load 'a.plt'

或gnuplot a.plt

save "graph.gp"

或save "graph.plt"

傳入參數

call "a.plt" param1 param2

param1、param2在a.plt中對應於$0、$1

 

(9)字體設置

輸出錯誤:

Could not find/open font when opening font "arial", using internal non-scalable font

下載RPM包:

wget http://www.my-guides.net/en/images/stories/fedora12/msttcore-fonts-2.0-3.noarch.rpm

rpm -ivh msttcore-fonts-2.0-3.noarch.rpm

設置環境變量:

修改/etc/profile或~/.bashrc,這樣設置可以固定下來。

export GDFONTPATH="/usr/share/fonts/msttcore"

export GNUPLOT_DEFAULT_GDFONT="arial"

. /profile/etc

OK,現在可以使用arial字體了。


 

Reference

 

[1]. http://en.wikipedia.org/wiki/Gnuplot

[2]. http://people.duke.edu/~hpgavin/gnuplot.html

[3]. http://blog.csdn.net/liyuanbhu/article/details/8502461

[4]. http://linux.chinaunix.net/techdoc/develop/2009/07/21/1125242.shtml

[5]. http://www.fnal.gov/docs/products/gnuplot/manual/

[6]. gnuplot中文手冊

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM