python中利用随机数绘制曲线
创始人
2024-04-16 09:06:13
0

使用python绘制数学函数图像很方便,在构造函数自变量取值时可以利用随机数生成模块,因本人工作需要,现将python中随机数的使用,以及二次函数图像绘制进行梳理总结

目录

1. python中的随机数产生

1.1 random模块

1.1 numpy.random模块

2. 绘制函数图形

matplotlib.pyplot模块绘图

3.调用举例


1. python中的随机数产生

1.1 random模块

使用方式: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([...])

说明:把区间[...]中的元素随机打乱

1.1 numpy.random模块

使用方法: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随机打乱顺序

2. 绘制函数图形

matplotlib.pyplot模块绘图

使用方法:import matplotlib.pyplot as plt

plt.figure

说明:产生一个图标

sub = fig.add_subplot

说明:创建子图sub

plt.plot

说明:绘制图形

sub1.plot

子图绘制图形

3.调用举例

如下代码在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()

运行结果如下:

 

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
ChatGPT 怎么用最新详细... ChatGPT 以其强大的信息整合和对话能力惊艳了全球,在自然语言处理上面表现出了惊人...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...