0

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?

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33

1 Answers1

0

a slightly inefficient but easy to understand implementation would be to just loop over each year and get the max of latitudes in it with list comprehensions.

def max_latitude(observations):
    years_list = sorted(list(set(x.year for x in observations)))
    max_latitude_list = []
    for year in years_list:
        max_value = max(x.latitude for x in observations if x.year == year)
        max_latitude_list.append(max_value)
    return years_list, max_latitude_list

a more faster approach would use groupby, which also needs items to be sorted, the following code shows its use, but it's slightly more involved.

import itertools
def max_latitude(observations):
    years_list = []
    max_latitude_list = []
    # returns the year and the observations in that year in a list.
    for year, observations_in_year_list in itertools.groupby(
            sorted(observations, key=lambda x:x.year), key=lambda x: x.year):
        years_list.append(year)
        max_latitude_list.append(max(x.latitude for x in observations_in_year_list))
    return years_list, max_latitude_list
Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23