Tracking Bird Migration Using NumPy Module

by | Mar 19, 2022 | Coding, Python

Home » DIY » Tracking Bird Migration Using NumPy Module

Introduction of the Project

Hello! Do you like birds? Are you curious to know where all of these birds migrate when climate changes? Today, we will create a simple and efficient code for Tracking Bird Migration with NumPy Module in Python. We will plot their migrations, speed, and location on graphs and maps! Wow, so interesting! So, what are you waiting for? Let’s code it to find their migration route and speed.



1. To run the code, you need Python; you can use VSCode or any python IDE.

2. Pre-installed Numpy, Pandas & Matplotlib Module.

3. Download the Dataset CSV file.

Steps For Tracking Bird Migration Using NumPy Module

Step 1: Install the modules; if you haven’t it in your system, paste the below command lines in your command prompt and press enter.

pip install matplotlib

pip install pandas

pip install numpy

Step 2: Paste the below code in your IDE/code editor.

Source Code

# Import required modules
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import datetime

# To read the CSV file
birdData = pd.read_csv("C:\\Users\\misss\\Downloads\\bird_tracking.csv")

# To get the unique names of birds
birdNames = pd.unique(birdData.bird_name)

# To store the indices of the bird Eric
i = birdData.bird_name == "Eric"
x,y = birdData.longitude[i], birdData.latitude[i]

# Create a new figure, or activate an existing figure
plt.figure(figsize = (4.5,6))

# Plot each bird in the same figure
plt.figure(figsize = (4.5,6))

# Plot latitude & longitude in graph
for name in birdNames:
            i = birdData.bird_name == name
            x,y = birdData.longitude[i], birdData.latitude[i]
            plt.plot(x,y,".", label=name)

# Labelling Longitute and Latitude   
plt.legend(loc="lower right")

# To get the timestamps of a day
timestamps = []
for k in range(len(birdData)):
            timestamps.append(datetime.datetime.strptime(birdData.date_time.iloc[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)

# To find the mean speed
next_day = 1
inds = []
daily_mean_speed = []
for (i,t) in enumerate(elapsed_days):
            if t < next_day:
                        next_day += 1
                        inds = []

# For plotting the graph and labelling
plt.figure(figsize = (5,6))
plt.plot(daily_mean_speed, "rs-")
plt.xlabel(" Day ")
plt.ylabel(" Mean Speed (m/s) ");

# Display all open figures

Explanation Of The Code

In the beginning, we imported all the necessary modules into our code.

1. Firstly, we read the CSV file and get the unique names of the birds from it.

2. After this, we get the indices of the bird named Eric and plot the figure using the plot function.

3. After this, we plot latitude and longitude in the second figure using x and y coordinates.

4. We are labeling the x and y-axis using the label function.

5. Now, we are calculating the timestamp for our third figure.

6. After it, we find the mean speed for the same bird – Eric.

7. Then, we plot the graph and label it according to the axis.

8. We have shown the speed in meters per second, and the x-axis represents the days.

9. Finally, we display all the open figures using the show function.


Below are the results of successfully running the code for Tracking Bird Migration Using the NumPy Module.

Figures 1 and 2. show the migrating route of the birds in terms of latitude and longitude. 

Figure 3. shows the graphical representation of the mean speed of a bird per day when observed for a long time.

Tracking Bird Migration Using NumPy Module

Things to Remember 

  • Download the CSV file before proceeding with the code.
  • Install all the required modules before running the code.
  • Alter the CSV file path in your code according to your CSV file location.


You May Also Like To Create…


Submit a Comment

Your email address will not be published. Required fields are marked *