python做频率统计图 完整版

Caomeinico
• 阅读 494

from matplotlib import pyplot as plt

import pandas as pd

def linearCongruentialMethod(Xo, m, a, c, randomNums, U):

    randomNums[0] = Xo

    U[0] = randomNums[0] / m

    for i in range(1, 10000):

        # Follow the linear congruential method

        randomNums[i] = ((randomNums[i - 1] * a) + c) % m

        U[i] = randomNums[i] / m

print("a = 1597, b = 0, m = 244944")

a = 1597

c = 0

m = 244944

i = 0.01

for i in range(1, 6):

    X0 = i * 0.01

    print("\n X0 = ", X0, "\n")

    noOfRandomNums = 10005

    randomNums = [0] * (noOfRandomNums)

    U = [0] * (noOfRandomNums)

    linearCongruentialMethod(X0, m, a, c, randomNums, U)

    intervals = 20

    freq = [0] * (intervals)

    for j in U:

        x = 1

        x = j * 100 / 5

        freq[int(x)] = freq[int(x)] + 1

    mydata = {'Interval  ': ['[0.00,0.05)', '[0.05,0.10)', '[0.10,0.15)', '[0.15,0.20)', '[0.20,0.25)', '[0.25,0.30)',

                             '[0.30,0.35)', '[0.35,0.40)', '[0.40,0.45)', '[0.45,0.50)', '[0.50,0.55)', '[0.55,0.60)',

                             '[0.60,0.65)', '[0.65,0.70)', '[0.70,0.75)', '[0.75,0.80)', '[0.80,0.85)', '[0.85,0.90)',

                             '[0.90,0.95)', '[0.95,1.00)'],

              'Freuency': freq}

    df = pd.DataFrame(mydata)

    print(df)

    data = {'1': freq[0], '2': freq[1], '3': freq[2], '4': freq[3], '5': freq[4], '6': freq[5], '7': freq[6],

            '8': freq[7], '9': freq[8], '10': freq[9], '11': freq[10], '12': freq[11], '13': freq[12], '14': freq[13],

            '15': freq[14], '16': freq[15], '17': freq[16], '18': freq[17], '19': freq[18], '20': freq[19], }

    interval = list(data.keys())

    frequency = list(data.values())

    fig = plt.figure(figsize=(10, 5))

    plt.bar(interval, frequency, color='maroon', width=0.4)

    plt.show()

print("\na = 51749, b = 0, m = 244944")

a = 51749

c = 0

m = 244944

i = 0.01

for i in range(1, 6):

    X0 = i * 0.01

    print("\n X0 = ", X0, "\n")

    noOfRandomNums = 10005

    randomNums = [0] * (noOfRandomNums)

    U = [0] * (noOfRandomNums)

    linearCongruentialMethod(X0, m, a, c, randomNums, U)

    intervals = 20

    freq = [0] * (intervals)

    for j in U:

        x = 1

        x = j * 100 / 5

        freq[int(x)] = freq[int(x)] + 1

    mydata = {'Interval  ': ['[0.00,0.05)', '[0.05,0.10)', '[0.10,0.15)', '[0.15,0.20)', '[0.20,0.25)', '[0.25,0.30)',

                             '[0.30,0.35)', '[0.35,0.40)', '[0.40,0.45)', '[0.45,0.50)', '[0.50,0.55)', '[0.55,0.60)',

                             '[0.60,0.65)', '[0.65,0.70)', '[0.70,0.75)', '[0.75,0.80)', '[0.80,0.85)', '[0.85,0.90)',

                             '[0.90,0.95)', '[0.95,1.00)'],

              'Freuency': freq}

    df = pd.DataFrame(mydata)

    print(df)

    data = {'1': freq[0], '2': freq[1], '3': freq[2], '4': freq[3], '5': freq[4], '6': freq[5], '7': freq[6],

            '8': freq[7], '9': freq[8], '10': freq[9], '11': freq[10], '12': freq[11], '13': freq[12], '14': freq[13],

            '15': freq[14], '16': freq[15], '17': freq[16], '18': freq[17], '19': freq[18], '20': freq[19], }

    interval = list(data.keys())

    frequency = list(data.values())

    fig = plt.figure(figsize=(10, 5))

    plt.bar(interval, frequency, color='blue', width=0.4)

    plt.show()

好买网www,goodmai,com

点赞
收藏
评论区
推荐文章
浅梦一笑 浅梦一笑
5个月前
初学 Python 需要安装哪些软件?超级实用,小白必看!
编程这个东西是真的奇妙。对于懂得的人来说,会觉得这个工具是多么的好用、有趣,而对于小白来说,就如同大山一样。其实这个都可以理解,大家都是这样过来的。那么接下来就说一下python相关的东西吧,并说一下我对编程的理解。本人也是小白一名,如有不对的地方,还请各位大神指出01名词解释:如果在编程方面接触的比较少,那么对于软件这一块,有几个名词一定要了解,比如开发环
blmius blmius
1年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
1年前
Python能用来做什么?以下是Python的三大主要用途
!(https://oscimg.oschina.net/oscnet/2a77edbfa77743348fec9927208d6473.gif)!(https://oscimg.oschina.net/oscnet/56f08a006fe1475ab0ba90c59984d4e1.jpg)作者 YKSugi编译 M
Stella981 Stella981
1年前
Python内置简易HTTP服务器的使用
Python内置了一个简单的HTTP服务器,命令行切换到放置网页文件(例如index.html)的目录,只需要在命令行下面敲一行命令,一个HTTP服务器就起来了:1)Python2.x:pythonmSimpleHTTPServer80802)Python3.xpythonmhttp.server80
Stella981 Stella981
1年前
Pipenv创建和管理虚拟环境报的错
1、pipinstallpipenv安装Pipev时报错是因为我安装有python2和python3两个,选择如下的解决方式:1.python2mpipinstallxxx或者2.python3mpipinstallxxx2、pipenvinstall时出现如下错误:call
Easter79 Easter79
1年前
vue+element 表格formatter数据格式化并且插入html标签
前言   vue中element框架,其中表格组件,我既要行内数据格式化,又要插入html标签一贯思维,二者不可兼得也一、element表格数据格式化  !(https://oscimg.oschina.net/oscnet/3c43a1cb3cbdeb5b5ad58acb45a42612b00.p
Wesley13 Wesley13
1年前
Oracle 12c DG备库Alert报错ORA
环境是12.2.0.1version,OracleDataGuard备库近段时间一直报错,但是备库主库同步一致,数据一致。20190306T23:42:22.18404808:00Errorsinfile/u01/app/oracle/diag/rdbms/ccdb/ccdb/trace/ccdb_m000_12
Wesley13 Wesley13
1年前
4cast
4castpackageloadcsv.KumarAwanish发布:2020122117:43:04.501348作者:KumarAwanish作者邮箱:awanish00@gmail.com首页:
Stella981 Stella981
1年前
Python之time模块的时间戳、时间字符串格式化与转换
Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y
Stella981 Stella981
1年前
Shell脚本之sed的使用
1.sed命令;主要作用是查找;新增删除和修改替换。user.txt daokrcatuser.txtIDNameSexAge1zhangM192wangG203chengM104h