Scatter Plot

Published: 28 March 2022
on channel: ARIHO K ATWIBU
13
0

Scatter Plot together with the line of best fit
Step 1: Get the equation of line of best fit. We will use Numpy's polyfit() method by passing in the following:
• x: x-coordinates of the data.
• y: y-coordinates of the data.
• deg: Degree of fitting polynomial. 1 = linear, 2 = quadratic, and so on.
df_Total['year'] = df_Total['year'].astype('int')
x = df_Total['year']
y = df_Total['Total']
fit = np.polyfit(x, y, deg=1)
fit array([ 5.56709228e+03, -1.09261952e+07])

The output is an array with the polynomial coefficients, highest powers first. Since we are plotting a linear regression y= a * x + b, our output has 2 elements [5.56709228e+03, -1.09261952e+07] with the the slope in position 0 and intercept in position 1.

Step 2: Plot the regression line on the scatter plot.
df_Total.plot(kind='scatter', x='year', y='Total', figsize=(10,6), color='darkblue')
plt.title('Total Immigration to Canada from 1980 - 2013')
plt.xlabel('Year')
plt.xticks(rotation=60)
plt.ylabel('Number of Immigrants')
plt.plot(x, fit[0] * x + fit[1], color='red') # recall that x is the Years
plt.annotate('y={0:.0f} x + {1:.0f}'.format(fit[0], fit[1]), xy=(2000, 150000))
plt.show()


On this page of the site you can watch the video online Scatter Plot with a duration of hours minute second in good quality, which was uploaded by the user ARIHO K ATWIBU 28 March 2022, share the link with friends and acquaintances, this video has already been watched 13 times on youtube and it was liked by 0 viewers. Enjoy your viewing!