• python中利用随机数绘制曲线


    使用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上演示使用随机数模块生成函数自变量来绘制二次函数图形,在实际工作有参考作用,代码如下:

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. import random
    4. import string
    5. #range()函数是python的内置函数,它能返回一系列连续添加的整数,能够生成一个列表对象。
    6. myListX = list(range(-100,101))
    7. myListY = []
    8. #随机数的产生方法
    9. #方法一:random模块
    10. #产生[1, 10]之间的随机数
    11. print(random.randint(1, 10))
    12. #产生[1, 10)之间的随机数
    13. print(random.randrange(1, 10))
    14. #产生[0, 100)之间的偶数
    15. print(random.randrange(0, 101, 2))
    16. #产生[0, 1.0)之间的随机浮点数
    17. print(random.random())
    18. #产生[1.0, 2.0]之间的随机浮点数
    19. print(random.uniform(1, 2))
    20. #从fsjfksdj973459中随机选择一个字符
    21. print(random.choice("fsjfksdj973459"))
    22. #从a-z A-Z 0-9生成5个不重复的随机字符
    23. print(random.sample(string.ascii_letters + string.digits, 10))
    24. print(random.sample("dfsfs", 2))
    25. print(random.sample([1, 2, 3, 4], 2))
    26. #打乱列表顺序
    27. testList = ["123av", 1, 2, 3, 4, 5, 6]
    28. print(testList)
    29. random.shuffle(testList)
    30. print(testList)
    31. #方法二:numpy模块
    32. #生成10个[0.0, 1.0)之间的随机浮点数数组
    33. print(np.random.rand(10))
    34. #生成10个具有正态分布的浮点数数数组
    35. print(np.random.randn(10))
    36. #随机返回[1, 10)长度为2的整形数组
    37. print(np.random.randint(1, 10, 2))
    38. #随机返回[1, 20)长度为5的整形数组
    39. print(np.random.random_integers(1, 20, 5))
    40. #分别产生不大于10和不大于20的两个数
    41. print(np.random.randint(10), np.random.random_integers(20))
    42. #产生[1, 20)步长为2的数组
    43. arr = np.arange(1, 20, 2)
    44. print(arr)
    45. #将数组arr打乱顺序
    46. np.random.shuffle(arr)
    47. print(arr)
    48. #num:标题
    49. #figsize:8英寸宽 4英寸高
    50. #dpi:每英寸90个像素
    51. #facecolor:背景颜色绿色
    52. #frameon是否显示边框
    53. fig = plt.figure(num="plt绘制曲线", figsize=(10, 8), dpi=90, facecolor="green", frameon=False)
    54. #创建2行3列子图
    55. sub1 = fig.add_subplot(2, 3, 1) #参数为(232)也可以
    56. sub2 = fig.add_subplot(2, 3, 2)
    57. sub3 = fig.add_subplot(2, 3, 3)
    58. sub4 = fig.add_subplot(2, 3, 4)
    59. sub4 = fig.add_subplot(2, 3, 5)
    60. sub4 = fig.add_subplot(2, 3, 6)
    61. for x in myListX:
    62. myListY.append((x) * (x))
    63. #默认绘制在最后一个子图上
    64. plt.plot(myListX, myListY, 'green', label='y=x*x')
    65. myListY.clear()
    66. for x in myListX:
    67. myListY.append((x - 10) * (x - 10))
    68. sub1.set_title("y=(x-10)^2")
    69. sub1.plot(myListX, myListY, 'red', label='y=(x-10)*(x-10)')
    70. myListX = np.arange(-101, 101)
    71. def fun1(x):
    72. return (x+20) * (x+20)
    73. myListY = fun1(myListX)
    74. sub2.set_title("y=(x+20)^2")
    75. sub2.plot(myListX, myListY, 'y.', label='y=(x+20)^2')
    76. myListX = np.random.rand(50)*4* np.pi - 2*np.pi
    77. def fun2(x):
    78. return np.sin(x)+0.5*x
    79. myListY = fun2(myListX)
    80. sub3.set_title("y=sin(x)+0.5x and nihe")
    81. sub3.plot(myListX, myListY, 'b.', label='')
    82. #一行两列,在第二列,会把原来的第一行第二列和第二行第二列合并
    83. #plt.subplot(122).plot(myListX, myListY, 'b.', label='')
    84. #噪声数据xu,yu,得到拟合多项式系数,自由度为5
    85. reg = np.polyfit(myListX, myListY, 5)
    86. #计算多项式的函数值。返回在x处多项式的值,p为多项式系数,元素按多项式降幂排序
    87. rMyListY = np.polyval(reg, myListX)
    88. sub3.plot(myListX, rMyListY, 'r.', label='regression')#红色点状
    89. plt.legend(loc=0)
    90. plt.show()

    运行结果如下:

     

  • 相关阅读:
    什么是泛型
    设计模式-责任链模式
    《刷新:重新发现商业与未来》~语句摘录
    MWC 2024:华为手机展现科技创新实力,持续强化高端科技品牌形象
    关于企业微信中开发第三方应用遇到的退出问题
    [1024]程序员节 一晃6年过去了
    python对象持久化shelve模块
    7.基于SpringBoot3+Security6+JWT实现鉴权机制(一)
    vue2条件渲染v-if VS v-show
    对抗生成网络总结
  • 原文地址:https://blog.csdn.net/hsy12342611/article/details/128151853