เครื่องหมาย Matplotlib
เครื่องหมาย
คุณสามารถใช้อาร์กิวเมนต์คำหลักmarker
เพื่อเน้นแต่ละจุดด้วยเครื่องหมายที่ระบุ:
ตัวอย่าง
ทำเครื่องหมายแต่ละจุดด้วยวงกลม:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o')
plt.show()
ผลลัพธ์:
ตัวอย่าง
ทำเครื่องหมายแต่ละจุดด้วยดาว:
...
plt.plot(ypoints, marker = '*')
...
ผลลัพธ์:
เครื่องหมายอ้างอิง
คุณสามารถเลือกเครื่องหมายใดก็ได้:
Marker | Description | |
---|---|---|
'o' | Circle | |
'*' | Star | |
'.' | Point | |
',' | Pixel | |
'x' | X | |
'X' | X (filled) | |
'+' | Plus | |
'P' | Plus (filled) | |
's' | Square | |
'D' | Diamond | |
'd' | Diamond (thin) | |
'p' | Pentagon | |
'H' | Hexagon | |
'h' | Hexagon | |
'v' | Triangle Down | |
'^' | Triangle Up | |
'<' | Triangle Left | |
'>' | Triangle Right | |
'1' | Tri Down | |
'2' | Tri Up | |
'3' | Tri Left | |
'4' | Tri Right | |
'|' | Vline | |
'_' | Hline |
รูปแบบสตริงfmt
คุณยังสามารถใช้ พารามิเตอร์ สัญลักษณ์สตริงช็ อต คัทเพื่อระบุตัวทำเครื่องหมาย
พารามิเตอร์นี้เรียกอีกอย่างว่าfmt
และเขียนด้วยไวยากรณ์นี้:
marker|line|color
ตัวอย่าง
ทำเครื่องหมายแต่ละจุดด้วยวงกลม:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, 'o:r')
plt.show()
ผลลัพธ์:
ค่าเครื่องหมายสามารถเป็นอะไรก็ได้จากการอ้างอิงตัวทำเครื่องหมายด้านบน
ค่าบรรทัดสามารถเป็นค่าใดค่าหนึ่งต่อไปนี้:
อ้างอิงสาย
Line Syntax | Description | |
---|---|---|
'-' | Solid line | |
':' | Dotted line | |
'--' | Dashed line | |
'-.' | Dashed/dotted line |
หมายเหตุ:หากคุณ ละเว้นค่า บรรทัดในพารามิเตอร์ fmt จะไม่มีเส้นใดถูกพล็อต
ค่าสีแบบสั้นสามารถเป็นค่าใดค่าหนึ่งต่อไปนี้:
อ้างอิงสี
Color Syntax | Description | |
---|---|---|
'r' | Red | |
'g' | Green | |
'b' | Blue | |
'c' | Cyan | |
'm' | Magenta | |
'y' | Yellow | |
'k' | Black | |
'w' | White |
ขนาดเครื่องหมาย
คุณสามารถใช้อาร์กิวเมนต์คำหลักmarkersize
หรือเวอร์ชันที่สั้นกว่าms
เพื่อกำหนดขนาดของเครื่องหมาย:
ตัวอย่าง
กำหนดขนาดของเครื่องหมายเป็น 20:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20)
plt.show()
ผลลัพธ์:
สีมาร์กเกอร์
คุณสามารถใช้อาร์กิวเมนต์คำหลักmarkeredgecolor
หรือค่าที่สั้นกว่าmec
เพื่อกำหนดสีของ
ขอบของเครื่องหมาย:
ตัวอย่าง
ตั้งค่าสี EDGE เป็นสีแดง:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r')
plt.show()
ผลลัพธ์:
คุณสามารถใช้อาร์กิวเมนต์คำหลักmarkerfacecolor
หรือค่าที่สั้นกว่าmfc
เพื่อกำหนดสีภายในขอบของเครื่องหมาย:
ตัวอย่าง
ตั้งค่าสี FACE เป็นสีแดง:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20, mfc = 'r')
plt.show()
ผลลัพธ์:
ใช้ทั้ง the mec
และmfc
อาร์กิวเมนต์เพื่อกำหนดสีของมาร์กเกอร์ทั้งหมด:
ตัวอย่าง
กำหนดสีของทั้งขอบและใบหน้าเป็นสีแดง:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r', mfc = 'r')
plt.show()
ผลลัพธ์:
คุณยังสามารถใช้ค่าสีฐานสิบหก :
ตัวอย่าง
ทำเครื่องหมายแต่ละจุดด้วยสีเขียวที่สวยงาม:
...
plt.plot(ypoints, marker = 'o', ms = 20, mec = '#4CAF50', mfc = '#4CAF50')
...
ผลลัพธ์:
หรือชื่อสีที่รองรับ140 ชื่อ
ตัวอย่าง
ทำเครื่องหมายแต่ละจุดด้วยสีที่ชื่อว่า "hotpink":
...
plt.plot(ypoints, marker = 'o', ms = 20, mec = 'hotpink', mfc = 'hotpink')
...
ผลลัพธ์: