我使用以下代码创建范围内随机数的加权列表。
import csv
import random
import numpy as np
import matplotlib.pyplot as plt
itemsList = []
rnd_numbs = csv.writer(open("rnd_numbs.csv", "wb"))
rnd_numbs.writerow(['number'])
items = [1, 2, 3, 4, 5]
probabilities= [0.1, 0.1, 0.2, 0.2, 0.4]
prob = sum(probabilities)
print prob
c = (1.0)/prob
probabilities = map(lambda x: c*x, probabilities)
print probabilities
ml = max(probabilities, key=lambda x: len(str(x)) - str(x).find('.'))
ml = len(str(ml)) - str(ml).find('.') -1
amounts = [ int(x*(10**ml)) for x in probabilities]
itemsList = list()
for i in range(0, len(items)):
itemsList += items[i:i+1]*amounts[i]
for item in itemsList:
rnd_numbs.writerow([item])
我想要做的是(a)在csv列中随机列出这些数字,不确定它们为什么要预先排序,(b)列出总数而不是作为一个条目,和( c)按照定义的时间间隔创建和保存多个组织图,例如前100个数字,然后是前250个数字,然后是前500个数字,......到最后
对于(c)我想为数据列表的各种截止值创建多个这样的图片。
尝试直方图
x = itemsList[0:20]
fig = plt.figure()
ax = fig.add_subplot(111)
# 100 is the number of bins
ax.hist(x, 10, normed=1, facecolor='green', alpha=0.75)
ax.set_xlim(0, 5)
ax.set_ylim(0, 500)
ax.grid(True)
plt.show()
答案 0 :(得分:0)
至于问题的第三部分,请查看matplotlib
(和numpy.loadtxt()
来阅读您的数据)。有许多examples可以帮助您学习基础知识以及高级功能。这是绘制随机正态分布直方图的快速示例:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(10000)
fig = plt.figure()
ax = fig.add_subplot(111)
# 100 is the number of bins
n = ax.hist(x, 100, facecolor='green', alpha=0.75)
# n[0] is the array of bin heights,
# n[1] is the array of bin edges
xmin = min(n[1]) * 1.1
xmax = max(n[1]) * 1.1
ymax = max(n[0]) * 1.1
ax.set_xlim(xmin, xmax)
ax.set_ylim(0, ymax)
ax.grid(True)
plt.show()
给你一个很好的形象:
您可以使用不同的数据范围制作循环以生成多个图像,并以多种格式保存生成的图形,无论是否先预览它们。