Earthquake is a natural phenomenon whose occurrence predictability is still a hot topic in academia. This is because of the destructive power it holds. In this article, we’ll learn how to analyze and visualize earthquake data with Python and Matplotlib.
Importing Libraries and Dataset
Python libraries make it very easy for us to handle the data and perform typical and complex tasks with a single line of code.
- Pandas – This library helps to load the data frame in a 2D array format and has multiple functions to perform analysis tasks in one go.
- Matplotlib/Seaborn – This library is used to draw visualizations.
Python3
import pandas as pd import matplotlib.pyplot as plt import seaborn as sb import warnings warnings.filterwarnings( 'ignore' ) |
Now, let’s load the dataset into the panda’s data frame for easy analysis.
Python3
df = pd.read_csv( 'dataset.csv' ) df.head() |
Output:
The dataset we are using here contains data for the following columns:
- Origin time of the Earthquake
- Latitude and the longitude of the location.
- Depth – This means how much depth below the earth’s level the earthquake started.
- The magnitude of the earthquake
- Location
Python3
df.shape |
Output:
(2719, 6)
Now let’s see which data is present in which type of data format.
Python3
df.info() |
Output:
Looking at the descriptive statistical measures also gives us some idea regarding the distribution of the data.
Python3
df.describe() |
Output:
From the above description of the dataset, we can conclude that:
- The maximum magnitude of the Earthquake is 7.
- The maximum depth at which the earthquake started is 471 km below the ground.
Feature Engineering
Feature Engineering helps to derive some valuable features from the existing ones. These extra features sometimes help in increasing the performance of the model significantly and certainly help to gain deeper insights into the data.
Python3
splitted = df[ 'Origin Time' ]. str .split( ' ' , n = 1 , expand = True ) df[ 'Date' ] = splitted[ 0 ] df[ 'Time' ] = splitted[ 1 ]. str [: - 4 ] df.drop( 'Origin Time' , axis = 1 , inplace = True ) df.head() |
Output:
Now, let’s divide the date column into the day, month, and year columns respectively.
Python3
splitted = df[ 'Date' ]. str .split( '-' , expand = True ) df[ 'day' ] = splitted[ 2 ].astype( 'int' ) df[ 'month' ] = splitted[ 1 ].astype( 'int' ) df[ 'year' ] = splitted[ 0 ].astype( 'int' ) df.drop( 'Date' , axis = 1 , inplace = True ) df.head() |
Output:
Exploratory Data Analysis
EDA is an approach to analyzing the data using visual techniques. It is used to discover trends, and patterns, or to check assumptions with the help of statistical summaries and graphical representations.
Python3
plt.figure(figsize = ( 10 , 5 )) x = df.groupby( 'year' ).mean()[ 'Depth' ] x.plot.bar() plt.show() |
Output:
The depth from which earthquakes are starting is reducing with every passing year.
Python3
plt.figure(figsize = ( 10 , 5 )) sb.lineplot(data = df, x = 'month' , y = 'Magnitude' ) plt.show() |
Output:
Here we can observe that the changes of an earthquake with higher magnitude are more observed during the season of monsoon.
Python3
plt.subplots(figsize = ( 15 , 5 )) plt.subplot( 1 , 2 , 1 ) sb.distplot(df[ 'Depth' ]) plt.subplot( 1 , 2 , 2 ) sb.boxplot(df[ 'Depth' ]) plt.show() |
Output:
From the distribution graph, it is visible that there are some outliers that can be confirmed by using the boxplot. But the main point to observe here is that the distribution of the depth at which the earthquake rises is left-skewed.
Python3
plt.subplots(figsize = ( 15 , 5 )) plt.subplot( 1 , 2 , 1 ) sb.distplot(df[ 'Magnitude' ]) plt.subplot( 1 , 2 , 2 ) sb.boxplot(df[ 'Magnitude' ]) plt.show() |
Output:
As we know that many natural phenomena follow a normal distribution and here we can observe that the magnitude of the earthquake also follows a normal distribution.
Python3
plt.figure(figsize = ( 10 , 8 )) sb.scatterplot(data = df, x = 'Latitude' , y = 'Longitude' , hue = 'Magnitude' ) plt.show() |
Output:
Now by using Plotly let’s plot the latitude and the longitude data on the map to visualize which areas are more prone to earthquakes.
Python3
import plotly.express as px import pandas as pd fig = px.scatter_geo(df, lat = 'Latitude' , lon = 'Longitude' , color = "Magnitude" , fitbounds = 'locations' , scope = 'asia' ) fig.show() |
Output: