Matplotlib Scatter
การสร้างพล็อตกระจาย
ด้วย Pyplot คุณสามารถใช้scatter()
ฟังก์ชันนี้เพื่อวาดแผนภาพแบบกระจาย
ฟังก์ชันscatter()
จะพล็อตจุดหนึ่งจุดสำหรับการสังเกตแต่ละครั้ง มันต้องการสองอาร์เรย์ที่มีความยาวเท่ากัน อาร์เรย์หนึ่งสำหรับค่าของแกน x และอีกชุดสำหรับค่าบนแกน y:
ตัวอย่าง
พล็อตกระจายอย่างง่าย:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.show()
ผลลัพธ์:
การสังเกตในตัวอย่างข้างต้นเป็นผลจากรถยนต์ 13 คันที่ผ่านไปมา
แกน X แสดงอายุรถ
แกน Y แสดงความเร็วของรถเมื่อผ่าน
มีความสัมพันธ์ใด ๆ ระหว่างการสังเกตหรือไม่?
ดูเหมือนว่ายิ่งรถใหม่เท่าไหร่ก็ยิ่งขับเร็วขึ้น แต่นั่นอาจเป็นเรื่องบังเอิญ เพราะเราจดทะเบียนรถเพียง 13 คันเท่านั้น
เปรียบเทียบพล็อต
ในตัวอย่างข้างต้น ดูเหมือนว่าจะมีความสัมพันธ์ระหว่างความเร็วและอายุ แต่ถ้าเราพล็อตข้อสังเกตจากวันอื่นด้วยล่ะ พล็อตกระจายจะบอกอะไรเราอีกไหม
ตัวอย่าง
วาดสองแปลงในรูปเดียวกัน:
import matplotlib.pyplot as plt
import numpy as np
#day one, the age
and speed of 13 cars:
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x,
y)
#day two, the age and speed of 15 cars:
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)
plt.show()
ผลลัพธ์:
หมายเหตุ:แปลงทั้งสองนี้ถูกวางแผนด้วยสีที่ต่างกันสองสี โดยค่าเริ่มต้นคือสีน้ำเงินและสีส้ม คุณจะได้เรียนรู้วิธีเปลี่ยนสีในบทนี้ต่อไป
เมื่อเปรียบเทียบทั้งสองแปลงแล้ว ฉันคิดว่าน่าจะพูดได้เต็มปากว่าทั้งสองแปลงให้ข้อสรุปแบบเดียวกันคือ ยิ่งรถใหม่ ยิ่งขับเร็ว
สี
คุณสามารถกำหนดสีของคุณเองสำหรับพล็อตกระจายแต่ละพล็อตด้วย
อาร์กิวเมนต์color
หรือ :c
ตัวอย่าง
กำหนดสีของเครื่องหมายของคุณเอง:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x,
y, color = 'hotpink')
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y, color = '#88c999')
plt.show()
ผลลัพธ์:
สีแต่ละจุด
คุณยังสามารถกำหนดสีเฉพาะสำหรับแต่ละจุดได้โดยใช้อาร์เรย์ของสีเป็นค่าสำหรับ
c
อาร์กิวเมนต์:
หมายเหตุ:คุณไม่สามารถใช้color
อาร์กิวเมนต์สำหรับสิ่งนี้c
อาร์กิวเมนต์เท่านั้น
ตัวอย่าง
กำหนดสีของเครื่องหมายของคุณเอง:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array(["red","green","blue","yellow","pink","black","orange","purple","beige","brown","gray","cyan","magenta"])
plt.scatter(x, y, c=colors)
plt.show()
ผลลัพธ์:
แผนที่สี
โมดูล Matplotlib มีแผนที่สีจำนวนมาก
แผนที่สีเป็นเหมือนรายการสี โดยแต่ละสีมีค่าที่อยู่ในช่วงตั้งแต่ 0 ถึง 100
นี่คือตัวอย่างของ colormap:
แผนที่สีนี้เรียกว่า 'viridis' และอย่างที่คุณเห็นว่ามีตั้งแต่ 0 ซึ่งเป็นสีม่วง และสูงสุด 100 ซึ่งเป็นสีเหลือง
วิธีใช้ ColorMap
คุณสามารถระบุ colormap ด้วยอาร์กิวเมนต์ของคีย์เวิร์ด
cmap
ด้วยค่าของ colormap ในกรณีนี้'viridis'
ซึ่งเป็นหนึ่งใน colormap ที่มีอยู่แล้วใน Matplotlib
นอกจากนี้ คุณต้องสร้างอาร์เรย์ที่มีค่า (ตั้งแต่ 0 ถึง 100) หนึ่งค่าสำหรับแต่ละจุดในแผนภาพกระจาย:
ตัวอย่าง
สร้างอาร์เรย์สี และระบุแผนผังสีในพล็อตแบบกระจาย:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0,
10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
plt.scatter(x, y, c=colors, cmap='viridis')
plt.show()
ผลลัพธ์:
คุณสามารถรวม colormap ไว้ในภาพวาดได้โดยการใส่plt.colorbar()
คำสั่ง:
ตัวอย่าง
รวมแผนผังสีจริง:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0,
10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.show()
ผลลัพธ์:
ColorMaps ที่มีจำหน่าย
คุณสามารถเลือกแผนผังสีที่มีอยู่แล้วภายใน:
Name | Reverse | |||
---|---|---|---|---|
Accent | Accent_r | |||
Blues | Blues_r | |||
BrBG | BrBG_r | |||
BuGn | BuGn_r | |||
BuPu | BuPu_r | |||
CMRmap | CMRmap_r | |||
Dark2 | Dark2_r | |||
GnBu | GnBu_r | |||
Greens | Greens_r | |||
Greys | Greys_r | |||
OrRd | OrRd_r | |||
Oranges | Oranges_r | |||
PRGn | PRGn_r | |||
Paired | Paired_r | |||
Pastel1 | Pastel1_r | |||
Pastel2 | Pastel2_r | |||
PiYG | PiYG_r | |||
PuBu | PuBu_r | |||
PuBuGn | PuBuGn_r | |||
PuOr | PuOr_r | |||
PuRd | PuRd_r | |||
Purples | Purples_r | |||
RdBu | RdBu_r | |||
RdGy | RdGy_r | |||
RdPu | RdPu_r | |||
RdYlBu | RdYlBu_r | |||
RdYlGn | RdYlGn_r | |||
Reds | Reds_r | |||
Set1 | Set1_r | |||
Set2 | Set2_r | |||
Set3 | Set3_r | |||
Spectral | Spectral_r | |||
Wistia | Wistia_r | |||
YlGn | YlGn_r | |||
YlGnBu | YlGnBu_r | |||
YlOrBr | YlOrBr_r | |||
YlOrRd | YlOrRd_r | |||
afmhot | afmhot_r | |||
autumn | autumn_r | |||
binary | binary_r | |||
bone | bone_r | |||
brg | brg_r | |||
bwr | bwr_r | |||
cividis | cividis_r | |||
cool | cool_r | |||
coolwarm | coolwarm_r | |||
copper | copper_r | |||
cubehelix | cubehelix_r | |||
flag | flag_r | |||
gist_earth | gist_earth_r | |||
gist_gray | gist_gray_r | |||
gist_heat | gist_heat_r | |||
gist_ncar | gist_ncar_r | |||
gist_rainbow | gist_rainbow_r | |||
gist_stern | gist_stern_r | |||
gist_yarg | gist_yarg_r | |||
gnuplot | gnuplot_r | |||
gnuplot2 | gnuplot2_r | |||
gray | gray_r | |||
hot | hot_r | |||
hsv | hsv_r | |||
inferno | inferno_r | |||
jet | jet_r | |||
magma | magma_r | |||
nipy_spectral | nipy_spectral_r | |||
ocean | ocean_r | |||
pink | pink_r | |||
plasma | plasma_r | |||
prism | prism_r | |||
rainbow | rainbow_r | |||
seismic | seismic_r | |||
spring | spring_r | |||
summer | summer_r | |||
tab10 | tab10_r | |||
tab20 | tab20_r | |||
tab20b | tab20b_r | |||
tab20c | tab20c_r | |||
terrain | terrain_r | |||
twilight | twilight_r | |||
twilight_shifted | twilight_shifted_r | |||
viridis | viridis_r | |||
winter | winter_r |
ขนาด
คุณสามารถเปลี่ยนขนาดของจุดด้วย
s
อาร์กิวเมนต์
เช่นเดียวกับสี ตรวจสอบให้แน่ใจว่าอาร์เรย์สำหรับขนาดมีความยาวเท่ากับอาร์เรย์สำหรับแกน x และ y:
ตัวอย่าง
กำหนดขนาดของคุณเองสำหรับเครื่องหมาย:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes =
np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x,
y, s=sizes)
plt.show()
ผลลัพธ์:
อัลฟ่า
คุณสามารถปรับความโปร่งใสของจุดด้วย
alpha
อาร์กิวเมนต์
เช่นเดียวกับสี ตรวจสอบให้แน่ใจว่าอาร์เรย์สำหรับขนาดมีความยาวเท่ากับอาร์เรย์สำหรับแกน x และ y:
ตัวอย่าง
กำหนดขนาดของคุณเองสำหรับเครื่องหมาย:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes =
np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x,
y, s=sizes, alpha=0.5)
plt.show()
ผลลัพธ์:
รวมขนาดสีและอัลฟ่า
คุณสามารถรวม colormap กับขนาดต่างๆ บนจุดต่างๆ ได้ วิธีนี้จะทำให้เห็นภาพได้ดีที่สุดหากจุดต่างๆ โปร่งใส:
ตัวอย่าง
สร้างอาร์เรย์สุ่มที่มีค่า 100 ค่าสำหรับจุด x จุด y สี และขนาด:
import matplotlib.pyplot as plt
import numpy as np
x =
np.random.randint(100, size=(100))
y = np.random.randint(100, size=(100))
colors = np.random.randint(100, size=(100))
sizes = 10 * np.random.randint(100,
size=(100))
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')
plt.colorbar()
plt.show()
ผลลัพธ์: