我需要绘制一个图表,x 轴为“年”,y 轴为每年的“最大纬度”。我为每个观测创建了一个对象,但如何计算观测对象中每个不同年份的最大纬度?我有大约 5000 个对象,年份从 1996 年到 2021 年。 一个对象看起来像这样:
x = Obs(59.0193253,17.619529,'2021-07-28T22:00:00.0000000',1)
这是我的代码:
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()
"""
如何实现
max_latitude
函数,以便它返回每年的最大纬度?
效率稍低但易于理解的实现方式是每年循环并通过列表推导获得纬度的
max
。
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
更快的方法是使用
groupby
,它还需要对项目进行排序,下面的代码显示了它的用法,但它稍微复杂一些。
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