1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Trying to plot two data sets pertaining to sea ice data on to one graph and one of...

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024 às 20:32.

  1. Stack

    Stack Membro Participativo

    I am trying to plot two separate datasets pertaining to sea ice data onto one graph. The first data set concerns the years 2017 to 2021, and the second set looks at the average sea extent by day for the period 1981-2010. This data set also has statistical information, as shown below. For this problem, I am only working with the AverageExtent, the 10th, and the 90th standard deviation. I also included the first 4 days of the data, as there are 365 days in total.

    This is what the first couple of lines of the dataset looks like

    The problem is that the program won't print this data. I'm not sure what is going on since my program is printing the other data set for the years 2017 to 2021.

    Here is what I have so far. Please note that I am new to 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()


    enter image description here: This is the resulting graph.

    I have tried changing the column numbers in order to read the dataset to no avail. Not sure what else I should do, as I am new to python and coding in general.

    Continue reading...

Compartilhe esta Página