import pylab
import random
import matplotlib
import matplotlib.pyplot as plt
def Step1():
# 柱状图:pylab.hist()
SAMPLE_SIZE = 10000 # 100的区间会看到数据有起伏波动,10000时会趋于平稳,因为10个数一组
random.seed() # 使用系统当前时间
# 值的区间为0~1的
real_rand_vars = [random.random() for val in range(SAMPLE_SIZE)] # 存储随机数数值
print('real_rand_vars=', real_rand_vars)
pylab.hist(real_rand_vars, 10) # 100个随机数放入其中,10个随机数为一组
pylab.xlabel('Number range')
pylab.ylabel('Count')
pylab.show()
return
def Step2():
# 柱状图:pylab.hist()
# 数值的变化转为[1 ~ 6]的, 这里使用random.randint()
min = 1
max = 6
SAMPLE_SIZE = 10000 # 100的区间会看到数据有起伏波动,10000时会趋于平稳,因为10个数一组
random.seed() # 使用系统当前时间
# 值的区间为0~1的
real_rand_vars = [random.randint(min, max) for val in range(SAMPLE_SIZE)] # 存储随机数数值
print('real_rand_vars=', real_rand_vars)
pylab.hist(real_rand_vars, 10) # 100个随机数放入其中,10个随机数为一组
pylab.xlabel('Number range')
pylab.ylabel('Count')
pylab.show()
return
def Step3(): # 画出来是个折线图
duration = 100 # 100个数据点为虚拟天数的序列
mean_inc = 0.2 # 中值(中位数)
std_dev_inc = 1.2 # 标准差
x = range(duration)
y = []
price_today = 0
for i in x:
# 从中值为mean_inc、标准差为std_dev_inc的正太分布中选取一个随机值,然后加上前一天的价格作为当天的价格
next_delta = random.normalvariate(mean_inc, std_dev_inc)
price_today = price_today + next_delta
y.append(price_today)
pylab.plot(x, y) # 把每个点传入,画出来的就是每个点的连线,这里是个走势图
pylab.xlabel('Time')
pylab.ylabel('Value')
pylab.show()
return
def Step3():
# plt.hist 直方图,下面是几种分布的直方图,每个样本数量为1000
# random.seed(),使用系统当前时间,seed()来初始化随机数生成器
# 如果要避免生成的序列重复,推荐使用random.SystemRandom,因为它底层使用的是os.urandom,os.urandom提供了
# 对更多熵源的访问,而seed()和setstate()没有影响
SAMPLE_SIZE = 1000
random.seed()
buckets = 100
plt.figure() # 生成一个图表
matplotlib.rcParams.update({'font.size' : 7})
plt.subplot(621) # 定义一个6x2的子图,0~1之间分布的随机变量
plt.xlabel('random.random')
res = [random.random() for _ in range(1, SAMPLE_SIZE)] # 1000个随机数组成的样本集
plt.hist(res, buckets)
plt.subplot(622) # 定义一个6x2的子图,均匀分布的随机变量
plt.xlabel('random.uniform')
a = 1
b = SAMPLE_SIZE
res = [random.uniform(a, b) for _ in range(1, SAMPLE_SIZE)] # random.uniform(x, y) 方法将随机生成一个实数,它在 [x,y] 范围内
plt.hist(res, buckets)
plt.subplot(623) # 定义一个6x2的子图,三角形分布
plt.xlabel('random.triangular')
low = 1
high = SAMPLE_SIZE
res = [random.triangular(low=low, high=high) for _ in range(1, SAMPLE_SIZE)]
plt.hist(res, buckets)
plt.subplot(624) # 定义一个6x2的子图,beta分布
plt.xlabel('random.betavariate')
alpha = 1 # 这里alpha与beta都大于0
beta = 10
res = [random.betavariate(alpha=alpha, beta=beta) for _ in range(1, SAMPLE_SIZE)]
plt.hist(res, buckets)
plt.subplot(625) # 定义一个6x2的子图,指数分布
plt.xlabel('random.expovariate')
lambd = 1 / ((SAMPLE_SIZE + 1) / 2)
res = [random.expovariate(lambd=lambd) for _ in range(1, SAMPLE_SIZE)]
plt.hist(res, buckets)
plt.subplot(626) # gamma分布
plt.xlabel('random.gammavariate')
alpha = 1
beta = 10
res = [random.gammavariate(alpha=alpha, beta=beta) for _ in range(1, SAMPLE_SIZE)]
plt.hist(res, buckets)
plt.subplot(627) # 对数正态分布
plt.xlabel('random.lognormvariate')
mu = 1
sigma = 0.5
res = [random.lognormvariate(mu=mu, sigma=sigma) for _ in range(1, SAMPLE_SIZE)]
plt.hist(res, buckets)
plt.subplot(628)
plt.xlabel('random.normalvariate')
mu = 1
sigma = 0.5
res = [random.normalvariate(mu=mu, sigma=sigma) for _ in range(1, SAMPLE_SIZE)]
plt.hist(res, buckets)
plt.subplot(629) # 帕累托分布
plt.xlabel('random.paretovariate')
alpha = 1
res = [random.paretovariate(alpha=alpha) for _ in range(1, SAMPLE_SIZE)]
plt.hist(res, buckets)
plt.tight_layout()
plt.show()
return
def WorkSpace():
Step1() # 简单的柱状图
Step2() # 10000个点,画出的是折线走势图
Step3() # 各种分布下的柱状图
return
WorkSpace()
下面依次为step1、step2、step3的可视化图