Whale distribution (OBIS SEAMAP)

Here we download and plot “humpback whale sightings” from the OBIS SEAMAP database.


First, download the data as a .csv file. Then use following code:

import csv
import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

# Open file
f = open('species_180530_points.csv', 'r')

# Read file
r = csv.DictReader(f)

# Initialize empty variables
lat, lon, num = [],[],[]

# Loop to parse data into our variables
errors = 0
for row in r:
    try: # The "Try/except" routine, prevents the loop to break if there is an empty field
        if not row['count'] == '':
            num.append(1) # If "count" is empty, replace it with a 1
    except ValueError:
        errors = errors + 1 # Simple error counter

# Set up projection and limits
m1 = Basemap(projection='mill',llcrnrlat=-90,urcrnrlat=90,\

# Given the projection, estimate plot coordinates from lats and lons
x,y = m1(lon, lat)

# Project data points into a grid
nbins = 200
H, xedges, yedges = np.histogram2d(x,y,bins=nbins,weights=num)

# Rotate and flip H...
H = np.rot90(H)
H = np.flipud(H)

# Mask zeros
Hmasked = np.ma.masked_where(H==0,H)

# Log H for better display
Hmasked = np.log10(Hmasked)

# Make map
fig1 = plt.figure()
cs = m1.pcolor(xedges,yedges,Hmasked,shading='flat')
plt.title('Distribution of humpback whales')
cbar = plt.colorbar(orientation='horizontal', extend='both')
cbar.ax.set_xlabel('Number of whales')

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s