当前位置:网站首页 > Python编程 > 正文

用python绘制函数图像(python绘制函数图像代码)



    复习用,终究还是画了图标记出来比较清楚。参考张宇老师的30讲。其中公式类似MathJax,部分可参考但略有不同,详细内容见:

matplotlib 官网

https://matplotlib.org/stable/tutorials/text/mathtext.html

基本函数图像(p6)

    幂函数:y = x

1.定义域:定义域取决于指数a。当 | a | < 1,x < 0 在实数范围无定义。

import mathimport numpy as npimport matplotlib.pyplot as pltimport mpl_toolkits.axisartist as axisartist
def draw(X): fig = plt.figure(figsize=(10, 10)) ax = axisartist.Subplot(fig, 111) fig.add_axes(ax) ax.axis["x"] = ax.new_floating_axis(0, 0, axis_direction="bottom") ax.axis["y"] = ax.new_floating_axis(1, 0, axis_direction="bottom") ax.set_yticks([-5,-4,-3,-2,-1.5,-1,1,1.5,2,3,4,5]) ax.set_xticks([-5,-4,-3,-2,-1.5,-1,0,1,1.5,2,3,4,5]) ax.axis["x"].set_axisline_style("->", size=1.0) ax.axis["y"].set_axisline_style("->", size=1.0) ax.axis['top','right','left','bottom'].set_visible(False) plt.xlim(-max(X), max(X)) plt.ylim(-max(X), max(X)) ax.text(-0.5, max(x), 'y', fontsize=20) ax.annotate(text=' x', xy=(max(X), 0), fontsize=20) ax.grid(True, linestyle='-.') return ax if __name__=="__main__": x = np.linspace(-5, 5, 100) ax = draw(x) y = x ax.plot(x, y,label = 'y=x') y = x2 ax.annotate(r'y=x²',xy=(2, 4), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.annotate(r'y=x²',xy=(-2, 4), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.plot(x, y,label = 'y=x²') y = x3 ax.annotate(r'y=x³',xy=(math.pow(3, 1/3), 3), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.annotate(r'y=x³',xy=(-math.pow(3, 1/3), -3), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.plot(x, y,label = 'y=x³') y = 1/x ax.annotate(r'y=$\frac{1}{x}$',xy=(0.5, 2), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.annotate(r'y=$\frac{1}{x}$',xy=(-(1/1.2), -1.2), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) ax.plot(x, y,label = 'y=1/x (y=x⁻¹)') y = 1/x2 y = x-2 ax.annotate(r'y=$\frac{1}{x²}$',xy=(2, 0.25), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.annotate(r'y=$\frac{1}{x²}$',xy=(-2, 0.25), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.plot(x, y,label = 'y=1/x² (y=x⁻²)') x = np.linspace(0, 5, 100) y = list(map(lambda x:eval("math.sqrt(x)"), x)) label = 'y=x⁻²' ax.annotate(r'y=$\sqrt{x}$',xy=(4, 2), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.plot(x, y, label=label) y = list(map(lambda x:eval("math.pow(x, 1/3)"), x)) label = 'y=x⁻³' ax.annotate(r'y=$\sqrt[3]{x}$',xy=(3, math.pow(3, 1/3)), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.3")) ax.plot(x, y, label=label)
plt.legend() plt.show()

    指数函数:y = aˣ

3.单调性:底数 a > 1 单增,0 < a< 1 单减

4.极限:当 x -> -∞,eˣ -> 0;x -> +∞:eˣ -> +∞

 x = np.linspace(-5, 5, 100) ax = draw(x) y = list(map(lambda x:math.exp(x), x)) label = 'y=eˣ' ax.plot(x, y, label=label)  y = list(map(lambda x:1/math.exp(x), x)) label = 'y=e⁻ˣ' ax.plot(x, y, label=label)
y = 2x label = 'y=2ˣ' ax.plot(x, y, label=label) y = 0.5x label = r'y=($\frac{1}{2}$)ˣ' ax.plot(x, y, label=label) plt.legend() plt.show()

    对数函数:y = logx

3.单调性:底数 a > 1 单增,0 < a< 1 单减

4.特殊值:lnx = logx = 2.71828...,log1 = 0,loga = 1,lne=1

5.极限:当 x -> 0,lnx -> -∞ (∵e>1);x -> +∞:lnx -> +∞

6.代码:

 x = np.linspace(0.001, 5, 100) ax = draw(x)
y = np.log(x) label = 'y=lnx' ax.plot(x, y, label=label) y = list(map(lambda x:math.log(x,0.2), x)) label = r'y=log$_\frac{1}{5}$x' ax.plot(x, y, label=label) y = list(map(lambda x:math.log(x,5), x)) label = r'y=log$_5$x' ax.plot(x, y, label=label) plt.legend() plt.show()

    三角函数

    ①正弦、余弦

2.值域:[-1,+1]

3.奇偶性:y = sin(x) 是奇函数,y = cos(x) 是偶函数

    4.周期性:2π 为最小正周期

5.有界性:| sin(x) | ≤ 1,| cos(x) | ≤ 1

6.特殊值:sin0=0,sin(π/6)=0.5...

def show_value(ax, show,coordinate,xytext): ax.annotate(show,xy=coordinate, xycoords='data',xytext=xytext, textcoords='offset points', fontsize=12, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.1"))
if __name__=="__main__": x = np.linspace(-5, 5, 100) ax = draw(x) y = np.sin(x) label = 'y=sinx' plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) ax.plot(x, y, label=label) y = np.cos(x) label = 'y=cosx' plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) ax.plot(x, y, label=label) t = -4*np.pi/3 ax.plot([t,t],[0,np.cos(t)], color ='blue', linewidth=2.5, linestyle="--") ax.scatter([t,],[np.cos(t),], 50, color ='blue') ax.plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, linestyle="--") ax.scatter([t,],[np.sin(t),], 50, color ='red') show_value(ax,r'$\sin(\frac{-4\pi}{3})=\frac{\sqrt{3}}{2}$',(t, np.sin(t)),(-30, +50)) show_value(ax,r'$\cos(\frac{-4\pi}{3})=-\frac{1}{2}$',(t, np.cos(t)),(-30, -70)) show_value(ax,r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',(2*np.pi/3, np.sqrt(3)/2),(+50, +20)) show_value(ax,r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',(2*np.pi/3, -1/2),(-30, -70)) show_value(ax,r'sin0=0',(0,0),(-70, -70)) show_value(ax,r'$\sin(\frac{\pi}{6})=\frac{1}{2}$',(np.pi/6,0.5),(-120, -20)) show_value(ax,r'$\sin(\frac{\pi}{4})=\frac{\sqrt{2}}{2}$',(np.pi/4,np.sqrt(2)/2),(-40, -75)) show_value(ax,r'$\sin(\frac{\pi}{3})=\frac{\sqrt{3}}{2}$',(np.pi/3, np.sqrt(3)/2),(-10, +50)) show_value(ax,r'$\sin(\frac{\pi}{2})=1$',(np.pi/2, 1),(+60, +50)) show_value(ax,r'$\sin\pi(\sin2\pi)=0$',(np.pi, 0),(+20, +30)) show_value(ax,r'$\sin(\frac{3\pi}{2})=-1$',(3*np.pi/2, -1),(+20, -40)) show_value(ax,r'cos0=1',(0,1),(-60, +40)) show_value(ax,r'$\cos(\frac{\pi}{6})=\frac{\sqrt{3}}{2}$',(np.pi/6, np.sqrt(3)/2),(-130, -5)) show_value(ax,r'$\cos(\frac{\pi}{4})=\frac{\sqrt{2}}{2}$',(np.pi/4,np.sqrt(2)/2),(-30, +100)) show_value(ax,r'$\cos(\frac{\pi}{3})=\frac{1}{2}$',(np.pi/3,0.5),(-5, -90)) show_value(ax,r'$\cos(\frac{\pi}{2})=1$',(np.pi/2, 1),(-10, -40)) show_value(ax,r'$\cos(\frac{3\pi}{2})=0$',(3*np.pi/2, 0),(+10, -35)) show_value(ax,r'$\cos\pi(\cos2\pi)=-1$',(np.pi, -1),(-20, -70)) ax.plot([np.pi/6,np.pi/6],[0.5, np.sqrt(3)/2], color ='orange', linewidth=0.5, linestyle="--") ax.plot([np.pi/3,np.pi/3],[0.5, np.sqrt(3)/2], color ='orange', linewidth=0.5, linestyle="--") ax.plot([2*np.pi/3,2*np.pi/3],[0, np.sqrt(3)/2], color ='orange', linewidth=0.5, linestyle="--") ax.plot([2*np.pi/3,2*np.pi/3],[0, -1/2], color ='cyan', linewidth=0.5, linestyle="--") ax.plot([3*np.pi/2,3*np.pi/2],[0, -1], color ='cyan', linewidth=0.5, linestyle="--")
plt.legend()    plt.show()

    ② 正切,余切

    1.定义域:y = tan(x) 的定义域 x ≠ kπ + π/2(k∈Ζ)的实数x;

                    y = cot(x) 的定义域 x ≠ kπ (k∈Ζ)的实数x。

    3.周期性:均以 π 为最小正周期

4.代码(由于除法原因有些位置不好调,不想浪费时间,所以代码乱点):

# 参考了 stackoverflow 上的一个问题def draw_point_by_point(plt,x_largest,y_largest,expr): n = 1000 X = [] Y = [] Z = [] for i in range(0,2*n): x = -x_largest + i*x_largest/n y = eval(expr) if -y_largest < y and y < y_largest: X.append(x) Y.append(y) else: if len(X) > 0 and len(Y) > 0: Z.append([X,Y]) plt.plot(X[0],Y[0]) del X,Y X = [] Y = [] for i in range(0, len(Z)): plt.plot(Z[i][0],Z[i][1])

x = np.linspace(-5, 5, 100) ax = draw(x) y_largest = 5 x_largest = 5 n = 1000 X = [] Y = [] Z = [] for i in range(0,2*n): x = 3.14 + i*x_largest/n tanx = np.tan(x) if tanx == 0: continue y = 1/tanx if -y_largest < y and y < y_largest and x < x_largest: X.append(x) Y.append(y) else: if len(X) > 0 and len(Y) > 0: Z.append([X,Y]) plt.plot(X[0],Y[0]) del X,Y X = [] Y = [] for i in range(0, len(Z)): plt.plot(Z[i][0],Z[i][1]) pass
# y = np.tan(x) label = 'y=tanx' plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) draw_point_by_point(plt,3*np.pi/2,5,"np.tan(x)") show_value(ax,r'y=tan(x)',(math.atan(3),3),(+20, +20)) show_value(ax,r'y=tan(x)',(math.atan(3)-np.pi,3),(+20, +20)) show_value(ax,r'y=tan(x)',(math.atan(3)+np.pi,3),(+20, +20)) draw_point_by_point(plt,3*np.pi/2,5,"1/np.tan(x)") show_value(ax,r'y=cot(x)',(math.atan(-1/3),-3),(+20, +20))# x = 1/math.atan(-1/3)# print(x)# x = 1/math.atan(-1/3) + np.pi# show_value(ax,r'y=cot(x)',(x,-3),(+20, +20))# x = 1/math.atan(-1/3) + 2*np.pi# print(x)# show_value(ax,r'y=cot(x)',(x,-3),(+20, +20)) show_value(ax,r'y=cot(x)',(2.9,-1/math.atan(3)-np.pi),(+20, +20)) show_value(ax,r'y=cot(x)',(2.9-2*np.pi,-1/math.atan(3)-np.pi),(+50, +20)) show_value(ax,r'tan(0)=0',(0,0),(-70, -70)) show_value(ax,r'$\tan(\frac{\pi}{6})=\frac{\sqrt{3}}{3}$',(np.pi/6,np.sqrt(3)/3),(-120, -10)) show_value(ax,r'$\tan(\frac{\pi}{4})=1$',(np.pi/4, 1),(+60, +10)) show_value(ax,r'$\tan(\frac{\pi}{3})=\sqrt{3}$',(np.pi/3, np.sqrt(3)),(+40, +20)) show_value(ax,r'$\tan(\pi)=0 [\tan(2\pi)=0]$',(np.pi, 0),(+20, -30))# show_value(ax,r'$\tan2\pi=0$',(2*np.pi, 0),(+20, +30)) show_value(ax,r'$\cot(\frac{\pi}{6})=\sqrt{3}$',(np.pi/6, np.sqrt(3)),(0, +40)) show_value(ax,r'$\cot(\frac{\pi}{4})=1$',(np.pi/4, 1),(+60, -30)) show_value(ax,r'$\cot(\frac{\pi}{3})=\frac{\sqrt{3}}{3}$',(np.pi/3,np.sqrt(3)/3),(-150, +30)) show_value(ax,r'$\cot(\frac{\pi}{2})=0$',(np.pi/2, 0),(-60, -60)) show_value(ax,r'$\cot(\frac{3\pi}{2})=0$',(3*np.pi/2, 0),(-10, +40)) show_value(ax,r'$\lim_{x\to \frac{\pi}{2}}\tan(x)=∞$',(np.pi/2, 5),(+20, +30)) show_value(ax,r'$\lim_{x\to \frac{3\pi}{2}}\tan(x)=∞$',(3*np.pi/2, 5),(+50, +30)) show_value(ax,r'$\lim_{x\to 0}\cot(x)=∞$',(0, 5),(+20, +30)) show_value(ax,r'$\lim_{x\to \pi(2\pi)}\cot(x)=∞$',(np.pi, 5),(+20, +30)) ax.scatter([np.pi/4,],[1,], 50, color ='blue') plt.show()
# 也可以用插入 nan 值不让它自动连线的方法,但是图很丑# pos = np.where(np.abs(np.diff(y)) >= np.pi/2)[0]+1# x[pos] = np.nan# y[pos] = np.nan# x = np.insert(x, pos, np.nan)# y = np.insert(y, pos, np.nan)

    ③ 正割、余割

1.定义域:y = sec(x) 的定义域 x ≠ kπ + π/2(k∈Ζ)的实数x;

                    y = csc(x) 的定义域 x ≠ kπ (k∈Ζ)的实数x。

                值域:(-∞,-1] ∪ [1,+∞)。

    2.奇偶性:y = sec(x) 是偶函数;y = csc(x) 奇函数。

    3.周期性:都以 2π 为最小正周期。

 x = np.linspace(-7, 7, 100) ax = draw(x,5) # plt.ylim(-y, y)
plt.xticks([-2*np.pi, -3*np.pi/2, -np.pi, -np.pi/2, 0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi],[r'$-2\pi$', r'$-3\pi/2$', r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$', r'$+3\pi/2$', r'$+2\pi$']) draw_point_by_point(plt,3*np.pi,5,"1/np.cos(x)") ax.plot([-7,7],[1, 1], color ='red', linewidth=1, linestyle="--") ax.plot([-7,7],[-1, -1], color ='red', linewidth=1, linestyle="--") ax.plot([-3*np.pi/2,-3*np.pi/2],[-5, 5], color ='orange', linewidth=1, linestyle="--") ax.plot([-np.pi/2,-np.pi/2],[-5, 5], color ='orange', linewidth=1, linestyle="--") ax.plot([3*np.pi/2,3*np.pi/2],[-5, 5], color ='orange', linewidth=1, linestyle="--") ax.plot([np.pi/2,np.pi/2],[-5, 5], color ='orange', linewidth=1, linestyle="--") ax.annotate(r'$\sec(x)=\frac{1}{\cos(x)}$',xy=(-1.5,5), xycoords='data',xytext=(0,+50), textcoords='offset points', fontsize=25) plt.show() x = np.linspace(-7, 7, 100) ax = draw(x,5) plt.xticks([-2*np.pi, -3*np.pi/2, -np.pi, -np.pi/2, 0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi],[r'$-2\pi$', r'$-3\pi/2$', r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$', r'$+3\pi/2$', r'$+2\pi$']) draw_point_by_point(plt,3*np.pi,7,"1/np.sin(x)")
ax.plot([-7,7],[1, 1], color ='red', linewidth=1, linestyle="--") ax.plot([-7,7],[-1, -1], color ='red', linewidth=1, linestyle="--") ax.plot([-2*np.pi,-2*np.pi],[-5, 5], color ='orange', linewidth=1, linestyle="--") ax.plot([-np.pi,-np.pi],[-5, 5], color ='orange', linewidth=1, linestyle="--") ax.plot([2*np.pi,2*np.pi],[-5, 5], color ='orange', linewidth=1, linestyle="--") ax.plot([np.pi,np.pi],[-5, 5], color ='orange', linewidth=1, linestyle="--")
ax.annotate(r'$\csc(x)=\frac{1}{\sin(x)}$',xy=(-1.5,5), xycoords='data',xytext=(0,+50), textcoords='offset points', fontsize=25) plt.show()

    反三角函数:

    ① 反正弦函数、反余弦函数:

    1.定义域:[-1,1]

    2.单调性:arcsin 单增,arccos 单减

    3.奇偶性:arcsin 奇函数

    4.有界性:定义域内有界   

                    -π/2 ≤ arcsin(x) ≤ π/2

                     0 ≤ arccos(x) ≤ π

def draw(X,x,y,ax):  ax.axis["x"] = ax.new_floating_axis(0, 0, axis_direction="bottom") ax.axis["y"] = ax.new_floating_axis(1, 0, axis_direction="bottom") ax.set_yticks([-5,-4,-3,-2,-1.5,-1,1,1.5,2,3,4,5]) ax.set_xticks([-5,-4,-3,-2,-1.5,-1,0,1,1.5,2,3,4,5]) ax.axis["x"].set_axisline_style("->", size=1.0) ax.axis["y"].set_axisline_style("->", size=1.0) ax.axis['top','right','left','bottom'].set_visible(False) plt.xlim(-x, x) plt.ylim(-y, y)
ax.text(-0.5, x, 'y', fontsize=20) ax.annotate(text=' x', xy=(max(X), 0), fontsize=20) ax.grid(True, linestyle='-.')    return ax
def show_value(ax, show,coordinate,xytext): ax.annotate(show,xy=coordinate, xycoords='data',xytext=xytext, textcoords='offset points', fontsize=12, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.1"))
if __name__=="__main__":
x = np.linspace(-1, 1, 10000) fig = plt.figure(12,figsize=(15, 10)) ax = axisartist.Subplot(fig, 121) fig.add_axes(ax) ax = draw(x,1.5,2,ax)
plt.yticks([ -np.pi/2, 0, np.pi/2],[r'$-\pi/2$', r'$0$', r'$+\pi/2$'])
y = np.arcsin(x)#list(map(lambda x:math.asin(x), x)) ax.plot(x, y,label = 'y=arcsin(x)') ax.annotate(r'$y=arcsin(x)$',xy=(-1.4,.5), xycoords='data',xytext=(0,0), textcoords='offset points', fontsize=25) show_value(ax,r'arcsin(0)=0',(0,0),(+70, -70)) show_value(ax,r'$\arcsin(\frac{1}{2})=\frac{\pi}{6}$',(0.5,np.pi/6),(+50, -20)) show_value(ax,r'$\arcsin(\frac{\sqrt{2}}{2})=\frac{\pi}{4}$',(np.sqrt(2)/2,np.pi/4),(-75, +50)) show_value(ax,r'$\arcsin(\frac{\sqrt{3}}{2})=\frac{\pi}{3}$',(np.sqrt(3)/2,np.pi/3),(+60, +30)) show_value(ax,r'$\arcsin(1)=\frac{\pi}{2}$',(1,np.pi/2),(-10, +50))

# ------------------------------------------------------------------------------------------ x = np.linspace(-1, 1, 10000) ax1 = axisartist.Subplot(fig, 122) fig.add_axes(ax1) ax1 = draw(x,1.5,2,ax1)
plt.yticks([ -.5, 0, np.pi/2, np.pi],[r'$0.5$', r'$0$', r'$+\pi/2$', r'$+\pi$']) ax1.annotate(r'$y=arccos(x)$',xy=(-1.4,.5), xycoords='data',xytext=(0,+50), textcoords='offset points', fontsize=25)
y = np.arccos(x)#list(map(lambda x:math.asin(x), x)) ax1.plot(x, y,label = 'y=arccos(x)') show_value(ax1,r'arccos(1)=0',(1,0),(-10, -70)) show_value(ax1,r'$\arccos(\frac{\sqrt{3}}{2})=\frac{\pi}{6}$',(np.sqrt(3)/2,np.pi/6),(+60, +5)) show_value(ax1,r'$\arccos(\frac{\sqrt{2}}{2})=\frac{\pi}{4}$',(np.sqrt(2)/2,np.pi/4),(+20, +70)) show_value(ax1,r'$\arccos(\frac{1}{2})=\frac{\pi}{3}$',(1/2,np.pi/3),(-10, +90)) show_value(ax1,r'$\arccos(0)=\frac{\pi}{2}$',(0,np.pi/2),(-90, +100))
plt.show()

    ②反正切、反余切

    4.有界性:定义域内都有界

                    arctan(x) ∈ (-π/2,π/2)

                    arccot(x) ∈ (0,π)

    5.性质:arctan(x) + arccot(x) = π/2 (-∞ ≤ x ≤ +∞)

def draw(X,x,y): fig = plt.figure(figsize=(2*x, 2*y)) ax = axisartist.Subplot(fig, 111) fig.add_axes(ax) ax.axis["x"] = ax.new_floating_axis(0, 0, axis_direction="bottom")    ax.axis["y"] = ax.new_floating_axis(1, 0, axis_direction="bottom") ax.set_xticks([-5,-4,-3,-2,-1.5,-1,0,1,1.5,2,3,4,5]) ax.axis["x"].set_axisline_style("->", size=1.0) ax.axis["y"].set_axisline_style("->", size=1.0) ax.axis['top','right','left','bottom'].set_visible(False) plt.xlim(-x, x) plt.ylim(-y, y)
ax.text(-0.5, y, 'y', fontsize=20) ax.annotate(text=' x', xy=(max(X), 0), fontsize=20) ax.grid(True, linestyle='-.') return ax

def show_value(ax, show,coordinate,xytext): ax.annotate(show,xy=coordinate, xycoords='data',xytext=xytext, textcoords='offset points', fontsize=12, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.1"))
def acot(x):    r = [] for one in x: if one > 0: r.append(np.arctan(1/one)) elif one < 0: r.append(np.pi + np.arctan(1/one)) else: r.append(np.pi/2)            pass pass return r if __name__=="__main__":
x = np.linspace(-5, 5, 10000) ax = draw(x,5,np.pi/2) plt.yticks([ -np.pi/2, -1, 1, np.pi/2],[r'$-\pi/2$', '-1', '1', r'$+\pi/2$'])
y = np.arctan(x) ax.plot(x, y, label = 'y=arctan(x)') ax.annotate(r'$y = arctan(x)$',xy=(-1.5,np.pi/2), xycoords='data',xytext=(0,+30), textcoords='offset points', fontsize=25)
show_value(ax,r'arctan(0)=0',(0,0),(+50, -50)) show_value(ax,r'$\arctan(\frac{\sqrt{3}}{3})=\frac{\pi}{6}$',(np.sqrt(3)/3,np.pi/6),(+60, -15)) show_value(ax,r'$\arctan(1)=\frac{\pi}{4}$',(1,np.pi/4),(-10, +50)) show_value(ax,r'$\arctan(\sqrt{3})=\frac{\pi}{3}$',(np.sqrt(3),np.pi/3),(+60, +30)) show_value(ax,r'$\lim_{x\to -∞}\arctan(x)=-\frac{\pi}{2}$',(-5, -np.pi/2),(-90, -40)) show_value(ax,r'$\lim_{x\to +∞}\arctan(x)=\frac{\pi}{2}$',(5, np.pi/2),(+20, +30)) ax.plot([0,np.sqrt(3)/3],[np.pi/6, np.pi/6], color ='orange', linewidth=1, linestyle="--") ax.plot([np.sqrt(3)/3,np.sqrt(3)/3],[0, np.pi/6], color ='violet', linewidth=1, linestyle="--")
plt.show() # ------------------------------------------------------------------------------------------ x = np.linspace(-5, 5, 10000) ax = draw(x,5,np.pi)
plt.yticks([1, np.pi/2, np.pi],['1', r'$+\pi/2$', r'$+\pi$']) y = acot(x) ax.plot(x, y, label = 'y=arccot(x)') ax.annotate(r'$y = arccot(x)$',xy=(-1.5,np.pi), xycoords='data',xytext=(0,+30), textcoords='offset points', fontsize=25) show_value(ax,r'$arccot(0)=\frac{\pi}{2}$',(0,np.pi/2),(-120, +20)) show_value(ax,r'$arccot(\sqrt{3})=\frac{\pi}{6}$',(np.sqrt(3),np.pi/6),(+60, +5)) show_value(ax,r'$arccot(1)=\frac{\pi}{4}$',(1,np.pi/4),(+50, +26)) show_value(ax,r'$arccot(\frac{\sqrt{3}}{3})=\frac{\pi}{3}$',(np.sqrt(3)/3,np.pi/3),(+30, +50)) show_value(ax,r'$\lim_{x\to -∞}arccot(x)=\pi$',(-5, np.pi),(-90, +30)) show_value(ax,r'$\lim_{x\to +∞}arccot(x)=0$',(5, 0),(+30, -40))
ax.plot([0,np.sqrt(3)],[np.pi/6, np.pi/6], color ='orange', linewidth=1, linestyle="--") ax.plot([np.sqrt(3),np.sqrt(3)],[0, np.pi/6], color ='violet', linewidth=1, linestyle="--") plt.show()

    初等函数

    基本初等函数经过有限次四则运算,以及有限次复合步骤构成,并可由一个式子表示

    1.初等函数可以是一个区间,也可以是几个区间的并集,甚至可以是一些孤立的点。如 y = (cosπ - 1)⁰·⁵,定义域 x = 0, ±2, ±4 ...。这个图就不画了,matplotlib 默认会自己给点连起来。

    2.幂指函数 u(x)ᵛ⁽ˣ⁾ = eᵛ⁽ˣ⁾ ⁿ ᵘ⁽ˣ⁾ 也是初等函数。还得找俩函数,而且本身就是各种组合,也不画了。

三个重要的分段函数

绝对值函数符号函数

取整函数

    1.定义域 R,值域 Z。在 x 为整数值处图形发生跳跃。

def roundX(x, plt, ax): plt.scatter(x,x, color='blue', marker='o', edgecolors='g', linewidth=2, s=100) plt.scatter(x+1,x, color='', marker='o', edgecolors='r', linewidth=2, s=100) ax.plot([x,x+0.9],[x,x], color ='blue', linewidth=2, linestyle="-")
if __name__=="__main__": x = np.linspace(-5, 5, 10000) fig = plt.figure(12,figsize=(20, 10)) ax = axisartist.Subplot(fig, 121) fig.add_axes(ax) ax = draw(x,5,5,ax) y = np.fabs(x) ax.plot(x, y, label = 'y =|x|') ax.annotate(r'$y = |x| = \{ \genfrac{}{}{0}{}{ x , x ≥ 0}{ -x , x < 0}$',xy=(-4.5,-2), xycoords='data',xytext=(-20,+30), textcoords='offset points', fontsize=30)
ax1 = axisartist.Subplot(fig, 122) fig.add_axes(ax1) ax1 = draw(x,5,5,ax1) ax1.annotate(r'$y = sign(x) = \{ \genfrac{}{}{0}{}{ 0 , x = 0 }{ \genfrac{}{}{0}{}{ 1 , x > 0}{ -1 , x < 0}} : x = |x|sign(x)$',xy=(-5,2), xycoords='data',xytext=(-40,+30), textcoords='offset points', fontsize=30) plt.scatter(0,0, color='blue', marker='o', edgecolors='g', linewidth=2, s=100) plt.scatter(0,1, color='', marker='o', edgecolors='r', linewidth=2, s=100) ax1.plot([0.1,6],[1,1], color ='blue', linewidth=2, linestyle="-") plt.scatter(0,-1, color='', marker='o', edgecolors='r', linewidth=2, s=100) ax1.plot([-0.1,-6],[-1,-1], color ='blue', linewidth=2, linestyle="-")# ax.scatter([0,0],[0,0], 50, color ='blue') plt.show()
x = np.linspace(-5, 5, 10000) fig = plt.figure(11,figsize=(10, 10)) ax = axisartist.Subplot(fig, 111) fig.add_axes(ax) ax = draw(x,5,5,ax) ax.annotate(r'$y = [x] : (x-1 < [x] <≤ x)$',xy=(-4.5,2), xycoords='data',xytext=(-100,+30), textcoords='offset points', fontsize=25) roundX(0, plt, ax) roundX(1, plt, ax) roundX(-1, plt, ax) roundX(2, plt, ax) roundX(-2, plt, ax) roundX(3, plt, ax) roundX(-3, plt, ax) roundX(4, plt, ax) roundX(-4, plt, ax) show_value(ax,r'[0.9]=0',(0.9,0),(+50, +25)) show_value(ax,r'$[\pi]=3$',(np.pi,3),(+50, -50)) show_value(ax,r'[-1]=-1',(-1,-1),(-110, +10)) show_value(ax,r'[-1.99]=-2',(-1.9,-2),(-60, +30)) show_value(ax,r'$\lim_{x\to 0^{+}}[x]=0$',(0, 0),(+50, -90)) show_value(ax,r'$\lim_{x\to 0^{-}}[x]=-1$',(0, -1),(-120, +90))
plt.show()

    常见图像就这样了,篇幅有点长了。如果后续画图像变换和极坐标图像的话就再说了。

到此这篇用python绘制函数图像(python绘制函数图像代码)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 读取pcap文件(python读取pcap文件)2025-05-23 20:00:09
  • python函数如果没有return语句(python如果函数中没有return语句)2025-05-23 20:00:09
  • python def函数怎么调用函数(python中def函数的调用)2025-05-23 20:00:09
  • python字典扩充(python 字典 增加)2025-05-23 20:00:09
  • python函数的定义和作用(python中函数定义包括)2025-05-23 20:00:09
  • vscode配置gdb(VSCode配置Python环境)2025-05-23 20:00:09
  • py文档怎么保存(python文档怎么保存)2025-05-23 20:00:09
  • list转为字符串 python(list转为string字符串 python)2025-05-23 20:00:09
  • pycharm怎么删除虚拟环境(python删除venv虚拟环境)2025-05-23 20:00:09
  • py文件怎么打包成可执行文件(python怎么把py文件打包成exe)2025-05-23 20:00:09
  • 全屏图片