You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.2 KiB
Python

4 years ago
import matplotlib.pyplot as plt
11 months ago
import numpy as np
import pandas as pd
4 years ago
from scipy.stats import linregress
11 months ago
4 years ago
def draw_plot():
# Read data from file
11 months ago
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')
4 years ago
# Create scatter plot
11 months ago
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")
4 years ago
# Create first line of best fit
11 months ago
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)")
4 years ago
# Create second line of best fit
11 months ago
ax4 = ax.plot(span2, result2.intercept+result2.slope*span2, "y")
4 years ago
# Add labels and title
# Save plot and return data for testing (DO NOT MODIFY)
plt.savefig('sea_level_plot.png')
return plt.gca()