Scatter plot and line

Code implementation:

  1. # 确定x轴和y轴的坐标
  2. x = np.linspace(dataSet.Population.min(), dataSet.Population.max())
  3. y = theta1[0, 0] + (theta1[0, 1] * x)
  4. fig, ax = plt.subplots(figsize=(12,8))
  5. # 绘制直线
  6. ax.plot(x, y, 'r', label='Prediction')
  7. # 绘制散点图
  8. ax.scatter(dataSet.Population, dataSet.Profit, label='Traning Data')
  9. # 设置图例(左上角)
  10. ax.legend(loc=2)
  11. # 设置x轴和y轴的标签以及图的标题
  12. ax.set_xlabel('Population')
  13. ax.set_ylabel('Profit')
  14. ax.set_title('Predicted Profit vs. Population Size')
  15. plt.show()

Running effect:
屏幕快照 2020-07-18 下午4.22.00.png
Correlation function

  • np.linspace

resources: https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

  • plt.subplots

resources: https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.subplots.html

  • ax.plot

resources: https://blog.csdn.net/leaf_zizi/article/details/87094168

  • ax.scatter

resources: https://blog.csdn.net/qiu931110/article/details/68130199

  • ax.legend