Analyze and Visualize Earthquake Data in Python with Matplotlib

Fellow coders, in this tutorial we are going to learn to analyze and visualize earthquake data in Python using matplotlib. We will learn from where to download the CSV file for this tutorial and how to access the data using the pandas library in python. We will then visualize this data using matplotlib. We will calculate the total number of occurrences of earthquakes and the frequency of each earthquake. After that, we will plot our data on the world¬†map. So, let’s get started with this tutorial by writing our code:

Earthquake data visualization in Python with Matplotlib

Before starting to work with code, I want to mention that we will plot graphs using matplotlib at the beginning of the code but as we progress further we will use Matplotlib’s “basemap” library to visualize data on the world map. Before starting, make sure that you have all the libraries installed in your environment. I am working in an anaconda environment.

Import all the required libraries:

import requests
import csv
from csv import DictReader
import pandas as pd
import numpy as np
from pandas import Series, DataFrame

import matplotlib.pyplot as plt
from matplotlib import rcParams
import seaborn as sb

# below lines are important when you get KeyError: 'PROJ_LIB'
import os
import conda
conda_file_dir = conda.__file__
conda_dir = conda_file_dir.split('lib')[0]
proj_lib = os.path.join(os.path.join(conda_dir, 'share'), 'proj')
os.environ["PROJ_LIB"] = proj_lib

from mpl_toolkits.basemap import Basemap
%matplotlib inline
rcParams['figure.figsize'] = 8, 4

Now we need to download the CSV file for the data. Download the CSV file from the link below:

The above CSV file contains data of all the earthquakes past 30 days with a magnitude of 4.5 or higher.

address = '4.5_month.csv'
eq = pd.read_csv(address)

The output of the above code should look like the figure below:

Analyze and Visualize Earthquake Data in Python with Matplotlib

The variable¬†address contains the address of the CSV file that you just downloaded. ‘eq.head()’ function displays the top 5 rows of our data. Let us find out how many rows our data contains.




Now, let us check the frequency of all the earthquakes:

freq = eq['mag'].value_counts()


4.50    96
4.60    84
4.70    59
4.90    44
4.80    41
5.00    32
5.20    23
5.10    22
5.30    20
5.50    11
5.40     8
5.60     6
6.10     5
6.00     4
5.70     3
6.20     3
5.80     2
6.30     1
4.62     1
7.70     1
5.90     1
6.70     1
Name: mag, dtype: int64

Next, we will plot the frequency:

fig = plt.figure()
ax = fig.add_axes([.1, .1, 1, 1])


Earthquake data visualization in Python with Matplotlib

Further, Let us plot each magnitude value in the CSV file:

fig = plt.figure()
ax = fig.add_axes([.1, .1, 2, 1])


Earthquake data visualization in Python with Matplotlib


Now, here comes the interesting part of this tutorial. We are going to display all the earthquakes above the magnitude of 1.0 on the world map with the help of dots of color green, yellow and red.
To proceed further we must download the new CSV file containing data of earthquakes of magnitude 1.0+. Below is the link to download the CSV file:

After downloading the file, we can proceed further. Let us take a look at the code to display the data on the world map:
Note: Make sure you have imported all the required libraries.

eq_lat, eq_lon = [], []
magnitudes = []
eq_ts = []
with open('eq_1.csv') as f:
    reader = csv.reader(f)
    # skip the header row
    # here we store latitude and longitude in seperate lists
    for row in reader:

def mk_color(magnitude):
    # red color for significant earthquakes, yellow for earthquakes below 4.5 and above 3.0
    #  and green for earthquakes below 3.0
    if magnitude < 3.0:
        return ('go')
    elif magnitude < 4.5:
        return ('yo')
        return ('ro')

my_map = Basemap(projection='robin', resolution = 'l', area_thresh = 1000.0, lat_0=0, lon_0=-10)

my_map.fillcontinents(color = '#aa96da')
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))

mk_size = 2.4

for lon, lat, mag in zip(eq_lon, eq_lat, magnitudes):
    x,y = my_map(lon, lat)
    msize = mag * mk_size
    marker_string = mk_color(mag)
    my_map.plot(x, y, marker_string, markersize=msize)
plt.title('Earthquakes of magnitude 1.0 or above')

# we can save the image as png file locally to the directory we are working in

Here is the output of the code above:
Earthquake data visualization in Python with Matplotlib

I hope this tutorial was helpful.
Happy coding!

One response to “Analyze and Visualize Earthquake Data in Python with Matplotlib”

  1. Ewnetu Abebe says:

    yes it is excellent!
    And please Help me since I would work on earthquake prediction using machine learning and deep learning algorithm in Ethiopia.

Leave a Reply

Your email address will not be published.