[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math

Search:


View post   

>> No.15585593 [View]
File: 40 KB, 1000x600, avg_latitude_per_year 2.png [View same] [iqdb] [saucenao] [google]
15585593

>>15585571
Fair enough. OP did write US. Also, I excluded all the precipitation measurements and only kept the TMAX (which is the quantity, OP is looking at).
Here's the updated version. Anything else wrong with the plot that you cannot fix yourself?

import pandas as pd
import matplotlib.pyplot as plt

# Define column names
columns = ['ID', 'Latitude', 'Longitude', 'Type', 'StartYear', 'EndYear']

# Load data from .txt file into pandas DataFrame
df = pd.read_csv('ghcnd-inventory.txt', delim_whitespace=True, names=columns)

# Filter rows where ID starts with 'US' and Type is 'TMAX'
df = df[df['ID'].str.startswith('US') & (df['Type'] == 'TMAX')]

# Create an empty DataFrame to store average latitude per year
avg_latitude = pd.DataFrame(columns=['Year', 'AvgLatitude'])

# Define the range of years to consider
start_year = 1900
end_year = 2023

# Iterate through the unique start and end years in the original DataFrame
for year in range(start_year, end_year + 1):
# Filter rows that are active in the current year
active_rows = df[(df['StartYear'] <= year) & (df['EndYear'] >= year)]

# Calculate average latitude
avg_lat = active_rows['Latitude'].mean()

# Append the year and the average latitude to the new DataFrame
avg_latitude = avg_latitude.append({'Year': year, 'AvgLatitude': avg_lat}, ignore_index=True)

# Plotting
plt.figure(figsize=(10, 6))
plt.plot(avg_latitude['Year'], avg_latitude['AvgLatitude'])
plt.title('Average Latitude per Year')
plt.xlabel('Year')
plt.ylabel('Average Latitude')
plt.grid(True)
plt.savefig('avg_latitude_per_year.png')

Navigation
View posts[+24][+48][+96]