# Predict the temperature rise for the next 500 years

Using a linear interpolation here is the trend in temperature increase.

[![linear trend.png](https://bookstack.dtomus.com/uploads/images/gallery/2025-01/7ctlinear-trend.png)](https://bookstack.dtomus.com/uploads/images/gallery/2025-01/linear-trend.png)

Interesting to see that the max temperatures will increase from around 20C to 23 in 500 years whereas the minimum temperatures have a steeper trend going from 11C to 19C for the same period of time. There will be hotter nights but days will remain almost the same.

Here is the code:

```python
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df_max = pd.read_csv('tmax.csv')
df_min = pd.read_csv('tmin.csv')

# Assuming df_max and df_min are your DataFrames
df_max = df_max.reset_index()
df_min = df_min.reset_index()

# Convert 'Date' column to datetime
df_max['Date'] = pd.to_datetime(df_max['Date'], dayfirst=True)
df_min['Date'] = pd.to_datetime(df_min['Date'], dayfirst=True)

# Calculate average temperature every year
df_max_year_avg = df_max.groupby(df_max['Date'].dt.year)['t_max'].mean().reset_index()
df_min_year_avg = df_min.groupby(df_min['Date'].dt.year)['t_min'].mean().reset_index()

# Rename columns
df_max_year_avg.columns = ['Year', 'Average Max Temperature']
df_min_year_avg.columns = ['Year', 'Average Min Temperature']

# Calculate slope and intercept of temperature trend
slope_max = (df_max_year_avg['Average Max Temperature'].iloc[-1] - df_max_year_avg['Average Max Temperature'].iloc[0]) / (df_max_year_avg['Year'].iloc[-1] - df_max_year_avg['Year'].iloc[0])
intercept_max = df_max_year_avg['Average Max Temperature'].iloc[0] - slope_max * df_max_year_avg['Year'].iloc[0]

slope_min = (df_min_year_avg['Average Min Temperature'].iloc[-1] - df_min_year_avg['Average Min Temperature'].iloc[0]) / (df_min_year_avg['Year'].iloc[-1] - df_min_year_avg['Year'].iloc[0])
intercept_min = df_min_year_avg['Average Min Temperature'].iloc[0] - slope_min * df_min_year_avg['Year'].iloc[0]

# Extrapolate temperature trend for next 500 years
years_future = np.arange(df_max_year_avg['Year'].iloc[-1] + 1, df_max_year_avg['Year'].iloc[-1] + 501)
temp_max_future = slope_max * years_future + intercept_max
temp_min_future = slope_min * years_future + intercept_min

# Plotting
plt.figure(figsize=(16,6))
plt.plot(df_max_year_avg['Year'], df_max_year_avg['Average Max Temperature'], color='red', label='MaxTemp')
plt.plot(df_min_year_avg['Year'], df_min_year_avg['Average Min Temperature'], color='blue', label='MinTemp')
plt.plot(years_future, temp_max_future, color='red', linestyle='--', label='MaxTemp Future')
plt.plot(years_future, temp_min_future, color='blue', linestyle='--', label='MinTemp Future')
plt.legend()
plt.xlabel('Year')
plt.ylabel('Temperature, C')
plt.grid(axis='y', linestyle='--', linewidth=0.5)
plt.title('Average Temperature every year')
plt.xticks(rotation=45)
plt.show()
```