#作用是当你调用matplotlib.pyplot的绘图函数plot()进行绘图的时候,#或者生成一个figure画布的时候,可以直接在你的python console里面生成图像。%matplotlib inline
from IPython import display
from mxnet import np, npx
from d2l import mxnet as d2l
npx.set_np()
#定义函数deff(x):return3* x **2-4* x
#求极限defnumerical_lim(f, x, h):return(f(x + h)- f(x))/ h
h =0.1for i inrange(5):#print字符串前面加f表示格式化字符串,加f后可以在字符串里面使用用花括号括起来的变量和表达式,#如果字符串里面没有表达式,那么前面加不加f输出应该都一样.print(f'h={h:.5f}, numerical limit = {numerical_lim(f,1, h):.5f}')
h *=0.1
#@savedefset_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):"""设置matplotlib的轴。"""
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
axes.set_xlim(xlim)
axes.set_ylim(ylim)if legend:
axes.legend(legend)
axes.grid()#@savedefplot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,
ylim=None, xscale='linear', yscale='linear',
fmts=('-','m--','g-.','r:'), figsize=(3.5,2.5), axes=None):"""绘制数据点。"""if legend isNone:
legend =[]
set_figsize(figsize)
axes = axes if axes else d2l.plt.gca()# 如果`X` 有⼀个轴,输出Truedefhas_one_axis(X):return(hasattr(X,"ndim")and X.ndim ==1orisinstance(X,list)andnothasattr(X[0],"__len__"))if has_one_axis(X):
X =[X]if Y isNone:
X, Y =[[]]*len(X), X
elif has_one_axis(Y):
Y =[Y]iflen(X)!=len(Y):
X = X *len(Y)
axes.cla()for x, y, fmt inzip(X, Y, fmts):iflen(x):
axes.plot(x, y, fmt)else:
axes.plot(y, fmt)
set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
x = np.arange(0,3,0.1)
plot(x,[f(x),2* x -3],'x','f(x)', legend=['f(x)','Tangent line (x=1)'])
<ipython-input-15-80d5adcc870a>:3: DeprecationWarning: `set_matplotlib_formats` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.set_matplotlib_formats()`
display.set_matplotlib_formats('svg')