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
27 lines (20 loc) · 848 Bytes
/
plot.py
File metadata and controls
27 lines (20 loc) · 848 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
27
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_csv("istherecorrelation.csv", sep=";")
df["WO [x1000]"] = df["WO [x1000]"].str.replace(",", ".").astype(float)
x = df["WO [x1000]"]
y = df["NL Beer consumption [x1000 hectoliter]"]
plt.figure(dpi=300)
plt.scatter(x, y, color="blue", label="Data points")
coeffs = np.polyfit(x, y, 1)
poly_eq = np.poly1d(coeffs)
plt.plot(x, poly_eq(x), color="red", label=f"Fit line: y={coeffs[0]:.2f}x+{coeffs[1]:.2f}")
plt.xlabel("WO (x1000 students)")
plt.ylabel("Beer consumption (x1000 hectoliter)")
plt.title("Relationship between WO and Beer consumption in NL")
plt.legend()
plt.savefig("myplot.png")
plt.show()
r = df["WO [x1000]"].corr(df["NL Beer consumption [x1000 hectoliter]"])
print("Correlation coefficient r =", r)