python做频率统计图 完整版

一、效果

 二、代码设计

# your code goes here
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()

三、数据区间和频率

控制台记录:

a = 1597, b = 0, m = 244944

 X0 =  0.01 

     Interval    Freuency
0   [0.00,0.05)       518
1   [0.05,0.10)       504
2   [0.10,0.15)       486
3   [0.15,0.20)       490
4   [0.20,0.25)       506
5   [0.25,0.30)       512
6   [0.30,0.35)       498
7   [0.35,0.40)       480
8   [0.40,0.45)       515
9   [0.45,0.50)       490
10  [0.50,0.55)       457
11  [0.55,0.60)       550
12  [0.60,0.65)       517
13  [0.65,0.70)       504
14  [0.70,0.75)       504
15  [0.75,0.80)       490
16  [0.80,0.85)       469
17  [0.85,0.90)       476
18  [0.90,0.95)       518
19  [0.95,1.00)       521

 X0 =  0.02 

     Interval    Freuency
0   [0.00,0.05)       496
1   [0.05,0.10)       479
2   [0.10,0.15)       526
3   [0.15,0.20)       528
4   [0.20,0.25)       499
5   [0.25,0.30)       504
6   [0.30,0.35)       488
7   [0.35,0.40)       506
8   [0.40,0.45)       491
9   [0.45,0.50)       519
10  [0.50,0.55)       496
11  [0.55,0.60)       506
12  [0.60,0.65)       489
13  [0.65,0.70)       478
14  [0.70,0.75)       480
15  [0.75,0.80)       476
16  [0.80,0.85)       502
17  [0.85,0.90)       531
18  [0.90,0.95)       512
19  [0.95,1.00)       499

 X0 =  0.03 

     Interval    Freuency
0   [0.00,0.05)       537
1   [0.05,0.10)       517
2   [0.10,0.15)       462
3   [0.15,0.20)       476
4   [0.20,0.25)       506
5   [0.25,0.30)       498
6   [0.30,0.35)       492
7   [0.35,0.40)       505
8   [0.40,0.45)       512
9   [0.45,0.50)       506
10  [0.50,0.55)       493
11  [0.55,0.60)       508
12  [0.60,0.65)       529
13  [0.65,0.70)       508
14  [0.70,0.75)       489
15  [0.75,0.80)       503
16  [0.80,0.85)       483
17  [0.85,0.90)       504
18  [0.90,0.95)       468
19  [0.95,1.00)       509

 X0 =  0.04 

     Interval    Freuency
0   [0.00,0.05)       522
1   [0.05,0.10)       486
2   [0.10,0.15)       486
3   [0.15,0.20)       497
4   [0.20,0.25)       486
5   [0.25,0.30)       501
6   [0.30,0.35)       500
7   [0.35,0.40)       499
8   [0.40,0.45)       534
9   [0.45,0.50)       528
10  [0.50,0.55)       539
11  [0.55,0.60)       502
12  [0.60,0.65)       454
13  [0.65,0.70)       478
14  [0.70,0.75)       511
15  [0.75,0.80)       496
16  [0.80,0.85)       481
17  [0.85,0.90)       485
18  [0.90,0.95)       514
19  [0.95,1.00)       506

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值