ในงานวิจัยบางอย่าง นักวิจัยใช้โมดูล GPS เพื่อติดตามพฤติกรรมสัตว์ พวกเขาสามารถติดตามได้ว่าสัตว์เหล่านั้นเดินทางไปสถานที่ต่างๆ ในช่วงเวลาต่างๆ ของปีอย่างไร
ในตัวอย่างนี้เราใช้ชุดข้อมูลประเภทนี้เพื่อได้ไอเดียว่านกเคลื่อนที่ในสถานที่ต่างๆ อย่างไร ในชุดข้อมูลนี้มีรายละเอียดตำแหน่งจากโมดูล GPS ถูกเก็บไว้ ชุดข้อมูลทั้งหมดอยู่ในรูปแบบ CSV ในไฟล์นั้นมีฟิลด์ต่างๆ ฟิลด์แรกคือ Bird Id แล้ว date_time, Latitude, longitude และ speed
สำหรับงานนี้ เราต้องการโมดูลบางอย่างที่ใช้ในโค้ด Python ได้
เราใช้ matplotlib, pandas, และ cartopy modules ติดตั้งลง Anaconda ด้วยคำสั่งต่อไปนี้ จะติดตั้งโมดูลสำคัญอื่นๆ อีกเมื่อจำเป็น
conda install -c conda-forge matplotlib conda install -c anaconda pandas conda install -c scitools/label/archive cartopy
ก่อนอื่นเราจะ plot ตำแหน่งโดยใช้ค่า latitude และ longitude มีสองนกในชุดข้อมูล ดังนั้นใช้สีสองสี เราสามารถ visualize ตำแหน่งการติดตามได้
ตัวอย่างโค้ด
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('bird_tracking.csv')
cr = df.groupby('bird_id').groups
cr_groups = df.groupby('bird_id')
group_list = []
for group in cr:
group_list.append(group)
plt.figure(figsize=(7, 7))
# สร้างกราฟจากชุดข้อมูลใช้กลุ่มแรกของนก
for group in group_list:
x, y = cr_groups.get_group(group).longitude, cr_groups.get_group(group).latitude
plt.plot(x, y, marker='o', markersize=2)
plt.show()
ผลลัพธ์

ตอนนี้เราสามารถ plot ผลการติดตามนี้บนแผนที่ภูมิศาสตร์จริง เพื่อ visualize ทางที่แน่นอนที่นกเหล่านั้นใช้
ตัวอย่างโค้ด
import pandas as pd
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
df = pd.read_csv("bird_tracking.csv")
bird_id = pd.unique(birddata.bird_id)
# Setup projection เพื่อแสดงรายละเอียดลงแผนที่
projection = ccrs.Mercator()
plt.figure(figsize=(7, 7))
axes = plt.axes(projection=projection)
axes.set_extent((-30.0, 25.0, 50.0, 10.0))
axes.add_feature(cfeature.LAND)
axes.add_feature(cfeature.OCEAN)
axes.add_feature(cfeature.COASTLINE)
axes.add_feature(cfeature.BORDERS, linestyle=':')
for id in bird_id:
index = df['bird_id'] == id
x = df.longitude[index]
y = df.latitude[index]
axes.plot(x, y, '.', transform=ccrs.Geodetic(), label=id)
plt.legend(loc="lower left")
plt.show()
ผลลัพธ์
