Hey guys I am learning about matplotlib right now and linear functions. So I got this task at codecademy:
"We have provided a slope, m, and an intercept, b, that seems to describe the revenue data you have been given.
Create a new list, y, that has every element in months, multiplied by m and added to b.
A list comprehension is probably the easiest way to do this!"
So my solution looks like this (I am going to print the whole code in for simplicity):
import codecademylib3_seaborn
import matplotlib.pyplot as plt
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
revenue = [52, 74, 79, 95, 115, 110, 129, 126, 147, 146, 156, 184]
#slope:
m = 10
#intercept:
b = 53
y = 0
for x in months:
y += m*x + b
plt.plot(months, revenue, "o")
plt.plot(months, y)
plt.show()
Well, I have no Idea why but it doesnt work then if I want to plot y. So I looked up what they would do diffrent, and it looks like this. They changed y.
y = [m*x + b for x in months]
I have no idea why this works or how this works. I think not only the code looks awful, also the syntax can't be right. Can please someone explain this to me?