import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus']=False
fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot(111)
x = np.arange(-10, 10)
y = np.where(x<0,0,x)
plt.xlim(-11,11)
plt.ylim(-11,11)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.set_xticks([-10,-5,0,5,10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
ax.set_yticks([-10,-5,5,10])
plt.plot(x,y,label="ReLU",color = "blue")
plt.legend()
plt.show()
当然Relu函数不用这么复杂,只需要手动就可以画出,在这里给出方法,只是希望能在画别的函数的时候得到启发。结果如下: