2022年 11月 7日

Python绘制简单函数曲线(包括坐标范围限制、刻度指定)

python绘制简单函数曲线,包括坐标范围限制、刻度指定

  1. ################### 二维曲线图 ##################
  2. import numpy as np
  3. import math
  4. import matplotlib.pyplot as plt
  5. x = np.arange(-5, 5, 0.1)
  6. sigmoid, tanh, relu = [], [], []
  7. for t in x:
  8. y_1 = 1 / (1 + math.exp(-t))
  9. sigmoid.append(y_1)
  10. y_1 = (math.exp(t) - math.exp(-t)) / (math.exp(t) + math.exp(-t))
  11. tanh.append(y_1)
  12. y_1 = max(0, t)
  13. relu.append(y_1)
  14. plt.plot(x, sigmoid)
  15. #plt.ylim(0, 1)
  16. plt.show()
  17. plt.plot(x, tanh)
  18. #plt.ylim(-1, 1)
  19. plt.yticks([-1.0, -0.5, 0.0, 0.5, 1.0])
  20. plt.show()
  21. plt.plot(x, relu)
  22. #plt.ylim(0, 1)
  23. plt.show()
  24. ################### 三维曲线图 ##################
  25. import matplotlib.pyplot as plt
  26. from mpl_toolkits.mplot3d import axes3d
  27. ax = plt.gca(projection='3d')
  28. ax.set_xlim([0.7, 1.0])
  29. ax.set_ylim([-0.3, 0.2])
  30. ax.set_zlim([0.8, 1.0])
  31. ax.plot(y_pre[:, 0], y_pre[:, 1], y_pre[:, 2], 'r')
  32. ax.plot(ver_y[:, 0], ver_y[:, 1], ver_y[:, 2], 'b')
  33. plt.show()