draw_util.py 877 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. #绘制散点图
  6. def drawScatter(heights,weights):
  7. #创建散点图
  8. #第一个参数为点的横坐标
  9. #第二个参数为点的纵坐标
  10. plt.scatter(heights,weights)
  11. plt.xlabel('Heights')
  12. plt.ylabel('Weight')
  13. plt.title('Heights & Weight of Students')
  14. plt.show()
  15. def drawLine(w, b):
  16. plt.xlabel("x")
  17. plt.ylabel("y")
  18. x = np.arange(0, 10)
  19. y = w*x + b
  20. plt.plot(x, y)
  21. plt.show()
  22. def drawScatterAndLine(p, q, w, b):
  23. plt.scatter(p, q)
  24. plt.xlabel('p')
  25. plt.ylabel('q')
  26. plt.title('line regesion')
  27. x = np.arange(0, 11)
  28. y = w * x + b
  29. plt.plot(x, y, color='red')
  30. plt.show()
  31. if __name__ == '__main__':
  32. heights = [1.5, 1.7]
  33. weights = [43, 61]
  34. drawScatter(heights,weights)
  35. drawLine(1,2)