Python笔记之用turtle库绘制三角函数和反三角函数的图像(考研党福利)

Grace ·
更新时间:2024-09-21
· 828 次阅读

文章目录前言程序源代码函数图像sin(x)图像arcsin(x)图像cos(x)图像arccos(x)图像tan(x)图像arctan(x)图像cot(x)图像arccot(x)图像sec(x)图像arcsec(x)图像csc(x)图像arccsc(x)图像 前言

最近因为复习高数在网上找三角函数图像,发现大多数都模糊不清,实在是头痛,所以自己学以致用,用Python画出了三角函数图像。希望我的博客可以帮到大家,也祝考研党们早日上岸,考上理想的学校!
想了解代码的详细信息可以去看我的另一篇博客:
https://blog.csdn.net/qq_44437695/article/details/104762815

程序源代码 from math import * from tkinter import mainloop from turtle import * # 画出坐标轴 def axis(t): s = Screen() s.setworldcoordinates(-4, -4, 4, 4) t.speed(0) s.delay(0) t.goto(-4, 0) for i in range(-4, 4): t.write(i, False, "right", ("Arial", 12, "normal")) t.fd(1) t.stamp() t.write("x", False, "center", ("Arial", 20, "normal")) t.penup() t.goto(0, -4) t.pendown() t.left(90) for i in range(-4, 4): t.write(i, False, "right", ("Arial", 12, "normal")) t.fd(1) t.write("y", False, "center", ("Arial", 20, "normal")) t.stamp() t.penup() t.hideturtle() # 三角函数的sin(x)、arcsin(x)、cos(x)、arccos(x)、tan(x)、arctan(x)均可以用math库调用 # 余切函数cot(x) def cot(x): if tan(x) != 0: return 1 / tan(x) else: exit("x取值不在定义域内!") # 反余切函数arccot(x) def acot(x): if x > 0: return atan(1 / x) elif x = 1: return atan(sqrt(pow(x, 2) - 1)) elif x = 1: return acot(sqrt(pow(x, 2) - 1)) elif x <= -1: return 0 - acot(sqrt(pow(x, 2) - 1)) else: exit("x取值不在定义域内!") # 在坐标轴上画出正弦函数sin(x)图像 def sin_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("正弦函数sin(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(3 * pi / 2, -1) t.write("(3*π/2,-1)", False, "center", ("Arial", 15, "normal")) t.goto(pi, 0) t.write("(π,0)", False, "left", ("Arial", 15, "normal")) t.goto(pi / 2, 1) t.write("(π/2,1)", False, "center", ("Arial", 15, "normal")) t.goto(0, 0) t.write("(0,0)", False, "left", ("Arial", 15, "normal")) t.goto(-pi / 2, -1) t.write("(-π/2,-1)", False, "center", ("Arial", 15, "normal")) t.goto(-pi, 0) t.write("(-π,0)", False, "right", ("Arial", 15, "normal")) t.goto(-3 * pi / 2, 1) t.write("(-3*π/2,1)", False, "center", ("Arial", 15, "normal")) t.color("black") t.pendown() for i in range(-5000, 5000): x = 0.001 * i t.goto(x, sin(x)) # 在坐标轴上画出反正弦函数asin(x)图像 def asin_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("反正弦函数asin(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(1, pi / 2) t.write("(1,π/2)", False, "left", ("Arial", 15, "normal")) t.goto(-1, -pi / 2) t.write("(-1,-π/2)", False, "right", ("Arial", 15, "normal")) t.color("black") t.pendown() for i in range(-1000, 1000): x = 0.001 * i t.goto(x, asin(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(0, pi / 2) t_line.pendown() t_line.fd(1) t_line.right(90) t_line.fd(pi / 2) t_line.penup() t_line.goto(-1, 0) t_line.pendown() t_line.fd(pi / 2) t_line.left(90) t_line.fd(1) # 在坐标轴上画出余弦函数cos(x)图像 def cos_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("余弦函数cos(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(3 * pi / 2, 0) t.write("(3*π/2,0)", False, "center", ("Arial", 15, "normal")) t.goto(pi, -1) t.write("(π,-1)", False, "left", ("Arial", 15, "normal")) t.goto(pi / 2, 0) t.write("(π/2,0)", False, "center", ("Arial", 15, "normal")) t.goto(0, 1) t.write("(0,1)", False, "left", ("Arial", 15, "normal")) t.goto(-pi / 2, 0) t.write("(-π/2,0)", False, "center", ("Arial", 15, "normal")) t.goto(-pi, -1) t.write("(-π,-1)", False, "right", ("Arial", 15, "normal")) t.goto(-3 * pi / 2, 0) t.write("(-3*π/2,0)", False, "center", ("Arial", 15, "normal")) t.color("black") t.pendown() for i in range(-5000, 5000): x = 0.001 * i t.goto(x, cos(x)) # 在坐标轴上画出反余弦函数acos(x)图像 def acos_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("反余弦函数acos(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(1, 0) t.write("(1,0)", False, "left", ("Arial", 15, "normal")) t.goto(-1, pi) t.write("(-1,π)", False, "right", ("Arial", 15, "normal")) t.color("black") t.pendown() for i in range(-1000, 1000): x = 0.001 * i t.goto(x, acos(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(-1, 0) t_line.pendown() t_line.left(90) t_line.fd(pi) t_line.right(90) t_line.fd(1) # 在坐标轴上画出正切函数tan(x)图像 def tan_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("正切函数tan(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(-pi, 0) t.write("(-π,0)", False, "right", ("Arial", 15, "normal")) t.goto(0, 0) t.write("(0,0)", False, "left", ("Arial", 15, "normal")) t.goto(pi, 0) t.write("(π,0)", False, "left", ("Arial", 15, "normal")) t.color("black") t.goto(-4.467, tan(-4.467)) t.pendown() for i in range(-4467, -1817): x = 0.001 * i t.goto(x, tan(x)) t.penup() t.goto(-1.325, tan(-1.325)) t.pendown() for i in range(-1325, 1329): x = 0.001 * i t.goto(x, tan(x)) t.penup() t.goto(1.817, tan(1.817)) t.pendown() for i in range(1817, 4467): x = 0.001 * i t.goto(x, tan(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(-pi / 2, -4) t_line.write("渐近线x=-π/2", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.left(90) t_line.fd(8) t_line.penup() t_line.goto(pi / 2, -4) t_line.write("渐近线x=π/2", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(8) # 在坐标轴上画出反正切函数arctan(x)图像 def atan_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("反正切函数arctan(x)图像", False, "center", ("Arial", 20, "normal")) t.goto(-5, atan(-5)) t.pendown() for i in range(-5000, 5000): x = 0.001 * i t.goto(x, atan(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(-5, pi / 2) t_line.write("渐近线y=π/2", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) t_line.penup() t_line.goto(-5, -pi / 2) t_line.write("渐近线y=-π/2", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) # 在坐标轴上画出余切函数cot(x)图像 def cot_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("余切函数cot(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(-3 * pi / 2, 0) t.write("(-3*π/2,0)", False, "center", ("Arial", 15, "normal")) t.goto(-pi / 2, 0) t.write("(-π/2,0)", False, "center", ("Arial", 15, "normal")) t.goto(pi / 2, 0) t.write("(π/2,0)", False, "center", ("Arial", 15, "normal")) t.goto(3 * pi / 2, 0) t.write("(3*π/2,0)", False, "center", ("Arial", 15, "normal")) t.color("black") t.goto(-6.038, cot(-6.038)) t.pendown() for i in range(-6038, -3388): x = 0.001 * i t.goto(x, cot(x)) t.penup() t.goto(-2.896, cot(-2.896)) t.pendown() for i in range(-2896, -246): x = 0.001 * i t.goto(x, cot(x)) t.penup() t.goto(0.246, cot(0.246)) t.pendown() for i in range(246, 2896): x = 0.001 * i t.goto(x, cot(x)) t.penup() t.goto(3.388, cot(3.388)) t.pendown() for i in range(3388, 6038): x = 0.001 * i t.goto(x, cot(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(-pi, -4) t_line.write("渐近线x=-π", False, "left", ("Arial", 20, "normal")) t_line.pendown() t_line.left(90) t_line.fd(8) t_line.penup() t_line.goto(0, -4) t_line.write("渐近线x=0", False, "left", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(8) t_line.penup() t_line.goto(pi, -4) t_line.write("渐近线x=π", False, "left", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(8) # 在坐标轴上画出反余切函数arccot(x)图像 def acot_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("反余切函数arccot(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(0, pi / 2) t.write("(0,π/2)", False, "left", ("Arial", 15, "normal")) t.color("black") t.goto(-5, acot(-5)) t.pendown() for i in range(-5000, 5000): x = 0.001 * i t.goto(x, acot(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(-5, pi) t_line.write("渐近线y=π", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) t_line.penup() t_line.goto(-5, 0) t_line.write("渐近线y=0", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) # 在坐标轴上画出正割函数sec(x)图像 def sec_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("正割函数sec(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(0, 1) t.write("(0,1)", False, "left", ("Arial", 15, "normal")) t.goto(-pi, -1) t.write("(-π,-1)", False, "center", ("Arial", 15, "normal")) t.goto(pi, -1) t.write("(π,-1)", False, "center", ("Arial", 15, "normal")) t.color("black") t.goto(-4.467, sec(-4.467)) t.pendown() for i in range(-4467, -1817): x = 0.001 * i t.goto(x, sec(x)) t.penup() t.goto(-1.325, sec(-1.325)) t.pendown() for i in range(-1325, 1329): x = 0.001 * i t.goto(x, sec(x)) t.penup() t.goto(1.817, sec(1.817)) t.pendown() for i in range(1817, 4467): x = 0.001 * i t.goto(x, sec(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(-pi / 2, -4) t_line.write("渐近线x=-π/2", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.left(90) t_line.fd(8) t_line.penup() t_line.goto(pi / 2, -4) t_line.write("渐近线x=π/2", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(8) t_line.right(90) t_line.penup() t_line.goto(-5, 1) t_line.write("y=1", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) t_line.penup() t_line.goto(-5, -1) t_line.write("y=-1", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) # 在坐标轴上画出反正割函数arcsec(x)图像 def asec_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("反正割函数arcsec(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(1, 0) t.write("(1,0)", False, "left", ("Arial", 15, "normal")) t.color("black") t.pendown() for i in range(1000, 5000): x = 0.001 * i t.goto(x, asec(x)) t.penup() t.color("blue") t.goto(-1, pi) t.write("(-1,π)", False, "right", ("Arial", 15, "normal")) t.color("black") t.pendown() for i in range(-1000, -5000, -1): x = 0.001 * i t.goto(x, asec(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(-1, 0) t_line.pendown() t_line.left(90) t_line.fd(pi) t_line.right(90) t_line.fd(1) t_line.penup() t_line.goto(-5, pi / 2) t_line.write("渐近线y=π/2", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) # 在坐标轴上画出余割函数csc(x)图像 def csc_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("余割函数csc(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(-3 * pi / 2, 1) t.write("(-3*π/2,1)", False, "left", ("Arial", 15, "normal")) t.goto(-pi / 2, -1) t.write("(-π/2,-1)", False, "center", ("Arial", 15, "normal")) t.goto(pi / 2, 1) t.write("(π/2,1)", False, "center", ("Arial", 15, "normal")) t.goto(3 * pi / 2, -1) t.write("(3*π/2,-1)", False, "center", ("Arial", 15, "normal")) t.color("black") t.goto(-6.038, csc(-6.038)) t.pendown() for i in range(-6038, -3388): x = 0.001 * i t.goto(x, csc(x)) t.penup() t.goto(-2.896, csc(-2.896)) t.pendown() for i in range(-2896, -246): x = 0.001 * i t.goto(x, csc(x)) t.penup() t.goto(0.246, csc(0.246)) t.pendown() for i in range(246, 2896): x = 0.001 * i t.goto(x, csc(x)) t.penup() t.goto(3.388, csc(3.388)) t.pendown() for i in range(3388, 6038): x = 0.001 * i t.goto(x, csc(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(-pi, -4) t_line.write("渐近线x=-π", False, "left", ("Arial", 20, "normal")) t_line.pendown() t_line.left(90) t_line.fd(8) t_line.penup() t_line.goto(0, -4) t_line.write("渐近线x=0", False, "left", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(8) t_line.penup() t_line.goto(pi, -4) t_line.write("渐近线x=π", False, "left", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(8) t_line.right(90) t_line.penup() t_line.goto(-5, 1) t_line.write("y=1", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) t_line.penup() t_line.goto(-5, -1) t_line.write("y=-1", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) # 在坐标轴上画出反余割函数arccsc(x)图像 def acsc_draw(): t = Turtle() axis(t) t.goto(-4, 4) t.write("反余割函数arccsc(x)图像", False, "center", ("Arial", 20, "normal")) t.color("blue") t.goto(1, pi / 2) t.write("(1,π/2)", False, "left", ("Arial", 15, "normal")) t.color("black") t.pendown() for i in range(1000, 5000): x = 0.001 * i t.goto(x, acsc(x)) t.penup() t.color("blue") t.goto(-1, -pi / 2) t.write("(-1,-π/2)", False, "right", ("Arial", 15, "normal")) t.color("black") t.pendown() for i in range(-1000, -5000, -1): x = 0.001 * i t.goto(x, acsc(x)) t_line = Turtle() t_line.hideturtle() t_line.color("green") t_line.speed(0) t_line.penup() t_line.goto(0, pi / 2) t_line.pendown() t_line.fd(1) t_line.right(90) t_line.fd(pi / 2) t_line.right(90) t_line.fd(2) t_line.left(90) t_line.fd(pi / 2) t_line.left(90) t_line.fd(1) t_line.penup() t_line.goto(-5, 0) t_line.write("渐近线y=0", False, "right", ("Arial", 20, "normal")) t_line.pendown() t_line.fd(10) if __name__ == '__main__': sin_draw() mainloop() 函数图像 sin(x)图像

在这里插入图片描述

arcsin(x)图像

在这里插入图片描述

cos(x)图像

在这里插入图片描述

arccos(x)图像

在这里插入图片描述

tan(x)图像

在这里插入图片描述

arctan(x)图像

在这里插入图片描述

cot(x)图像

在这里插入图片描述

arccot(x)图像

在这里插入图片描述

sec(x)图像

在这里插入图片描述

arcsec(x)图像

在这里插入图片描述

csc(x)图像

在这里插入图片描述

arccsc(x)图像

在这里插入图片描述


作者:上轩希言



三角函数 考研 函数 turtle Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号