Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Analyze and Visualize Earthquake Data in Python with Matplotlib
Earthquakes are natural phenomena that can have significant effects on human life and infrastructure. With the availability of geospatial and seismic datasets, scientists and researchers can analyze and visualize earthquake data to identify patterns, trends, and risk zones.
Python, along with libraries like Matplotlib, Pandas, and NumPy, provides powerful tools to process and visualize this data. In this article, we will analyze and visualize earthquake data in Python with Matplotlib.
Dataset Overview
For the examples in this article, we use a dataset that contains information about earthquakes. The data is stored in rows and columns, where each row represents one earthquake event and the columns represent various attributes of that event.
Here's our sample dataset structure:
| time | latitude | longitude | depth | magnitude | place |
|---|---|---|---|---|---|
| 2025-05-12T14:32:10Z | 38.456 | 142.333 | 10.0 | 6.8 | Punjab |
| 2025-04-25T06:20:15Z | -6.212 | 100.840 | 70.0 | 5.8 | Delhi |
| 2025-03-17T03:45:30Z | 30.012 | -115.233 | 8.5 | 4.8 | Hyderabad |
| 2025-02-10T22:10:05Z | 35.789 | 138.342 | 12.2 | 5.2 | Mumbai |
| 2025-02-28T08:05:50Z | -33.453 | -70.456 | 32.0 | 6.4 | Goa |
Creating Sample Dataset
First, let's create a sample dataset to work with:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Create sample earthquake data
data = {
'time': ['2025-01-15', '2025-01-22', '2025-02-05', '2025-02-18', '2025-03-10',
'2025-03-25', '2025-04-08', '2025-04-20', '2025-05-12', '2025-05-28'],
'latitude': [38.456, -6.212, 30.012, 35.789, -33.453,
40.123, 25.678, -15.234, 45.890, 28.456],
'longitude': [142.333, 100.840, -115.233, 138.342, -70.456,
-120.789, 85.123, 145.678, -95.234, 77.890],
'depth': [10.0, 70.0, 8.5, 12.2, 32.0, 15.8, 45.3, 22.1, 18.7, 35.4],
'magnitude': [6.8, 5.8, 4.8, 5.2, 6.4, 4.5, 5.9, 4.2, 6.1, 5.0],
'place': ['Punjab', 'Delhi', 'Hyderabad', 'Mumbai', 'Goa',
'Chennai', 'Kolkata', 'Bangalore', 'Ahmedabad', 'Jaipur']
}
# Create DataFrame
earthquake_df = pd.DataFrame(data)
earthquake_df['time'] = pd.to_datetime(earthquake_df['time'])
print(earthquake_df.head())
time latitude longitude depth magnitude place
0 2025-01-15 38.456 142.333 10.0 6.8 Punjab
1 2025-01-22 -6.212 100.840 70.0 5.8 Delhi
2 2025-02-05 30.012 -115.233 8.5 4.8 Hyderabad
3 2025-02-18 35.789 138.342 12.2 5.2 Mumbai
4 2025-03-10 -33.453 -70.456 32.0 6.4 Goa
Temporal Trends Analysis
Let's analyze earthquake occurrences over time by plotting the number of earthquakes per month:
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'time': ['2025-01-15', '2025-01-22', '2025-02-05', '2025-02-18', '2025-03-10',
'2025-03-25', '2025-04-08', '2025-04-20', '2025-05-12', '2025-05-28'],
'magnitude': [6.8, 5.8, 4.8, 5.2, 6.4, 4.5, 5.9, 4.2, 6.1, 5.0]
}
earthquake_df = pd.DataFrame(data)
earthquake_df['time'] = pd.to_datetime(earthquake_df['time'])
# Extract month from time column
earthquake_df['month'] = earthquake_df['time'].dt.to_period('M')
# Count earthquakes per month
monthly_counts = earthquake_df.groupby('month').size()
# Create the plot
plt.figure(figsize=(10, 6))
monthly_counts.plot(kind='line', marker='o', color='red', linewidth=2, markersize=8)
plt.title('Monthly Earthquake Counts', fontsize=16, fontweight='bold')
plt.xlabel('Month', fontsize=12)
plt.ylabel('Number of Earthquakes', fontsize=12)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
This visualization helps identify temporal patterns in earthquake activity, showing whether certain months have higher seismic activity.
Magnitude Distribution Analysis
Let's visualize the distribution of earthquake magnitudes to understand the frequency of strong versus mild earthquakes:
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
magnitudes = [6.8, 5.8, 4.8, 5.2, 6.4, 4.5, 5.9, 4.2, 6.1, 5.0, 4.7, 5.5, 6.0, 4.9, 5.3]
# Create histogram
plt.figure(figsize=(10, 6))
plt.hist(magnitudes, bins=8, color='skyblue', edgecolor='navy', alpha=0.7)
plt.title('Distribution of Earthquake Magnitudes', fontsize=16, fontweight='bold')
plt.xlabel('Magnitude', fontsize=12)
plt.ylabel('Frequency', fontsize=12)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
print(f"Mean magnitude: {sum(magnitudes)/len(magnitudes):.2f}")
print(f"Max magnitude: {max(magnitudes)}")
print(f"Min magnitude: {min(magnitudes)}")
Mean magnitude: 5.35 Max magnitude: 6.8 Min magnitude: 4.2
Magnitude vs Depth Analysis
Let's create a scatter plot to examine the relationship between earthquake magnitude and depth:
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'depth': [10.0, 70.0, 8.5, 12.2, 32.0, 15.8, 45.3, 22.1, 18.7, 35.4,
25.6, 55.2, 40.1, 28.9, 65.7],
'magnitude': [6.8, 5.8, 4.8, 5.2, 6.4, 4.5, 5.9, 4.2, 6.1, 5.0,
4.7, 5.5, 6.0, 4.9, 5.3]
}
earthquake_df = pd.DataFrame(data)
# Create scatter plot
plt.figure(figsize=(10, 6))
plt.scatter(earthquake_df['depth'], earthquake_df['magnitude'],
c='maroon', alpha=0.7, s=100, edgecolors='black', linewidth=1)
plt.title('Magnitude vs Depth of Earthquakes', fontsize=16, fontweight='bold')
plt.xlabel('Depth (km)', fontsize=12)
plt.ylabel('Magnitude', fontsize=12)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# Calculate correlation
correlation = earthquake_df['depth'].corr(earthquake_df['magnitude'])
print(f"Correlation between depth and magnitude: {correlation:.3f}")
Correlation between depth and magnitude: -0.156
Geographic Distribution
Let's visualize the geographic distribution of earthquakes using latitude and longitude:
import pandas as pd
import matplotlib.pyplot as plt
# Sample geographic data
data = {
'latitude': [38.456, -6.212, 30.012, 35.789, -33.453, 40.123, 25.678, -15.234, 45.890, 28.456],
'longitude': [142.333, 100.840, -115.233, 138.342, -70.456, -120.789, 85.123, 145.678, -95.234, 77.890],
'magnitude': [6.8, 5.8, 4.8, 5.2, 6.4, 4.5, 5.9, 4.2, 6.1, 5.0]
}
earthquake_df = pd.DataFrame(data)
# Create geographic scatter plot
plt.figure(figsize=(12, 8))
scatter = plt.scatter(earthquake_df['longitude'], earthquake_df['latitude'],
c=earthquake_df['magnitude'], s=earthquake_df['magnitude']*30,
cmap='Reds', alpha=0.7, edgecolors='black', linewidth=1)
plt.title('Geographic Distribution of Earthquakes', fontsize=16, fontweight='bold')
plt.xlabel('Longitude', fontsize=12)
plt.ylabel('Latitude', fontsize=12)
plt.colorbar(scatter, label='Magnitude')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
This visualization shows earthquake locations worldwide, with color and size representing magnitude intensity.
Conclusion
Analyzing earthquake data with Python and Matplotlib enables scientists to identify patterns, assess risks, and support disaster preparedness strategies. Through temporal analysis, magnitude distribution, and geographic visualization, we can transform complex seismic data into meaningful insights for both technical and non-technical audiences.
