Sea Level Predictor

python
Published

January 1, 2024

%pip install scipy pandas matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from scipy.stats import linregress
# Read data from file
df = pd.read_csv("data/epa-sea-level.csv")

print(df.describe())
print(df.info())
print(df.head())
              Year  CSIRO Adjusted Sea Level  Lower Error Bound  \
count   134.000000                134.000000         134.000000   
mean   1946.500000                  3.650341           3.204666   
std      38.826537                  2.485692           2.663781   
min    1880.000000                 -0.440945          -1.346457   
25%    1913.250000                  1.632874           1.078740   
50%    1946.500000                  3.312992           2.915354   
75%    1979.750000                  5.587598           5.329724   
max    2013.000000                  9.326772           8.992126   

       Upper Error Bound  NOAA Adjusted Sea Level  
count         134.000000                21.000000  
mean            4.096016                 7.363746  
std             2.312581                 0.691038  
min             0.464567                 6.297493  
25%             2.240157                 6.848690  
50%             3.710630                 7.488353  
75%             5.845472                 7.907365  
max             9.661417                 8.546648  
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 134 entries, 0 to 133
Data columns (total 5 columns):
 #   Column                    Non-Null Count  Dtype  
---  ------                    --------------  -----  
 0   Year                      134 non-null    int64  
 1   CSIRO Adjusted Sea Level  134 non-null    float64
 2   Lower Error Bound         134 non-null    float64
 3   Upper Error Bound         134 non-null    float64
 4   NOAA Adjusted Sea Level   21 non-null     float64
dtypes: float64(4), int64(1)
memory usage: 5.4 KB
None
   Year  CSIRO Adjusted Sea Level  Lower Error Bound  Upper Error Bound  \
0  1880                  0.000000          -0.952756           0.952756   
1  1881                  0.220472          -0.732283           1.173228   
2  1882                 -0.440945          -1.346457           0.464567   
3  1883                 -0.232283          -1.129921           0.665354   
4  1884                  0.590551          -0.283465           1.464567   

   NOAA Adjusted Sea Level  
0                      NaN  
1                      NaN  
2                      NaN  
3                      NaN  
4                      NaN  
def draw_plot():
    x = df["Year"]
    y = df["CSIRO Adjusted Sea Level"]

    # Create scatter plot
    plt.scatter(x, y)

    # Create first line of best fit
    # Adapted from https://stackoverflow.com/questions/61205263/how-can-i-extend-a-linear-regression-line-and-predict-the-future
    extended_x_1 = [*range(df.iloc[0, 0], 2051, 1)]

    res = linregress(x, y)

    line = [res.slope * x + res.intercept for x in extended_x_1]

    plt.plot(extended_x_1, line)

    # Create second line of best fit
    x_2000_2050 = df.loc[df["Year"] >= 2000, "Year"]
    y_2000_2050 = df.loc[df["Year"] >= 2000, "CSIRO Adjusted Sea Level"]

    extended_x_2 = [*range(2000, 2051, 1)]

    res_2000_2050 = linregress(x_2000_2050, y_2000_2050)

    line_2000_2050 = [
        res_2000_2050.slope * x + res_2000_2050.intercept for x in extended_x_2
    ]

    plt.plot(extended_x_2, line_2000_2050)

    # Add labels and title
    plt.xlabel("Year")
    plt.ylabel("Sea Level (inches)")
    plt.title("Rise in Sea Level")

    # plt.savefig("sea_level_plot.png")
    return plt.gca()


plt.show(draw_plot())