Tuesday, January 31, 2017

Data Visualisation : Mapping of Earthquake data and its analysis



In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.basemap import Basemap
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')

Data

In [2]:
earth_quake = pd.read_csv("earthquake.csv")
In [3]:
earth_quake.head()
Out[3]:
Date Time Latitude Longitude Type Depth Depth Error Depth Seismic Stations Magnitude Magnitude Type ... Magnitude Seismic Stations Azimuthal Gap Horizontal Distance Horizontal Error Root Mean Square ID Source Location Source Magnitude Source Status
0 01/02/1965 13:44:18 19.246 145.616 Earthquake 131.6 NaN NaN 6.0 MW ... NaN NaN NaN NaN NaN ISCGEM860706 ISCGEM ISCGEM ISCGEM Automatic
1 01/04/1965 11:29:49 1.863 127.352 Earthquake 80.0 NaN NaN 5.8 MW ... NaN NaN NaN NaN NaN ISCGEM860737 ISCGEM ISCGEM ISCGEM Automatic
2 01/05/1965 18:05:58 -20.579 -173.972 Earthquake 20.0 NaN NaN 6.2 MW ... NaN NaN NaN NaN NaN ISCGEM860762 ISCGEM ISCGEM ISCGEM Automatic
3 01/08/1965 18:49:43 -59.076 -23.557 Earthquake 15.0 NaN NaN 5.8 MW ... NaN NaN NaN NaN NaN ISCGEM860856 ISCGEM ISCGEM ISCGEM Automatic
4 01/09/1965 13:32:50 11.938 126.427 Earthquake 15.0 NaN NaN 5.8 MW ... NaN NaN NaN NaN NaN ISCGEM860890 ISCGEM ISCGEM ISCGEM Automatic
5 rows × 21 columns
In [4]:
earth_quake.columns
Out[4]:
Index(['Date', 'Time', 'Latitude', 'Longitude', 'Type', 'Depth', 'Depth Error',
       'Depth Seismic Stations', 'Magnitude', 'Magnitude Type',
       'Magnitude Error', 'Magnitude Seismic Stations', 'Azimuthal Gap',
       'Horizontal Distance', 'Horizontal Error', 'Root Mean Square', 'ID',
       'Source', 'Location Source', 'Magnitude Source', 'Status'],
      dtype='object')
In [5]:
earth = earth_quake[["Date","Latitude","Longitude","Magnitude"]]
In [6]:
earth.head()
Out[6]:
Date Latitude Longitude Magnitude
0 01/02/1965 19.246 145.616 6.0
1 01/04/1965 1.863 127.352 5.8
2 01/05/1965 -20.579 -173.972 6.2
3 01/08/1965 -59.076 -23.557 5.8
4 01/09/1965 11.938 126.427 5.8
In [7]:
earth.tail()
Out[7]:
Date Latitude Longitude Magnitude
23407 12/28/2016 38.3917 -118.8941 5.6
23408 12/28/2016 38.3777 -118.8957 5.5
23409 12/28/2016 36.9179 140.4262 5.9
23410 12/29/2016 -9.0283 118.6639 6.3
23411 12/30/2016 37.3973 141.4103 5.5
In [8]:
earth["Date"] = pd.to_datetime(earth["Date"])
In [9]:
earth.shape
Out[9]:
(23412, 4)

Creating a Basemap instance

In [10]:
m = Basemap(projection="mill")

Converting from spherical to cartesian coordinates

In [11]:
longitudes = earth["Longitude"].tolist()
latitudes = earth["Latitude"].tolist()
x,y = m(longitudes,latitudes)

Generating a Scatter Plot

In [12]:
fig = plt.figure(figsize=(12,10))
plt.title("All affected areas")
m.scatter(x,y, s = 4, c = "blue")
m.drawcoastlines()
m.fillcontinents(color='coral',lake_color='aqua')
m.drawmapboundary()
m.drawcountries()
plt.show()

The Severity of an Earthquake

Minimum and Maximum Magnitude
In [13]:
minimum = earth["Magnitude"].min()
maximum = earth["Magnitude"].max()
average = earth["Magnitude"].mean()

print("Minimum:", minimum)
print("Maximum:",maximum)
print("Mean",average)
Minimum: 5.5
Maximum: 9.1
Mean 5.882530753460003
In [14]:
(n,bins, patches) = plt.hist(earth["Magnitude"], range=(0,10), bins=10)
plt.xlabel("Earthquake Magnitudes")
plt.ylabel("Number of Occurences")
plt.title("Overview of earthquake magnitudes")

print("Magnitude" +"   "+ "Number of Occurence")
for i in range(5, len(n)):
    print(str(i)+ "-"+str(i+1)+"         " +str(n[i]))
Magnitude   Number of Occurence
5-6         16058.0
6-7         6616.0
7-8         698.0
8-9         38.0
9-10         2.0
  • Over 16,000 (68.5%) earthquakes magnitude were between 5 and 6
  • Over 40 (0.17%) earthquakes magitude were greater than 8.
In [15]:
plt.boxplot(earth["Magnitude"])
plt.show()
In [16]:
highly_affected = earth[earth["Magnitude"]>=8]
In [17]:
print(highly_affected.shape)
(40, 4)
In [18]:
longitudes = highly_affected["Longitude"].tolist()
latitudes = highly_affected["Latitude"].tolist()
x,y = m(longitudes,latitudes)

fig = plt.figure(figsize=(12,10))
plt.title("Highly affected areas affected areas")
m.scatter(x,y,  c = "blue", s = highly_affected["Magnitude"] *20)
m.drawcoastlines()
m.fillcontinents(color='coral',lake_color='aqua')
m.drawmapboundary()
m.drawcountries()
plt.show()