Python tkinter库之Canvas 根据函数解析式或参数方程画出图像
Python 函数 解析 参数 图像 根据 Canvas 方程
2023-09-14 09:01:28 时间
Canvas没有画点的函数,我们就用某一点到其右下相邻点的连线来代替。然后自定义一个函数Graph(),把给定函数f(x)在指定区间里的点 (x, f(x)) 依次画出,步进间隔小到一定程度就会连成f(x)对应的曲线。
for ... in range()的步进值只能是整数,所以引入numpy库的arange(),其步进可以是小数,例: for i in numpy.arange(0,2,0.01) # 表示区间[0,1)上步进0.01,共循环200次。
函数解析式的图像
import tkinter as tk
import pyautogui as ag
from math import *
from numpy import arange as np
def Window_Open(W, H):
X, Y = ag.size()
winSize = str(W)+"x"+str(H)
winPos = winSize + "+" + str((X - W) // 2)
winPos += "+" + str((Y - H) // 2)
win.geometry(winPos)
win.resizable(False, False)
title = u'桌面分辨率:' + str(X) + "x" + str(Y)
title += ' ' * 5 + u'窗体大小:' + winSize
win.title(title)
win.update()
def Graph(func,x0,y0,xmin,xmax,w,h,c='blue',step=0.001):
'xmin,xmax 自变量的取值范围; c 图像颜色'
'x0,y0 原点坐标 w,h 横纵轴半长 step 步进'
coord = x0-w,y0,x0+w,y0
tCanvas.create_line(coord,fill='black')
coord = x0,y0-h,x0,y0+h
tCanvas.create_line(coord,fill='black')
w1,w2=100,120 # w1,w2为自变量和函数值在横纵轴上的放大倍数
for x in np(xmin,xmax+step,step):
y = func(x)
coord = x0+w1*x,y0-w2*y,x0+w1*x+1,y0-w2*y+1
if abs(x*w1)<w and abs(y*w2)<h:
tCanvas.create_line(coord,fill=c)
tCanvas.update()
if __name__ == '__main__':
win = tk.Tk()
Window_Open(800,480)
tCanvas = tk.Canvas(win, width=win.winfo_width(), height=480, bg='white')
tCanvas.pack(side="top")
x0,y0=400,240 # 原点坐标
xmax=pi*2 # 自变量最大值,超过横坐标最大值被截短
w,h=380,220 # 横纵轴的正向最大值
fx1 = lambda x : sin(x)+cos(x)
fx2 = lambda x : sin(x)*cos(x)
gx1 = lambda x : exp(abs(x/4))-2
gx2 = lambda x : (x/2)**3-2*(x/2)**2+x/2-1
Graph(sin,x0,y0,-xmax,xmax,w,h)
Graph(cos,x0,y0,-xmax,xmax,w,h,'red')
Graph(fx1,x0,y0,-xmax,xmax,w,h,'lime')
Graph(fx2,x0,y0,-xmax,xmax,w,h,'orange')
Graph(gx1,x0,y0,-xmax,xmax,w,h,'magenta')
Graph(gx2,x0,y0,-xmax,xmax,w,h,'limegreen')
win.mainloop()
效果图:
心形曲线参数方程的图像
把Graph()函数稍作改进,就能画出参数方程对应的曲线图像,如下例:心形曲线的图像
import tkinter as tk
import pyautogui as ag
from math import *
from numpy import arange as np
def Window_Open(W, H):
X, Y = ag.size()
winSize = str(W)+"x"+str(H)
winPos = winSize + "+" + str((X - W) // 2)
winPos += "+" + str((Y - H) // 2)
win.geometry(winPos)
win.resizable(False, False)
title = u'桌面分辨率:' + str(X) + "x" + str(Y)
title += ' ' * 5 + u'窗体大小:' + winSize
win.title(title)
win.update()
def Graph(funcx,funcy,x0,y0,tmin,tmax,w,h,c='blue',step=0.001):
'xmin,xmax 自变量的取值范围; c 图像颜色'
'x0,y0 原点坐标 w,h 横纵轴半长 step 步进'
coord = x0-w,y0,x0+w,y0
tCanvas.create_line(coord,fill='black')
coord = x0,y0-h,x0,y0+h
tCanvas.create_line(coord,fill='black')
w1,w2=100,110 # w1,w2为自变量和函数值在横纵轴上的放大倍数
for t in np(tmin,tmax+step,step):
x = funcx(t)
y = funcy(t)
coord = x0+w1*x,y0-w2*y,x0+w1*x+1,y0-w2*y+1
if abs(x*w1)<w and abs(y*w2)<h:
tCanvas.create_line(coord,fill=c)
tCanvas.update()
if __name__ == '__main__':
win = tk.Tk()
Window_Open(480,480)
tCanvas = tk.Canvas(win, width=win.winfo_width(), height=480, bg='white')
tCanvas.pack(side="top")
x0,y0=240,240 # 原点坐标
a = 0.7
fx = lambda t : a*(2.0*sin(t)-sin(2*t))
fy = lambda t : a*(1.6*cos(t)-cos(2*t))+0.5
Graph(fx,fy,x0,y0,-pi,pi,220,200,c='red')
win.mainloop()
效果图:
椭圆、双曲线、抛物线的图像
import tkinter as tk
import pyautogui as ag
from math import *
from numpy import arange as np
def Window_Open(W, H):
X, Y = ag.size()
winSize = str(W)+"x"+str(H)
winPos = winSize + "+" + str((X - W) // 2)
winPos += "+" + str((Y - H) // 2)
win.geometry(winPos)
win.resizable(False, False)
title = u'桌面分辨率:' + str(X) + "x" + str(Y)
title += ' ' * 5 + u'窗体大小:' + winSize
win.title(title)
win.update()
def Graph(funcx,funcy,x0,y0,tmin,tmax,w,h,c='blue',step=0.001):
'xmin,xmax 自变量的取值范围; c 图像颜色'
'x0,y0 原点坐标 w,h 横纵轴半长 step 步进'
coord = x0-w,y0,x0+w,y0
tCanvas.create_line(coord,fill='black')
coord = x0,y0-h,x0,y0+h
tCanvas.create_line(coord,fill='black')
w1,w2=100,100 # w1,w2为自变量和函数值在横纵轴上的放大倍数
for t in np(tmin,tmax+step,step):
x = funcx(t)
y = funcy(t)
coord = x0+w1*x,y0-w2*y,x0+w1*x+1,y0-w2*y+1
if abs(x*w1)<w and abs(y*w2)<h:
tCanvas.create_line(coord,fill=c)
tCanvas.update()
if __name__ == '__main__':
win = tk.Tk()
Window_Open(480,480)
tCanvas = tk.Canvas(win, width=win.winfo_width(), height=480, bg='white')
tCanvas.pack(side="top")
x0,y0=240,240
a,b = 1,1.2
p = 0.5
f1x = lambda t : a*cos(t)
f1y = lambda t : b*sin(t)
f2x = lambda t : a/cos(t)
f2y = lambda t : b*tan(t)
f3x = lambda t : 2*p*t**2
f3y = lambda t : 2*p*t
Graph(f1x,f1y,x0,y0,-pi,pi,220,200)
Graph(f2x,f2y,x0,y0,-pi,pi,220,200,c='red')
Graph(f3x,f3y,x0,y0,-pi,pi,220,200,c='green')
win.mainloop()
效果图:
附:圆锥曲线的参数方程
椭圆
x = a*cost
y = b*sint
双曲线
x = a*sect
y = b*tant
抛物线
x = 2*p*t²
y = 2*p*t
相关文章
- Python内置函数reversed()用法分析
- Python中sort和sorted函数代码解析
- Python导出DBF文件到Excel的方法
- 小白学 Python 爬虫(37):爬虫框架 Scrapy 入门基础(五) Spider Middleware
- 【Python 22】52周存钱挑战2.0(列表list和math函数)
- python实现飞书创建机器人发送消息
- Python 调试方法
- Python 批量抓取help()函数的帮助内容(附36M帮助文档)
- Python 重解零基础100题(7)
- Python语言学习之lambda:lambda函数的简介、使用方法、案例大全之详细攻略
- Python:利用pandas对两个dataframe数据进行左联接/左关联(以左边为主体)、右联接/右关联(以右边为主体)、内联接(取交集)、外联接(取并集)操作案例实现代码
- Python之pandas:pandas中to_csv()、read_csv()函数的index、index_col(不将索引列写入)参数详解之详细攻略
- 蓝桥杯官网 试题 PREV-113 历届真题 估计人数【第十届】【决赛】【研究生组】【C++】【Java】【Python】三种解法
- 100天精通Python(数据分析篇)——第66天:Pandas透视表基础+实战案例(pivot_table函数)
- Python每日一练(数据分析篇)——第36天:中位函数
- Python每日一练——第27天:内置函数
- 100天精通Python(基础篇)——第22天:eval 函数基础以及危险警告
- 已解决Python爬虫报错<response [406]>
- python二级考试考生必看
- Python getattr() 函数==>获取一个对象的属性值
- 【Python实验】面向对象程序设计
- Python编程:shutil模块-操作目录及文件
- python hasattr()函数,getattr()函数, setattr()函数
- 使用py-spy查看python程序调用堆栈
- Python+Appium实现APP自动化测试
- Python学习笔记(九)—— 函数
- Python典型数据分析流程——纯理论(深入理解的看)
- Python少儿编程入门篇(9)内置函数