Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plot GBIF parakeet observation data
import os, sys, csv
import numpy as np
import pandas as pd
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import imageio
###################################################################################
# config:
specieslist = {
"Psittacula krameri": {'color':'g', 'label': 'rr parakeet'},
"Actitis hypoleucos": {'color':'y', 'label': 'c sandpiper'},
}
srcfiles = [os.path.expanduser(fpath) for fpath in [
"~/Documents/gbif/0028923-190918142434337-rrparakeet.csv",
"~/Documents/gbif/0029141-190918142434337-commonsandpiper.csv",
]]
yearmin = 2000
yearmax = 2019
###################################################################################
# load two sets of GBIF CSV data, trimming to only those with years in range, and observation positive
data = None
for asrc in srcfiles:
print("Loading file %s" % asrc)
df = pd.read_csv(asrc, sep='\t') #, dtype={'year':int})
print(" %i rows" % len(df))
# ensure trimmed to desired years and species, and also drop absence records
df = df[(df['verbatimScientificName'].isin(specieslist.keys())) & (df['year']>=yearmin) & (df['year']<yearmax) & (df['occurrenceStatus']!='absent')]
print(" %i rows after trimming" % len(df))
df.loc[df.individualCount.isna(),'individualCount'] = 1 # This is an assumption - if not mentioned, assuming they refer to one bird seen. Really, it's "presence" rather than "count".
if data is None:
data = df
else:
data = pd.concat([data, df])
print("%i rows in overall loaded dataset" % len(data))
###################################################################################
# Now iterate through the years, plotting
pdf = PdfPages("plot_gbif_parakeet.pdf")
animdata = []
if True:
for year in range(yearmin, yearmax):
fig, ax = plt.subplots(figsize=(6, 10))
for binom, spdata in specieslist.items():
subset = data[(data.year==year) & (data.verbatimScientificName==binom)]
plt.scatter(subset['decimalLongitude'], subset['decimalLatitude'], marker='.', alpha=0.4, color=spdata['color'], label=spdata['label'])
plt.legend()
plt.ylabel('Longitude')
plt.xlabel('Latitude')
plt.xlim(-6, 2)
plt.ylim(50, 58)
plt.title("%i" % (year))
pdf.savefig(fig)
fig.canvas.draw()
image = np.frombuffer(fig.canvas.tostring_rgb(), dtype='uint8')
image = image.reshape(fig.canvas.get_width_height()[::-1] + (3,))
animdata.append(image)
plt.close()
pdf.close()
imageio.mimsave('plot_gbif_parakeet.gif', animdata, fps=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.