I need to plot a graph with on the x axis 'years' and on the y axis the 'max latitude' per year. I've created an object for each observations but how do I calculate the max latitude for each different year in my observation objects? I have about 5000 objects with years from 1996-2021. An object looks like this:
x = Obs(59.0193253,17.619529,'2021-07-28T22:00:00.0000000',1)
Here is my code:
import matplotlib
class Obs:
def __init__(self, latitude, longitude, time, quantity):
self.latitude = latitude
self.longitude = longitude
self.time = time
self.quantity = quantity
self.year = year_out_of_time(self)
def year_out_of_time(self):
date,time = self.time.split("T")
year,month,day = date.split("-")
year = int(year)
return year
def read_data(filename):
observations = []
try:
with open(filename, "r") as f:
next(f) # skips the first line
for line in f:
if line.endswith("Artportalen\n"):
latitude, longitude, time, quantity, Artportalen = line.split(",")
else:
latitude, longitude, time, quantity = line.split(",")
latitude = float(latitude)
longitude = float(longitude)
quantity = int(quantity.strip())
time = time.strip("Z")
observations.append(Obs(latitude, longitude, time, quantity))
except Exception as e:
print("Error:", e)
return observations
def years(observations):
out = []
for obs in observations:
if not obs.year in out:
out.append(obs.year)
out = sorted(out)
return out
def max_latitude(observations):
pass
def plot_data(observations):
xpoints = years(observations)
ypoints = max_latitude(observations)
"""
plt.title("max latitude")
plt.xlabel("Year")
plt.ylabel("max latitude")
plt.scatter(xpoints, ypoints)
plt.show()
"""
How do I implement the max_latitude
function so that it returns the max latitude for each year?