With the increasing availability of large datasets and powerful computational tools, geospatial analysis has become an essential tool for understanding a wide range of phenomena. In this article, we explore how Python and libraries such as pandas, matplotlib, and Folium can be used to investigate seismic activity, using a dataset of earthquakes in Italy from August to November 2016.
Loading the Dataset
Our first step was to load the earthquake data using pandas, a powerful data analysis library in Python. The dataset contained information about the time, location (latitude and longitude), depth, and magnitude of each earthquake.
import pandas as pd
# Load the dataset
data = pd.read_csv('italy_earthquakes_from_2016-08-24_to_2016-11-30.csv')
Visualizing Earthquake Locations with Folium
To visualize the geographic distribution of earthquakes, we used Folium, a Python library that simplifies the process of creating interactive maps.
We first calculated the median latitude and longitude to center our map. Then, for each earthquake, we added a marker to the map, color-coded based on the magnitude of the earthquake.
import folium
# Calculate median coordinates to center the map
median_latitude = data['Latitude'].median()
median_longitude = data['Longitude'].median()
# Create a map centered at the median coordinates
m = folium.Map(location=[median_latitude, median_longitude], zoom_start=7)
# Add a marker for each earthquake
for idx, row in data.iterrows():
# Color code markers based on magnitude
if row['Magnitude'] < 3.0:
color = 'green'
elif row['Magnitude'] < 4.0:
color = 'orange'
else:
color = 'red'
folium.CircleMarker(
location=[row['Latitude'], row['Longitude']],
radius=5,
popup=f"Magnitude: {row['Magnitude']}\nDepth: {row['Depth/Km']}",
color=color,
fill=True,
fill_color=color
).add_to(m)
Analyzing Earthquake Depth and Magnitude
To understand the relationship between the depth and magnitude of earthquakes, we created a scatter plot using matplotlib. This revealed a weak negative correlation, suggesting that deeper earthquakes tend to have slightly lower magnitudes, and vice versa.
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
plt.scatter(data['Depth/Km'], data['Magnitude'], alpha=0.5)
plt.title('Scatter plot of Earthquake Magnitude vs Depth')
plt.xlabel('Depth/Km')
plt.ylabel('Magnitude')
plt.grid(True)
plt.show()
Time Series Analysis of Earthquake Frequency and Magnitude
Finally, we performed a time series analysis to understand how the frequency and magnitude of earthquakes changed over time. We resampled the data to get the daily counts of earthquakes and the daily average magnitudes. Then, we plotted these over time.
# Convert 'Time' column to datetime
data['Time'] = pd.to_datetime(data['Time'])
# Set 'Time' as the index
data.set_index('Time', inplace=True)
# Resample data by day and calculate count and average magnitude
daily_counts = data.resample('D').size()
daily_average_magnitudes = data['Magnitude'].resample('D').mean()
# Plot the daily counts and average magnitudes
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8), sharex=True)
ax1.plot(daily_counts, marker='o', linestyle='-')
ax1.set(ylabel='Daily Counts', title='Number of Earthquakes per Day')
ax2.plot(daily_average_magnitudes, marker='o', linestyle='-', color='r')
ax2.set(xlabel='Date', ylabel='Average Magnitude', title='Average Magnitude of Earthquakes per Day')
plt.tight_layout()
plt.show()
Conclusion
Through this analysis, we were able to gain insights into the spatial distribution, depth, magnitude, and temporal patterns of earthquakes in Italy over a three-month period. Tools like pandas, matplotlib, and Folium made it possible to load, process, visualize, and analyze this data effectively. As we continue to accumulate more and larger datasets on seismic activity and other geospatial phenomena, these tools will become increasingly valuable for understanding and responding to the challenges they pose.