NumPy - Matplotlib
Matplotlib เป็นไลบรารีพล็อตสำหรับ Python ใช้ร่วมกับ NumPy เพื่อจัดเตรียมสภาพแวดล้อมที่เป็นทางเลือกโอเพ่นซอร์สที่มีประสิทธิภาพสำหรับ MatLab นอกจากนี้ยังสามารถใช้กับชุดเครื่องมือกราฟิกเช่น PyQt และ wxPython
โมดูล Matplotlib เขียนขึ้นครั้งแรกโดย John D. Hunter ตั้งแต่ปี 2012 Michael Droettboom เป็นผู้พัฒนาหลัก ปัจจุบัน Matplotlib ver. 1.5.1 เป็นเวอร์ชันเสถียรที่พร้อมใช้งาน แพคเกจที่มีอยู่ในการกระจายไบนารีเช่นเดียวกับในรูปแบบรหัสที่มาในwww.matplotlib.org
ตามปกติแพ็กเกจจะถูกนำเข้าสู่สคริปต์ Python โดยเพิ่มคำสั่งต่อไปนี้ -
from matplotlib import pyplot as plt
ที่นี่ pyplot()เป็นฟังก์ชันที่สำคัญที่สุดในไลบรารี matplotlib ซึ่งใช้ในการลงจุดข้อมูล 2 มิติ สคริปต์ต่อไปนี้จะพล็อตสมการy = 2x + 5
ตัวอย่าง
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()
วัตถุ ndarray x ถูกสร้างขึ้นจาก np.arange() function เป็นค่าบน x axis. ค่าที่สอดคล้องกันบนy axis จะถูกเก็บไว้ในที่อื่น ndarray object y. ค่าเหล่านี้พล็อตโดยใช้plot() ฟังก์ชันของโมดูลย่อย pyplot ของแพ็คเกจ matplotlib
การแสดงกราฟิกจะแสดงโดย show() ฟังก์ชัน
รหัสด้านบนควรสร้างผลลัพธ์ต่อไปนี้ -
แทนที่จะเป็นกราฟเชิงเส้นคุณสามารถแสดงค่าได้อย่างไม่ชัดเจนโดยการเพิ่มสตริงรูปแบบลงในไฟล์ plot()ฟังก์ชัน สามารถใช้อักขระการจัดรูปแบบต่อไปนี้ได้
ซีเนียร์ | ตัวละครและคำอธิบาย |
---|---|
1 | '-' ลักษณะเส้นทึบ |
2 | '--' ลักษณะเส้นประ |
3 | '-.' สไตล์เส้นประ |
4 | ':' ลักษณะเส้นประ |
5 | '.' เครื่องหมายจุด |
6 | ',' เครื่องหมายพิกเซล |
7 | 'o' เครื่องหมายวงกลม |
8 | 'v' เครื่องหมาย Triangle_down |
9 | '^' เครื่องหมาย Triangle_up |
10 | '<' เครื่องหมาย Triangle_left |
11 | '>' เครื่องหมาย Triangle_right |
12 | '1' เครื่องหมาย Tri_down |
13 | '2' เครื่องหมาย Tri_up |
14 | '3' เครื่องหมาย Tri_left |
15 | '4' เครื่องหมาย Tri_right |
16 | 's' เครื่องหมายสี่เหลี่ยม |
17 | 'p' เครื่องหมายห้าเหลี่ยม |
18 | '*' เครื่องหมายดาว |
19 | 'h' เครื่องหมาย Hexagon1 |
20 | 'H' เครื่องหมาย Hexagon2 |
21 | '+' เครื่องหมายบวก |
22 | 'x' เครื่องหมาย X |
23 | 'D' เครื่องหมายเพชร |
24 | 'd' เครื่องหมาย Thin_diamond |
25 | '|' เครื่องหมาย Vline |
26 | '_' เครื่องหมาย Hline |
นอกจากนี้ยังมีการกำหนดตัวย่อสีต่อไปนี้
ตัวละคร | สี |
---|---|
'b' | สีน้ำเงิน |
'g' | เขียว |
'r' | แดง |
'ค' | สีฟ้า |
'm' | ม่วงแดง |
'y' | สีเหลือง |
'k' | ดำ |
'w' | สีขาว |
หากต้องการแสดงวงกลมที่แสดงจุดแทนเส้นในตัวอย่างข้างต้นให้ใช้ “ob” เป็นสตริงรูปแบบในฟังก์ชัน plot ()
ตัวอย่าง
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y,"ob")
plt.show()
รหัสด้านบนควรสร้างผลลัพธ์ต่อไปนี้ -
แผนไซน์เวฟ
สคริปต์ต่อไปนี้สร้างไฟล์ sine wave plot ใช้ matplotlib
ตัวอย่าง
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.title("sine wave form")
# Plot the points using matplotlib
plt.plot(x, y)
plt.show()
แผนการย่อย ()
ฟังก์ชัน subplot () ช่วยให้คุณสามารถพล็อตสิ่งต่างๆในรูปเดียวกันได้ ในสคริปต์ต่อไปนี้sine และ cosine values กำลังวางแผน
ตัวอย่าง
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()
รหัสด้านบนควรสร้างผลลัพธ์ต่อไปนี้ -
บาร์()
pyplot submodule ให้ bar()ฟังก์ชันในการสร้างกราฟแท่ง ตัวอย่างต่อไปนี้สร้างกราฟแท่งของสองชุดx และ y อาร์เรย์
ตัวอย่าง
from matplotlib import pyplot as plt
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [6,15,7]
plt.bar(x, y, align = 'center')
plt.bar(x2, y2, color = 'g', align = 'center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
รหัสนี้ควรสร้างผลลัพธ์ต่อไปนี้ -