draw_util.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. def mul_image():
  32. plt.figure(1) # 创建图表1
  33. plt.figure(2) # 创建图表2
  34. ax1 = plt.subplot(211) # 在图表2中创建子图1
  35. ax2 = plt.subplot(212) # 在图表2中创建子图2
  36. x = np.linspace(0, 3, 100)
  37. for i in range(5):
  38. plt.figure(1)
  39. plt.plot(x, np.exp(i * x / 3))
  40. plt.sca(ax1)
  41. plt.plot(x, np.sin(i * x))
  42. plt.sca(ax2)
  43. plt.plot(x, np.cos(i * x))
  44. plt.show()
  45. if __name__ == '__main__':
  46. # heights = [1.5, 1.7]
  47. # weights = [43, 61]
  48. # drawScatter(heights,weights)
  49. # drawLine(1,2)
  50. mul_image()