-
-
Save ageorgou/afc45ec535dcca1b6c84e30493b17f5d to your computer and use it in GitHub Desktop.
Earthquakes plotting solution (2021-2022)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A sample answer to https://github.com/UCL-MPHY0021-21-22/RSE-Classwork/issues/15.""" | |
import json | |
import statistics | |
from datetime import date | |
import matplotlib.pyplot as plt | |
import requests | |
def get_data(): | |
"""Retrieve the data we will be working with.""" | |
response = requests.get( | |
"http://earthquake.usgs.gov/fdsnws/event/1/query.geojson", | |
params={ | |
'starttime': "2000-01-01", | |
"maxlatitude": "58.723", | |
"minlatitude": "50.008", | |
"maxlongitude": "1.67", | |
"minlongitude": "-9.756", | |
"minmagnitude": "1", | |
"endtime": "2018-10-11", | |
"orderby": "time-asc"} | |
) | |
text = response.text | |
return json.loads(text) | |
def get_year(earthquake): | |
"""Extract the year in which an earthquake happened.""" | |
timestamp = earthquake['properties']['time'] | |
year = date.fromtimestamp(timestamp/1000).year | |
return year | |
def get_magnitude(earthquake): | |
"""Retrive the magnitude of an earthquake item.""" | |
return earthquake["properties"]["mag"] | |
def plot_average_magnitude_per_year(earthquakes): | |
magnitudes_per_year = get_magnitudes_per_year(earthquakes) | |
# Convert to lists for plotting | |
# Note that we have to ensure the same ordering in both lists! | |
# This is one way: | |
years = sorted(magnitudes_per_year.keys()) | |
magnitudes = [ | |
# the statistics module has some basic statistical functions | |
statistics.mean(magnitudes_per_year[year]) | |
for year in years | |
] | |
plt.plot(years, magnitudes) | |
plt.xlabel("Year") | |
plt.ylabel("Magnitude") | |
plt.title("Average earthquake magnitude per year") | |
plt.savefig("average_magnitudes.png") | |
def plot_number_per_year(earthquakes): | |
magnitudes_per_year = get_magnitudes_per_year(earthquakes) | |
# Convert to lists for plotting | |
# Note that we have to ensure the same ordering in both lists! | |
# This is one way: | |
years = sorted(magnitudes_per_year.keys()) | |
numbers = [len(magnitudes_per_year[year]) for year in years] | |
plt.bar(years, numbers) | |
plt.xlabel("Year") | |
plt.ylabel("Count") | |
plt.title("Number of earthquakes per year") | |
plt.savefig("frequencies.png") # Save the figure to a file | |
def get_magnitudes_per_year(earthquakes): | |
"""Retrieve the magnitudes of all the earthquakes in a given year. | |
Returns a dictionary with years as keys, and lists of magnitudes as values. | |
""" | |
magnitudes_per_year = {} | |
for earthquake in earthquakes: | |
year = get_year(earthquake) | |
magnitude = get_magnitude(earthquake) | |
if year not in magnitudes_per_year: # if we haven't seen this year before, add it | |
magnitudes_per_year[year] = [magnitude] | |
else: # otherwise record one more instance | |
magnitudes_per_year[year].append(magnitude) | |
return magnitudes_per_year | |
# Get the data we will work with | |
quakes = get_data()['features'] | |
# Plot the results - this is not perfect since the x axis is shown as real | |
# numbers rather than integers, which is what we would prefer! | |
plot_number_per_year(quakes) | |
plt.clf() # This clears the figure, so that we don't overlay the two plots | |
plot_average_magnitude_per_year(quakes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment