Last active
February 7, 2018 19:54
-
-
Save cjue25/30804b003fc159bbc95c8e9df7d9bb4e to your computer and use it in GitHub Desktop.
Using Python for Research_Case Study 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
np.isnan() | |
查看是否有nan | |
np.isnan(speed).any() | |
是true的話代表一定有nan值 | |
np.sum(np.isnan()) | |
看有幾個nan的值 | |
~對pd.Series和pd.DaraFrame可以轉換true與false | |
datetime.datetime.strptime(data,"%Y-%m-%d %H:%M:%S") 將時間以 年(西元) 月 日 時(24hr) 分 秒 去處理 | |
plt.hist有NAN值會error | |
有很酷的cartopy工具 | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import datetime | |
birddata=pd.read_csv("bird_tracking.csv") | |
#可以觀察data的詳細資料 | |
birddata.info() | |
bird_names=birddata.bird_name.unique() | |
plt.figure(figsize=(7,7)) | |
for birdname in bird_names: | |
ix=birddata.bird_name == birdname | |
x,y=birddata.longitude[ix] , birddata.latitude[ix] | |
plt.plot(x,y,'.',label=birdname) | |
plt.xlabel('Longitude') | |
plt.ylabel('Latitude') | |
plt.legend(loc="lower right") | |
plt.savefig("3traj.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plt.figure(figsize=(8,4)) | |
speed=birddata.speed_2d[birddata.bird_name=='Eric'] | |
ind=np.isnan(speed) | |
#只畫有數值的,並且normalize,讓frequency總和為1 | |
#注意用plt.hist方式畫畫時,要考慮NAN值 | |
plt.hist(speed[~ind],bins=np.linspace(0,30,30),normed=True) # ~ 對pd.Series和pd.DaraFrame可以轉換true與false | |
plt.axis('tight') | |
plt.xlabel('2D speed(m/s)') | |
plt.ylabel('Frequency'); | |
plt.savefig("hist.png") | |
##這裡帶到也可以用pandas畫畫,且沒有NAN值的限制 | |
##birddata.speed_2d.plot(kind='hist',range=[0,30]) | |
##plt.xlabel('2D speed') | |
##plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
timestamps=[] | |
#datetime.datetime.strptime(data,"%Y-%m-%d %H:%M:%S") 將時間以 年(西元) 月 日 時(24hr) 分 秒 去處理 | |
#在這裡消除原始資料中 +00 的值 | |
for k in range(len(birddata)): | |
timestamps.append(datetime.datetime.strptime(birddata.date_time[k][:-3],"%Y-%m-%d %H:%M:%S")) | |
birddata["timestamp"] = pd.Series(timestamps, index=birddata.index) | |
data=birddata[birddata.bird_name=="Eric"] | |
times=data.timestamp | |
elapsed_time=[time-times[0] for time in times] #算出和第一個相差時間 | |
elapsed_days=np.array(elapsed_time)/datetime.timedelta(days=1) #相差時間換算成天數、小時用hours=1 | |
plt.plot(elapsed_days) | |
plt.xlabel("Obeservation") | |
plt.ylabel("Elapsed time (days)"); | |
plt.savefig('timeplot.png') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#將同一天的紀錄取平均畫出來 | |
nextday=1 | |
inds=[] | |
daily_mean_speed=[] | |
for (i,k) in enumerate(elapsed_days): | |
if k<nextday: | |
inds.append(i) | |
else: | |
daily_mean_speed.append(np.mean(data.speed_2d[inds])) | |
nextday+=1 | |
inds=[] | |
plt.figure(figsize=(8,6)) | |
plt.plot(daily_mean_speed) | |
plt.xlabel('Day') | |
plt.ylabel('Mean speed (m/s)'); | |
plt.savefig('dms.png') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cartopy.crs as ccrs | |
import cartopy.feature as cfeature | |
proj=ccrs.Mercator() | |
plt.figure(figsize=(10,10)) | |
ax=plt.axes(projection=proj) | |
ax.set_extent((-25.0,20.0,52.0,10.0)) | |
ax.add_feature(cfeature.LAND) | |
ax.add_feature(cfeature.OCEAN) | |
ax.add_feature(cfeature.COASTLINE) | |
ax.add_feature(cfeature.BORDERS, linestyle=':') | |
for name in bird_names: | |
ix=birddata['bird_name']==name | |
x,y = birddata.longitude[ix],birddata.latitude[ix] | |
ax.plot(x,y,'.',transform=ccrs.Geodetic(),label=name) | |
plt.legend(loc="upper left") | |
plt.savefig("map.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First, use `groupby()` to group the data by "bird_name". | |
grouped_birds =birddata.groupby('bird_name') | |
# Now calculate the mean of `speed_2d` using the `mean()` function. | |
mean_speeds = grouped_birds['speed_2d'].mean() | |
# Use the `head()` method prints the first 5 lines of each bird. | |
grouped_birds.head() | |
# Find the mean `altitude` for each bird. | |
mean_altitudes = grouped_birds['altitude'].mean() | |
# Convert birddata.date_time to the `pd.datetime` format. | |
birddata.date_time = pd.to_datetime(birddata.date_time) | |
# Create a new column of day of observation | |
birddata["date"] = birddata.date_time.dt.date #只抓日期 | |
# Check the head of the column. | |
birddata["date"].head() | |
# Use `groupby()` to group the data by date. | |
grouped_bydates = birddata.groupby('date') | |
# Find the mean `altitude` for each date. | |
mean_altitudes_perday =grouped_bydates.altitude.mean() | |
# Use `groupby()` to group the data by bird and date. | |
grouped_birdday = birddata.groupby(['bird_name','date']) | |
# Find the mean `altitude` for each bird and date. | |
mean_altitudes_perday =grouped_birdday.altitude.mean() | |
# look at the head of `mean_altitudes_perday`. | |
mean_altitudes_perday.head() | |
#自己重新的另外一個寫法 | |
#eric_daily_speed = birddata[birddata.bird_name=='Eric'].groupby('date').speed_2d.mean() | |
#sanne_daily_speed = birddata[birddata.bird_name=='Sanne'].groupby('date').speed_2d.mean() | |
#nico_daily_speed = birddata[birddata.bird_name=='Nico'].groupby('date').speed_2d.mean() | |
eric_daily_speed = grouped_birdday.speed_2d.mean()["Eric"] | |
sanne_daily_speed = grouped_birdday.speed_2d.mean()["Sanne"] | |
nico_daily_speed = grouped_birdday.speed_2d.mean()["Nico"] | |
plt.plot(eric_daily_speed,label="Eric") | |
plt.plot(sanne_daily_speed,label="Sanne") | |
plt.plot(nico_daily_speed,label="Nico") | |
plt.xticks(fontsize=8) | |
plt.legend(loc="upper left") | |
plt.savefig('speed of each bird per day') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment