การพล็อตพิกัดเซนทรอยด์
เนื่องจากไม่สามารถพล็อต bounding box ทั้งหมดได้โดยตรง เราจึงสรุปตำแหน่งของ bounding box ให้เป็นจุดเดียวที่เรียกว่าเซนทรอยด์ (centroid) การพล็อตบนแผนที่ Basemap ทำได้ไม่ยาก เมื่อคำนวณเซนทรอยด์แล้ว ให้แยกค่าลองจิจูดและละติจูดออกจากกัน แล้วส่งให้เมธอด .scatter()
ออบเจ็กต์ Basemap m ถูกสร้างไว้ให้แล้ว รวมถึงชุดข้อมูล tweets_sotu และฟังก์ชัน calculateCentroid()
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์ข้อมูลโซเชียลมีเดียด้วย Python
คำแนะนำการฝึกหัด
- คำนวณเซนทรอยด์และเก็บผลลัพธ์ไว้ใน
centroids - ตั้งค่าอาร์กิวเมนต์
zorderในfillcontinentsเพื่อให้ทวีปแสดงอยู่ด้านหลังจุดข้อมูล - พล็อตจุดข้อมูล โดยตั้งค่าอาร์กิวเมนต์
latlonให้ถูกต้อง
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Calculate the centroids for the dataset
# and isolate longitudue and latitudes
centroids = ____[____].apply(____)
lon = [x[0] for x in centroids]
lat = [x[1] for x in centroids]
# Draw continents, coastlines, countries, and states
m.fillcontinents(color='white', ____ = ____)
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')
m.drawstates(color='gray')
# Draw the points and show the plot
____.____(____, ____, ____ = True, alpha = 0.7)
plt.show()