Вот как выглядят первые пару строк набора данных
Проблема в том, что программа не выводит эти данные. Я не уверен, что происходит, поскольку моя программа печатает другой набор данных за период с 2017 по 2021 год.
Вот что у меня есть на данный момент. Обратите внимание, что я новичок в Python.
Код: Выделить всё
import csv
import datetime
import numpy as np
import matplotlib.pyplot as plt
# Define the years of interest
years_of_interest = [2017, 2018, 2019, 2020, 2021]
# Initialize dictionaries to hold the data
sea_ice_data = {year: {} for year in years_of_interest}
avg_data = {}
Код: Выделить всё
# Read the daily sea ice extent data
with open('sea_ice_extent_daily.gsfs.nasateam.1978-2021.csv', 'r') as f:
reader = csv.reader(f)
header = next(reader) # Skip header line
for row in reader:
if not row:
continue # Skip empty rows
date_str = row[0] # Date in 'YYYY-MM-DD' format
try:
day_of_year = int(row[1]) # Julian day
sea_ice_extent = float(row[3]) # Sea ice extent is assumed to be in the fourth column
except (ValueError, IndexError):
continue # Skip rows with invalid or missing data
# Extract the year from the date
try:
date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
year = date.year
except ValueError:
continue # Skip rows with invalid date formats
# Store the data if the year is in the years of interest
if year in years_of_interest:
sea_ice_data[year][day_of_year] = sea_ice_extent
Код: Выделить всё
# Read the average sea ice extent data
with open('avg_sea_ice_extent_modified.csv', 'r') as f:
reader = csv.reader(f)
header = next(reader) # Skip header line
for row in reader:
if not row:
continue # Skip empty rows
try:
day_of_year = int(row[0])
avg_extent = float(row[1])
percentile_10th = float(row[4])
percentile_90th = float(row[8])
except (ValueError, IndexError):
continue # Skip rows with invalid or missing data
avg_data[day_of_year] = {
'avg': avg_extent,
'p10': percentile_10th,
'p90': percentile_90th
}
Код: Выделить всё
# Prepare data for plotting
days_of_year = range(1, 367) # Days from 1 to 366 (leap years included)
# Initialize dictionaries to hold ordered data for plotting
sea_ice_extents = {}
for year in years_of_interest:
extents = []
for day in days_of_year:
extent = sea_ice_data[year].get(day, np.nan)
extents.append(extent)
sea_ice_extents[year] = extents
# Prepare average, 10th, and 90th percentile data
avg_extents = []
p10_extents = []
p90_extents = []
for day in days_of_year:
data = avg_data.get(day, {'avg': np.nan, 'p10': np.nan, 'p90': np.nan})
avg_extents.append(data['avg'])
p10_extents.append(data['p10'])
p90_extents.append(data['p90'])
Код: Выделить всё
# Plotting the data
plt.figure(figsize=(14, 7))
# Plot sea ice extent for each year
for year in years_of_interest:
plt.plot(days_of_year, sea_ice_extents[year], label=str(year))
# Plot average, 10th percentile, and 90th percentile
plt.plot(days_of_year, avg_extents, label='Average (1981-2010)', color='black', linewidth=2)
plt.plot(days_of_year, p10_extents, label='10th Percentile (1981-2010)', color='gray', linestyle='--')
plt.plot(days_of_year, p90_extents, label='90th Percentile (1981-2010)', color='gray', linestyle='--')
# Customize the plot
plt.xlabel('Day of Year')
plt.ylabel('Sea Ice Extent (million square km)')
plt.title('Northern Hemisphere Sea Ice Extent (2017-2021)')
plt.legend(loc='upper right')
plt.grid(True)
plt.tight_layout()
# Show the plot
plt.show()
Я пытался изменить номера столбцов, чтобы прочитать набор данных, но безрезультатно. Не знаю, что еще мне делать, поскольку я новичок в Python и программировании в целом.
Подробнее здесь: https://stackoverflow.com/questions/790 ... ph-and-one