使用python绘制数学函数图像很方便,在构造函数自变量取值时可以利用随机数生成模块,因本人工作需要,现将python中随机数的使用,以及二次函数图像绘制进行梳理总结
目录
使用方式:import random
(1)产生整数
random.randint(a, b)
说明:生成区间在[a, b]的随机整数
random.randrange(start, stop, step)
说明:生成区间在[a, b],按step递增的随机数
(2)产生浮点数
random.random()
说明:生成区间在[0.0, 1.0)的浮点数
random.uniform(a, b)
说明:生成区间在[a, b]的浮点数
(3)区间随机选择
random.choice([...])
说明:在区间[...]中随机选择1个元素
random.sample([...], n)
说明:在区间[...]中随机选择n个元素
(4)区间随机打乱
random.shuffle([...])
说明:把区间[...]中的元素随机打乱
使用方法:import numpy as np
(1)产生整数
np.random.randint(low, high, size)
说明:在区间[low, high)随机生成size个整数
np.random.randint(low, high, size)
说明:在区间[low, high)随机生成size个整数
np.arange(low, high, step)
说明:在区间[low, high)中按照步长step生成列表
(2)产生浮点数
np.random.rand(n)
说明:在[0.0, 1.0)之间的随机浮点产生n个浮点数
np.random.randn(n)
说明:随机生成n个具有正态分布的浮点数
(3)随机打乱
np.random.shuffle(arr)
将数组arr随机打乱顺序
使用方法:import matplotlib.pyplot as plt
plt.figure
说明:产生一个图标
sub = fig.add_subplot
说明:创建子图sub
plt.plot
说明:绘制图形
sub1.plot
子图绘制图形
如下代码在python3.6.9上演示使用随机数模块生成函数自变量来绘制二次函数图形,在实际工作有参考作用,代码如下:
- import matplotlib.pyplot as plt
- import numpy as np
- import random
- import string
-
- #range()函数是python的内置函数,它能返回一系列连续添加的整数,能够生成一个列表对象。
- myListX = list(range(-100,101))
- myListY = []
-
- #随机数的产生方法
- #方法一:random模块
- #产生[1, 10]之间的随机数
- print(random.randint(1, 10))
- #产生[1, 10)之间的随机数
- print(random.randrange(1, 10))
- #产生[0, 100)之间的偶数
- print(random.randrange(0, 101, 2))
- #产生[0, 1.0)之间的随机浮点数
- print(random.random())
- #产生[1.0, 2.0]之间的随机浮点数
- print(random.uniform(1, 2))
- #从fsjfksdj973459中随机选择一个字符
- print(random.choice("fsjfksdj973459"))
- #从a-z A-Z 0-9生成5个不重复的随机字符
- print(random.sample(string.ascii_letters + string.digits, 10))
- print(random.sample("dfsfs", 2))
- print(random.sample([1, 2, 3, 4], 2))
- #打乱列表顺序
- testList = ["123av", 1, 2, 3, 4, 5, 6]
- print(testList)
- random.shuffle(testList)
- print(testList)
-
- #方法二:numpy模块
- #生成10个[0.0, 1.0)之间的随机浮点数数组
- print(np.random.rand(10))
- #生成10个具有正态分布的浮点数数数组
- print(np.random.randn(10))
- #随机返回[1, 10)长度为2的整形数组
- print(np.random.randint(1, 10, 2))
- #随机返回[1, 20)长度为5的整形数组
- print(np.random.random_integers(1, 20, 5))
- #分别产生不大于10和不大于20的两个数
- print(np.random.randint(10), np.random.random_integers(20))
- #产生[1, 20)步长为2的数组
- arr = np.arange(1, 20, 2)
- print(arr)
- #将数组arr打乱顺序
- np.random.shuffle(arr)
- print(arr)
-
- #num:标题
- #figsize:8英寸宽 4英寸高
- #dpi:每英寸90个像素
- #facecolor:背景颜色绿色
- #frameon是否显示边框
- fig = plt.figure(num="plt绘制曲线", figsize=(10, 8), dpi=90, facecolor="green", frameon=False)
-
- #创建2行3列子图
- sub1 = fig.add_subplot(2, 3, 1) #参数为(232)也可以
- sub2 = fig.add_subplot(2, 3, 2)
- sub3 = fig.add_subplot(2, 3, 3)
- sub4 = fig.add_subplot(2, 3, 4)
- sub4 = fig.add_subplot(2, 3, 5)
- sub4 = fig.add_subplot(2, 3, 6)
-
- for x in myListX:
- myListY.append((x) * (x))
- #默认绘制在最后一个子图上
- plt.plot(myListX, myListY, 'green', label='y=x*x')
-
- myListY.clear()
- for x in myListX:
- myListY.append((x - 10) * (x - 10))
- sub1.set_title("y=(x-10)^2")
- sub1.plot(myListX, myListY, 'red', label='y=(x-10)*(x-10)')
-
- myListX = np.arange(-101, 101)
- def fun1(x):
- return (x+20) * (x+20)
- myListY = fun1(myListX)
- sub2.set_title("y=(x+20)^2")
- sub2.plot(myListX, myListY, 'y.', label='y=(x+20)^2')
-
- myListX = np.random.rand(50)*4* np.pi - 2*np.pi
- def fun2(x):
- return np.sin(x)+0.5*x
- myListY = fun2(myListX)
- sub3.set_title("y=sin(x)+0.5x and nihe")
- sub3.plot(myListX, myListY, 'b.', label='')
- #一行两列,在第二列,会把原来的第一行第二列和第二行第二列合并
- #plt.subplot(122).plot(myListX, myListY, 'b.', label='')
-
- #噪声数据xu,yu,得到拟合多项式系数,自由度为5
- reg = np.polyfit(myListX, myListY, 5)
- #计算多项式的函数值。返回在x处多项式的值,p为多项式系数,元素按多项式降幂排序
- rMyListY = np.polyval(reg, myListX)
- sub3.plot(myListX, rMyListY, 'r.', label='regression')#红色点状
-
- plt.legend(loc=0)
- plt.show()
运行结果如下:

