forked from gzavo/CS_Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
26 lines (20 loc) · 761 Bytes
/
plot.py
File metadata and controls
26 lines (20 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_csv("istherecorrelation.csv", sep=";", decimal=",")
beer_per_student = (df.iloc[:, 2] / df.iloc[:, 1])
x = df["Year"].to_numpy(dtype=float)
y = beer_per_student.to_numpy(dtype=float)
m, b = np.polyfit(x, y, 1)
y_hat = m * x + b
r2 = 1 - np.sum((y - y_hat)**2) / np.sum((y - y.mean())**2)
plt.figure(figsize=(8,5), dpi=150)
plt.plot(x, y, marker="o", label="Beer (hl) per 1000 WO students")
plt.plot(x, y_hat, linestyle="--", label=f"Linear fit (R²={r2:.3f})")
plt.xlabel("Year")
plt.ylabel("Hectoliters per 1000 students")
plt.title("Beer per 1000 WO Students (linear fit)")
plt.grid(True)
plt.legend()
plt.savefig("beer_per_student_linear_fit.png", dpi=300)
plt.show()