การใช้งาน GnuPlot เบื้องต้น – GnuPlot Tutorial

ดาวน์โหลด

ติดตั้ง

เปิดโปรแกรม

สอนคอมฟรี

ลองพิมพ์คำสั่ง plot sin(x)

ต่อไปจะสาธิตการวาดกราฟเส้นที่มี 3 เส้น

เปิด NotePad, ลอกข้อมูลต่อไปนี้, และเซฟลงไฟล์ชื่อ a.dat ข้อมูลคอลัมน์แรกจะใช้เป็นค่าในแกน X, คอลัมน์ถัดๆไปจะใช้เป็นค่าในแกน Y


2 1 9 10 12
4 3 10 12 14
6 8 11 15 17
8 15 17 23 23
10 23 18 27 26
12 25 30 33 40

เปิด NotePad, ลอกคำสั่งต่อไปนี้ (เครื่องหมาย # คือคอมเมนท์), และเซฟลงไฟล์ชื่อ test.plt


# กำหนดชื่อกราฟและชื่อแกน
set title "My Title"
set xlabel "Xxxx"
set ylabel "Yyyy"

# กำหนดช่วงแกน x และระยะระหว่างขีดแบ่งแกน
set xrange [2:12]
set xtics 2
# กลับมาตั้งแบบอัตโนมัติโดย set autoscale x

# แสดงเส้นตารางในกราฟ
#set grid

# พล็อทหนึ่งชุด โดยใช้ค่าจากคอลัมน์ที่ 1 และ 2
plot "a.dat" using 1:2

ให้ไฟล์ test.plt และ a.dat อยู่ในโฟลเดอร์เดียวกัน ดับเบิ้ลคลิก test.plt

เพิ่มคำสั่งต่อไปนี้ลงในไฟล์ test.plt


# พล็อทสองเส้น
# ค่าจากคอลัมน์ที่ 1 และ 2 ชื่อ One
# ค่าจากคอลัมน์ที่ 1 และ 3 ชื่อ Two
plot "a.dat" using 1:2 title 'One' with lines, "" using 1:3 title 'Two' with lines


# พล็อทสองเส้น มีสัญลักษณ์กำกับเส้น
plot "a.dat" using 1:2 title 'One' with linespoint, "" using 1:3 title 'Two' with linespoint


# กำหนดตำแหน่ง legend
set key right bottom
# ค่าที่เป็นไปได้คือ center, left, right, top, bottom, outside, below
# เช่น center below
# หรือ right outside

# วาดกรอบล้อม legend
set key box

replot


# เพิ่มความกว้างของกรอบ legend
set key box width 5

# วาดกราฟสี่เส้น โดยกำหนดลักษณะเส้นเอง
## ชนิดเส้น lt (line type) 1 solid, 2 dash, 3 dotted
## ความหนาของเส้น lw (line weight)
## สีเส้น lc (line color)
## มาร์คเคอร์ pt (point type)
### ถ้าเป็นแบบ PlayStation ใช้ 8, 6, 2, 4
### triangle, circle, x, rectangle
## ขนาดมาร์คเคอร์ ps (point size)
plot "a.dat" using 1:2 title 'One' with linespoint lt 1 lw 2 lc rgb "#8B0000" pt 8 ps 1.4, "" using 1:3 title 'Two' with linespoint lt 1 lw 2 lc rgb "#006400" pt 6 ps 1.4, "" using 1:4 title 'Three' with linespoint lt 2 lw 2 lc rgb "#00008B" pt 2 ps 1.4, "" using 1:5 title 'Four' with linespoint lt 2 lw 2 lc rgb "#DAA520" pt 4 ps 1.4


# plot ลงไฟล์ PostScript
set term postscript enhanced color "Helvetica" 18
set output "a.ps"

replot

เปิดไฟล์ .ps โดยใช้ GSview

# แสดงภาพเป็นขาวดำ
set terminal windows monochrome
replot

สรุปโค้ด

set title "My Title"
set xlabel "Xxxx"
set ylabel "Yyyy"

set xrange [2:12]
set xtics 2

#set grid

set key right bottom
set key box width 10

plot "a.dat" using 1:2 title 'One' with linespoint lt 1 lw 2 lc rgb "#8B0000" pt 8 ps 1.4, "" using 1:3 title 'Two' with linespoint lt 1 lw 2 lc rgb "#006400" pt 6 ps 1.4, "" using 1:4 title 'Three' with linespoint lt 2 lw 2 lc rgb "#00008B" pt 2 ps 1.4, "" using 1:5 title 'Four' with linespoint lt 2 lw 2 lc rgb "#DAA520" pt 4 ps 1.4

set term postscript enhanced color "Helvetica" 18
set output "a.ps"

replot

หากจะนำเนื้อหาไปใช้ ต้องแสดงที่มา และห้ามใช้ในเชิงพาณิชย์

Leave a comment