Python函数可视化工具
目录
1.简介
对于如今的中学生乃至大学生,只要接触到数学相关内容的,就必然离不开函数这一“生死大关”。为什么说是生死大关呢?实在是函数类问题太令人头疼。那么,在进行此类解题(尤其是三角函数)及性质研究时,有没有一种方便、快捷的方法,让我们能够更透彻地,特别是不用手动画图的那种,研究函数的性质呢?当然有,这就是Python函数图像工具(EXE) 。
本程序运用Python中最令人喜爱的数据处理工具numpy和超强的图像库matplotlib,实现13种不同类别函数的分类图像整理,展示图像均可以保存为图片的形式,具备拖动、放大等功能。当然,因为教程的关系,我们就不再多说,有兴趣的小伙伴可以点击上方链接运行试一下。详细介绍:Python函数图像工具介绍
2.教程 -- 一次函数
先上效果图(当然只是1/13的成果):
从这里不难看出,整个一次函数图像的显示总共分为3步:分别是【主画板显示+函数名处理】、【坐标轴构建】以及【函数图像显示】。我们一块一块来说。
1)倒库
import matplotlib.pyplot as plt #图像库
import numpy as np #数据处理
import math #数学计算
2)主画板显示+函数名处理
主画板主要就是 plt.plot() 的构建,但注意我们还要命名,构建坐标轴等,所以先用 figure()来代替一下,结尾是 plt.tight_layout()。
x = np.linspace(left, right, abs(100*left*right)) #规定区间,第三个空是精度,太高会耗内存
#y = ax+b --函数操作,当然现在是数学语言
fig = plt.figure(title, figsize=(10, 8)) #title是名字,figsize窗口大小
fig.canvas.manager.window.wm_geometry('+450+50') #固定窗口位置
plt.tight_layout()
那么现在就要考虑函数名的问题了,这其实也是非常难搞的事情,如果操作不当,就会出很大的问题。
函数y=ax+b中,我们一定要确定的几种特殊情况是:
1.a=0时,y=b;a=1时,y=x+b;a=-1时,y=-x+b(不能写b-x)
2.b=0时,y=ax;b<0时,y=ax-|b|(不能输出加号)
那么,我们就可以尝试构建代码了(这里coefficient1表示一次项系数,也就是a,constant是系数,str_cfc和str_cst分别表示处理过的字符串格式的数据):
x = np.linspace(left, right, abs(100*left*right))
#y = ax+b
#数据处理
if coefficient_1 > 0:
if coefficient_1 == 1:
str_cfc1 = "x"
else:
str_cfc1 = str(coefficient_1)+"x"
if constant == 0:
str_cst = ""
elif constant > 0:
str_cst = "+"+str(constant)
else:
str_cst = str(constant)
else:
if coefficient_1 == -1:
str_cfc1 = "-x"
else:
str_cfc1 = str(coefficient_1)+"x"
if constant == 0:
str_cst = ""
elif constant > 0:
str_cst = "+"+str(constant)
else:
str_cst = "+"+str(constant)
#linear funscion 一次函数的名称搭建
fig = plt.figure("linear function: y="+str_cfc1 + str_cst, figsize=(10, 8))
fig.canvas.manager.window.wm_geometry('+450+50')
plt.tight_layout()
plt.show() #显示图像
给数据随便赋个值(注意left应该<=0,否则后面会有问题),运行结果:
可以明显看出,程序已经初具雏形了。
3)坐标轴构建+函数图像显示
事实上,坐标轴的构建就是把x,y轴从左下角拉到中间,再表上数值的过程。上代码:
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.set(xlim=[left, right], ylim=[left, right])
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
加上这一段话,运行结果:
真的已经和最终结果相差不远了,那么差点什么呢......没错!函数图像!!!
别忘了我们的目标是做出Python函数图像工具,现在万事俱备,最为关键的函数可不能少。
继续分析:
在前面的代码中,我们已经知道了x的值:
x = np.linspace(left, right, abs(100*left*right))
没错,x坐标的集合就是在 (left, right) 的区间内的每一个取值,abs(100*left*right)就是百倍的精度(即每单位长度显示多少个数值)。那么y坐标又如何表示呢?很简单,y=ax+b,转换成Python语言如下:
y = coefficient_1*x+constant
最后上传数据:
plt.plot(x, y)
组合+封装(函数)
def linear_function(left, right, coefficient_1, constant):
"""
函数名: linear_function
功能: 显示形如y=ax+b(a≠0)的一次函数的图像
参数:
left: 显示的区间(下界)
right: 显示的区间(上界)
coefficient_1: 一次项系数
constant: 常数项
"""
x = np.linspace(left, right, abs(100*left*right))
y = coefficient_1*x+constant
if coefficient_1 > 0:
if coefficient_1 == 1:
str_cfc1 = "x"
else:
str_cfc1 = str(coefficient_1)+"x"
if constant == 0:
str_cst = ""
elif constant > 0:
str_cst = "+"+str(constant)
else:
str_cst = str(constant)
else:
if coefficient_1 == -1:
str_cfc1 = "-x"
else:
str_cfc1 = str(coefficient_1)+"x"
if constant == 0:
str_cst = ""
elif constant > 0:
str_cst = "+"+str(constant)
else:
str_cst = "+"+str(constant)
fig = plt.figure("linear function: y="+str_cfc1 + str_cst, figsize=(10, 8))
fig.canvas.manager.window.wm_geometry('+450+50')
plt.plot(x, y)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.set(xlim=[left, right], ylim=[left, right])
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.tight_layout()
plt.show()
运行结果:
全部成功!
其它:
其它函数的做法,基本大同小异,整理过的代码如下(EXE下载:Python函数图像工具):
#from posixpath import lexists
import matplotlib.pyplot as plt
import numpy as np
import math
pai = 3.1415926
def linear_function(left, right, coefficient_1, constant):
"""
函数名: linear_function
功能: 显示形如y=ax+b(a≠0)的一次函数的图像
参数:
left: 显示的区间(下界)
right: 显示的区间(上界)
coefficient_1: 一次项系数
constant: 常数项
"""
x = np.linspace(left, right, abs(100*left*right))
y = coefficient_1*x+constant
if coefficient_1 > 0:
if coefficient_1 == 1:
str_cfc1 = "x"
else:
str_cfc1 = str(coefficient_1)+"x"
if constant == 0:
str_cst = ""
elif constant &