Matrix plot
มาฝึกสร้างการแสดงผลข้อมูลกัน อย่างแรกคือ Matrix plot ซึ่งใช้เมทริกซ์เป็นตัวแทนของเส้นเชื่อม (edges)
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์เครือข่ายเบื้องต้นด้วย Python
คำแนะนำการฝึกหัด
- สร้าง Matrix plot จากซับกราฟของ connected component ที่ใหญ่ที่สุด โดยจัดกลุ่ม author ตามหมายเลขกลุ่มผู้ใช้
- ขั้นแรก คำนวณซับกราฟของ connected component ที่ใหญ่ที่สุด โดยนำ
nx.connected_components(G)ใส่ในฟังก์ชันsorted()ที่เตรียมไว้ให้ ฟังก์ชันsorted()ของ Python รับ iterable และคืนค่าเป็นลิสต์ที่เรียงลำดับ (จากน้อยไปมากโดยค่าเริ่มต้น) ดังนั้น หากต้องการเข้าถึงซับกราฟของ connected component ที่ใหญ่ที่สุด ให้ใช้การ slice ด้วย[-1] - สร้าง
matrixplot ชื่อhโดยระบุพารามิเตอร์graphเป็นซับกราฟของ connected component ที่ใหญ่ที่สุด และgroup_byเป็น'grouping' - แสดง
matrixplot บนหน้าจอ
- ขั้นแรก คำนวณซับกราฟของ connected component ที่ใหญ่ที่สุด โดยนำ
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import necessary modules
from nxviz import matrix
import matplotlib.pyplot as plt
# Calculate the largest connected component: largest_ccs
largest_ccs = sorted(____, key=lambda x: len(x))[-1]
# Create the customized Matrix plot: h
h = ____
# Draw the Matrix plot to the screen
plt.show()