主要使用可Python中的matplotlib这个画图库。见下面代码
import matplotlib.pyplot as plt #导入matplotlib库
import numpy as np #导入numpy库
#创建画布并引入axisartist工具。
import mpl_toolkits.axisartist as axisartist
#创建画布
fig = plt.figure(figsize=(8, 8))
#使用axisartist.Subplot方法创建一个绘图区对象ax
ax = axisartist.Subplot(fig, 111)
#将绘图区对象添加到画布中
fig.add_axes(ax)
ax.axis[:].set_visible(False)#通过set_visible方法设置绘图区所有坐标轴隐藏
ax.axis["x"] = ax.new_floating_axis(0,0)#ax.new_floating_axis代表添加新的坐标轴
ax.axis["x"].set_axisline_style("->", size = 1.0)#给x坐标轴加上箭头
#添加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-y坐标轴背景下,绘制函数图像
#生成x步长为0.1的列表数据
x = np.arange(-6,6,0.01)
#生成sigmiod形式的y数据
y = x*(x-1)*(x-2)
#y=1/x
#设置x、y坐标轴的范围
xl, xr = -4, 6
yu, xd = -6, 8
plt.xlim(xl, xr)
plt.ylim(yu, xd)
plt.xticks([-4, -2, 1,2, 4, 6])
plt.yticks([-6, -4, -2, 2, 4, 6])
name = r'$y=x(x-1)(x-2)$'
plt.text(3.5, 6, name)
plt.text(6, -0.5, 'x')
plt.text(-0.3, 8, 'y')
#绘制图形
plt.plot(x,y, c='b')
plt.savefig(r'C:\Users\Tang\Desktop\fun1.png', dpi=400)
plt.show()
运行结果为