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

阅读 39

2022-03-22

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

################### 二维曲线图 ##################
import numpy as np
import math
import matplotlib.pyplot as plt



x = np.arange(-5, 5, 0.1)
sigmoid, tanh, relu = [], [], []

for t in x:
    y_1 = 1 / (1 + math.exp(-t))
    sigmoid.append(y_1)

    y_1 = (math.exp(t) - math.exp(-t)) / (math.exp(t) + math.exp(-t))
    tanh.append(y_1)

    y_1 = max(0, t)
    relu.append(y_1)

plt.plot(x, sigmoid)
#plt.ylim(0, 1)
plt.show()

plt.plot(x, tanh)
#plt.ylim(-1, 1)
plt.yticks([-1.0, -0.5, 0.0, 0.5, 1.0])
plt.show()

plt.plot(x, relu)
#plt.ylim(0, 1)
plt.show()








################### 三维曲线图 ##################

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

ax = plt.gca(projection='3d')
ax.set_xlim([0.7, 1.0])
ax.set_ylim([-0.3, 0.2])
ax.set_zlim([0.8, 1.0])
ax.plot(y_pre[:, 0], y_pre[:, 1], y_pre[:, 2], 'r')
ax.plot(ver_y[:, 0], ver_y[:, 1], ver_y[:, 2], 'b')
plt.show()

 

精彩评论(0)

0 0 举报