练习:Benford定律
# -*- coding:utf-8 -*-
# /usr/bin/python
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['axes.unicode_minus'] = False
mpl.rcParams['font.sans-serif'] = 'SimHei'
def top(number):
number -= int(number)
frequency[int(10 ** number) - 1] += 1
if __name__ == '__main__':
N = 100000
x = list(range(1, N+1))
frequency = np.zeros(9, dtype=np.int)
f = 1
y = np.cumsum(np.log10(x))
list(map(top, y))
plt.figure(facecolor='w')
t = np.arange(1, 10)
plt.plot(t, frequency, 'r-', t, frequency, 'go', lw=2, markersize=8)
for x,y in enumerate(frequency):
plt.text(x+1.1, y, frequency[x], verticalalignment='top', fontsize=13)
plt.title('%d!首位数字出现频率' % N, fontsize=18)
plt.xlim(0.5, 9.5)
plt.ylim(0, max(frequency)*1.03)
plt.grid(b=True)
plt.show()