python中内置数学函数详解和实例应用之三角函数曲线_初级阶段(三)

本文详细介绍了如何使用Python的matplotlib和numpy库绘制正弦、余弦、正切、余切、双曲正弦、双曲余弦、双曲正切、双曲余切以及它们的反函数曲线。通过示例代码展示了如何去除边框并区分不同函数的曲线,为初学者提供了直观的学习资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习目标:

利用matplotlib 和 numpy 画三角函数曲线

学习内容:

正弦,余弦,正切,余切函数曲线
双曲正弦,双曲余弦,双曲正切,双曲余切函数曲线
反正弦,反余弦,反正切,反余切函数曲线
反双曲正弦,反双曲余弦,反双曲正切,反双曲余切函数曲线


学习产出:

1.1, python画正弦函数曲线,保持原有的position不变的情况,代码如下:
import numpy as np
from matplotlib import pyplot as plt 

plt.figure(figsize = (6, 8), dpi = 200) # create a frame, dpi = 200
plt.subplot(111)    #create a subgraph, grid = 1 * 1

x = np.linspace(-np.pi, np.pi, 256, endpoint = True)  #numpy array:[-π, π], total 256 values
y = np.sin(x)

plt.plot(x, y, color = 'blue', linewidth = 2.0, linestyle= '-')      #define line color and style

plt.xlim(x.min() * 1.1, x.max() * 1.1)     #limit x range
plt.ylim(y.min() * 1.1, y.max() * 1.1)     #limit y range

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$'])   # 5 values in x_axis
plt.yticks([-1,-0.5,0,0.5,1],[r'$-1$',r'$-0.5$',r'$0$',r'$0.5$',r'$1$'])   # 5 values in y-axis

plt.show()

正弦曲线图片如下:
在这里插入图片描述
利用spine,在数据区域的边界,可以放置在任意位置, 在上述代码中加入,完整的代码和曲线如下:

import numpy as np
from matplotlib import pyplot as plt 

plt.figure(figsize = (6, 8), dpi = 200) # create a frame, dpi = 200
plt.subplot(111)    #create a subgraph, grid = 1 * 1

x = np.linspace(-np.pi, np.pi, 256, endpoint = True)  #numpy array:[-π, π], total 256 values
y = np.sin(x)

plt.plot(x, y, color = 'blue', linewidth = 2.0, linestyle= '-')      #define line color and style

plt.xlim(x.min() * 1.1, x.max() * 1.1)     #limit x range
plt.ylim(y.min() * 1.1, y.max() * 1.1)     #limit y range

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$'])   # 5 values in x_axis
plt.yticks([-1,-0.5,0,0.5,1],[r'$-1$',r'$-0.5$',r'$0$',r'$0.5$',r'$1$'])   # 5 values in y-axis

#move the boundary line,set origin is 0
ax = plt.gca()  #get current line position
ax.xaxis.set_ticks_position('bottom')       
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))  #set bottom position to 0
ax.spines['left'].set_position(('data', 0))
ax.spines['top'].set_color('none')      #cancel original boundary
ax.spines['right'].set_color('none')

plt.show()

去掉边框的正弦曲线图片如下:
在这里插入图片描述
1.2, python画余弦函数曲线, 我们在正弦曲线的代码中加入z = np.cos(x)的代码,同时需要区分正弦曲线和余弦曲线:

import numpy as np
from matplotlib import pyplot as plt 

plt.figure(figsize = (6, 8), dpi = 200) # create a frame, dpi = 200
plt.subplot(111)    #create a subgraph, grid = 1 * 1

x = np.linspace(-np.pi, np.pi, 256, endpoint = True)  #numpy array:[-π, π], total 256 values
y = np.sin(x)
z = np.cos(x)

plt.plot(x, y, color = 'blue', linewidth = 2.0, linestyle= '-')      #define line color and style
plt.plot(x, z, color = 'red', linewidth = 2.0, linestyle= '-')

plt.xlim(x.min() * 1.1, x.max
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值