python中内置数学函数详解和实例应用之三角函数曲线_初级阶段(三)
学习目标:
利用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 * 1x = 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 styleplt.xlim(x.min() * 1.1, x.max() * 1.1) #limit x range
plt.ylim(y.min() * 1.1, y.max() * 1.1) #limit y rangeplt.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-axisplt.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 * 1x = 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 styleplt.xlim(x.min() * 1.1, x.max() * 1.1) #limit x range
plt.ylim(y.min() * 1.1, y.max() * 1.1) #limit y rangeplt.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 * 1x = 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() * 1.1) #limit x range
plt.ylim(y.min() * 1.1, y.max() * 1.1) #limit y rangeplt.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.plot(x, y, label = 'sin(x)') #add label and show on the graph
plt.plot(x, z, label = 'cos(x)')
plt.legend(loc = 'upper left')plt.show()
去掉边框的正弦,余弦曲线图片如下:
1.3, python画正切函数曲线, 我们在上述的正余弦曲线的代码中加入a = np.tan(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 * 1x = np.linspace(-np.pi, np.pi, 256, endpoint = True) #numpy array:[-π, π], total 256 values
y = np.sin(x)
z = np.cos(x)
a = np.tan(x)plt.plot(x, y, color = 'blue', label = 'sin(x)',linewidth = 2.0, linestyle= '-') #define line color and style
plt.plot(x, z, color = 'red', label = 'cos(x)', linewidth = 2.0, linestyle= '--')
plt.plot(x, a, color = 'yellow', label = 'tan(x)', linewidth = 2.0, linestyle= '-.')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 rangeplt.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.legend(loc = (0, 0.92))plt.show()
去掉边框的正弦,余弦, 正切曲线图片如下:
1.4, 我们直接画出sin(x), cos(x), tan(x), cot(x)的函数曲线图吧
1.5, python画双曲正弦函数曲线, 由于y的值域范围和正弦余弦函数不同,我们重新创建一个双曲正弦曲线画布:
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 * 1x = np.linspace(-np.pi, np.pi, 256, endpoint = True) #numpy array:[-π, π], total 256 values
y = np.sinh(x)plt.plot(x, y, color = 'blue', label = 'sinh(x)',linewidth = 2.0, linestyle= '-') #define line color and styleplt.xlim(x.min() * 1.1, x.max() * 1.1) #limit x range
plt.ylim(y.min() * 1.1, y.max() * 1.1) #limit y rangeplt.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([-10,-5,5,10],[r'$-10$',r'$-5$',r'$5$',r'$10$']) # 4 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.legend(loc = 'upper left')plt.show()
去掉边框的双曲正弦曲线图片如下:
1.6, python画双曲余弦函数曲线,在双曲正弦函数曲线的画布基础上添加y = np.cosh(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 * 1x = np.linspace(-np.pi, np.pi, 256, endpoint = True) #numpy array:[-π, π], total 256 values
y = np.sinh(x)
z = np.cosh(x)plt.plot(x, y, color = 'blue', label = 'sinh(x)',linewidth = 2.0, linestyle= '-') #define line color and style
plt.plot(x, z, color = 'red', label = 'cosh(x)', linewidth = 2.0, linestyle= '--')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 rangeplt.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([-10,-5,5,10],[r'$-10$',r'$-5$',r'$5$',r'$10$']) # 4 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.legend(loc = 'upper left')plt.show()
去掉边框的双曲正弦,双曲余弦曲线图片如下:
1.7, python画双曲正切函数曲线,在双曲正弦函数和双曲余弦函数曲线的画布基础上添加a = np.tanh(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 * 1x = np.linspace(-np.pi, np.pi, 256, endpoint = True) #numpy array:[-π, π], total 256 values
y = np.sinh(x)
z = np.cosh(x)
a = np.tanh(x)plt.plot(x, y, color = 'blue', label = 'sinh(x)',linewidth = 2.0, linestyle= '-') #define line color and style
plt.plot(x, z, color = 'red', label = 'cosh(x)', linewidth = 2.0, linestyle= '--')
plt.plot(x, a, color = 'purple', label = 'tanh(x)', linewidth = 2.0, linestyle= '-.')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 rangeplt.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([-10,-5,5,10],[r'$-10$',r'$-5$',r'$5$',r'$10$']) # 4 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.legend(loc = 'upper left')plt.show()
去掉边框的双曲正弦,双曲余弦, 双曲正切曲线图片如下:
1.8, 我们再直接画出sinh(x), cosh(x), tanh(x)和coth(x)的函数曲线图吧。
1.9 python画反正弦函数曲线, 我们再重新创建一个反正弦曲线画布,注意x轴和y轴是等比例的:
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 * 1x = np.linspace(-1, 1, 1000, endpoint = True) #x range changed
y = np.arcsin(x)plt.plot(x, y, color = 'blue', label = 'arcsin(x)',linewidth = 1.0, linestyle= '-') #define line color and styleplt.xlim(-1, 1) #limit x range
plt.ylim(-np.pi/2, np.pi/2) #limit y rangeplt.xticks([-1,-0.5,0,0.5,1],[r'$-1$',r'$-0.5$',r'$0$',r'$0.5$',r'$1$']) # 5 values in x-axis
plt.yticks([-np.pi/2, -0.75, 0.75, np.pi/2],[r'$-\pi/2$',r'$-0.75$',r'$0.75$',r'$\pi/2$']) # 4 values in y_axis#move the boundary line,set origin is 0
plt.axis('equal') #x-axis and y-axis scale equally
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.legend(loc = 'upper left')plt.show()
去掉边框的反正弦曲线函数:
1.10, python画反余弦函数曲线, 我们在反正弦曲线画布基础上增加,注意x轴和y轴是等比例的:
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 * 1x = np.linspace(-1, 1, 1000, endpoint = True) # x range changed
y = np.arcsin(x)
z = np.arccos(x)plt.plot(x, y, color = 'blue', label = 'arcsin(x)',linewidth = 1.0, linestyle= '-') #define line color and style
plt.plot(x, z, color = 'red', label = 'arccos(x)',linewidth = 1.0, linestyle= '--') plt.xlim(-1, 1) #limit x range
plt.ylim(-np.pi/2, np.pi/2) #limit y rangeplt.xticks([-1,-0.5,0,0.5,1],[r'$-1$',r'$-0.5$',r'$0$',r'$0.5$',r'$1$']) # 5 values in x-axis
plt.yticks([-np.pi/2, -0.75, 0.75, np.pi/2],[r'$-\pi/2$',r'$-0.75$',r'$0.75$',r'$\pi/2$']) # 4 values in y_axis#move the boundary line,set origin is 0
plt.axis('equal') #x-axis and y-axis scale equally
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.legend(loc = 'upper left')plt.show()
去掉边框的反正弦, 反余弦函数曲线函数:
1.11, python画反正切函数曲线, 我们在反正弦曲线和反余弦曲线画布基础上增加,注意x轴和y轴是等比例的:
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 * 1x = np.linspace(-1, 1, 1000, endpoint = True) # x range changed
y = np.arcsin(x)
z = np.arccos(x)
a = np.arctan(x)plt.plot(x, y, color = 'blue', label = 'arcsin(x)',linewidth = 1.0, linestyle= '-') #define line color and style
plt.plot(x, z, color = 'red', label = 'arccos(x)',linewidth = 1.0, linestyle= '--')
plt.plot(x, a, color = 'purple', label = 'arctan(x)', linewidth = 1.0, linestyle= '-.')plt.xlim(-1, 1) #limit x range
plt.ylim(-np.pi/2, np.pi/2) #limit y rangeplt.xticks([-1,-0.5,0,0.5,1],[r'$-1$',r'$-0.5$',r'$0$',r'$0.5$',r'$1$']) # 5 values in x-axis
plt.yticks([-np.pi/2, -0.75, 0.75, np.pi/2],[r'$-\pi/2$',r'$-0.75$',r'$0.75$',r'$\pi/2$']) # 4 values in y_axis#move the boundary line,set origin is 0
plt.axis('equal') #x-axis and y-axis scale equally
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.legend(loc = 'upper left')plt.show()
去掉边框的反正弦, 反余弦函数,反正切函数曲线函数:
1.12, 我们再直接画出arcsin(x), arccos(x), arctan(x)和arccot(x)的函数曲线图吧。
1.13, 最后还有反双曲正弦,反双曲余弦,反双曲正切, 反双曲余切函数曲线,留给大家自己去发挥吧,注意x和y的值域。
最后,祝大家学习快乐,转载请注明出处。
python中内置数学函数详解和实例应用之三角函数曲线_初级阶段(三)相关推荐
- python中内置数学函数详解和实例应用之三角函数_初级阶段(二)
学习目标: 利用python进行三角函数运算 学习内容: python中的三角函数合集和应用实例 转载请注明出处! 学习产出: 环境:python 3.7, 利用shell进行实例操作. Note: ...
- python平方数迭代器_对python中的高效迭代器函数详解
python中内置的库中有个itertools,可以满足我们在编程中绝大多数需要迭代的场合,当然也可以自己造轮子,但是有现成的好用的轮子不妨也学习一下,看哪个用的顺手~ 首先还是要先import一下: ...
- python中plt定义,对Python中plt的画图函数详解
1.plt.legend plt.legend(loc=0)#显示图例的位置,自适应方式 说明: 'best' : 0, (only implemented for axes legends)(自适应 ...
- COMSOL中内置数学函数及内置运算符
前言 最近想通过COMSOL实现一些比较高级的操作,就对内置数学函数和内置运算符相关章节的帮助文档进行了阅读.在COMSOL中使用各类算子及运算符能够在建模时起到很大的帮助,本文将对帮助文档中的相应章 ...
- python中文件读写--open函数详解
python中open函数详解 在python中文件的读取分为三步走: 读:打开文件 -> 读文件 -> 关闭文件 (有点像把大象放进冰箱需要几步?的问题) 1.open函数 open函数 ...
- awk内置字符串函数详解
awk提供了许多强大的字符串函数,见下表: awk内置字符串函数 gsub(r,s) 在整个$0中用s替代r gsub(r,s,t) 在整个t中用s替代r index(s,t) 返回s中字符串t的第一 ...
- python __repr__方法_第8.13节 Python类中内置方法__repr__详解
当我们在交互环境下输入对象时会直接显示对象的信息,交互环境下输入print(对象)或代码中print(对象)也会输出对象的信息,这些输出信息与两个内置方法:__str__方法和__repr__方法有关 ...
- [转载] Python 中reduce()与lambda函数详解
参考链接: Python中的reduce a = [2,22,222] Sn = reduce(lambda x,y:x+y,a) print "计算和为:",Sn reduce( ...
- python中input()函数详解
1.input()函数赋值后数据在python内部的类型. if __name__=="__main__":a=input()print(type(a))b=input()prin ...
最新文章
- Ubuntu安装google拼音输入法
- 使用 Android NDK 的交叉编译工具链移植 C/C++ 项目到安卓平台
- android 多布局
- 模拟调频信号FM解调的matlab和C语言实现
- mysql 日志 设置 set_MySQL 慢查询日志的开启与配置
- python if elif else
- Pytorch 手工复现交叉熵损失(Cross Entropy Loss)
- 【Oracle】三种方式查看SQL语句的执行计划
- uniapp微信小程序获取位置(高德SDK)
- 自我监督学习和无监督学习_弱和自我监督的学习-第3部分
- makefile编写rtl仿真脚本
- MySQL基础——DDL语句
- 两种典型的解空间树:子集树和排列树
- xp无法搜索计算机,windows xp系统笔记本电脑搜索不到无线信号的解决方法
- 【JAVASE】缓冲流,转换流,序列化流,打印流
- BugKu ——WP(MISC[二])
- Kali扫描工具Recon-NG
- cgroup driver: cgroupfs还是systemd
- SpringBoot下实现华为云短信验证功能(含代码)
- android 死亡自动启动,android – 服务在Activity的’死亡’崩溃