sea-level-predictor/sea_level_predictor.py

35 lines
1.2 KiB
Python
Raw Permalink Normal View History

2020-09-29 16:50:39 +02:00
import matplotlib.pyplot as plt
2023-06-11 00:48:29 +02:00
import numpy as np
import pandas as pd
2020-09-29 16:50:39 +02:00
from scipy.stats import linregress
2023-06-11 00:48:29 +02:00
2020-09-29 16:50:39 +02:00
def draw_plot():
# Read data from file
2023-06-11 00:48:29 +02:00
df = pd.read_csv("epa-sea-level.csv")
result = linregress(x=df["Year"], y=df["CSIRO Adjusted Sea Level"])
result2 = linregress(x=df["Year"][df["Year"] >= 2000], y=df["CSIRO Adjusted Sea Level"][df["Year"] >= 2000])
span = np.arange(1880, 2051, dtype='float64')
span2 = np.arange(2000, 2051, dtype='float64')
2020-09-29 16:50:39 +02:00
# Create scatter plot
2023-06-11 00:48:29 +02:00
fig, ax = plt.subplots(ncols=1, figsize=(10, 10))
x1 = df.plot.scatter(x="Year", y="CSIRO Adjusted Sea Level", color="b", ax=ax).set(ylabel="Sea Level (inches)", title="Rise in Sea Level")
2020-09-29 16:50:39 +02:00
# Create first line of best fit
2023-06-11 00:48:29 +02:00
ax2 = ax.plot(span, result.intercept+result.slope*span, "r")
# ax3 = df.plot.scatter(x="Year", y="NOAA Adjusted Sea Level", color="r", ax=ax).set(ylabel="Sea Level (inches)")
2020-09-29 16:50:39 +02:00
# Create second line of best fit
2023-06-11 00:48:29 +02:00
ax4 = ax.plot(span2, result2.intercept+result2.slope*span2, "y")
2020-09-29 16:50:39 +02:00
# Add labels and title
# Save plot and return data for testing (DO NOT MODIFY)
plt.savefig('sea_level_plot.png')
return plt.gca()