import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
from sympy.codegen.tests.test_applications import np
def lrelu(x, leak=0.2): # , name="lrelu"
# with tf.variable_scope(name):
f1 = 0.5 * (1 + leak)
f2 = 0.5 * (1 - leak)
return f1 * x + f2 * abs(x)
# 画图
#创建画布
fig = plt.figure(figsize=(8, 8))
#使用axisartist.Subplot方法创建一个绘图区对象ax
ax = axisartist.Subplot(fig, 111)
#将绘图区对象添加到画布中
fig.add_axes(ax)
ax.axis[:].set_visible(False)
#ax.new_floating_axis代表添加新的坐标轴
ax.axis["x"] = ax.new_floating_axis(0,0)
#给x坐标轴加上箭头
ax.axis["x"].set_axisline_style("->", size = 1.0)
#添加y坐标轴,且加上箭头
ax.axis["y"] = ax.new_floating_axis(1,0)
ax.axis["y"].set_axisline_style("-|>", size = 1.0)
#设置x、y轴上刻度显示方向
ax.axis["x"].set_axis_direction("top")
ax.axis["y"].set_axis_direction("right")
x = np.arange(-15,15,0.1)
#生成leakyrelu形式的y数据
y=lrelu(x)
#设置x、y坐标轴的范围
plt.xlim(-12,12)
plt.ylim(-12, 12)
#绘制图形
plt.plot(x,y, c='b')
plt.show()
画出来的图像如下所示: