简体   繁体   中英

Turn Weighted Numbers into Multiple Histograms

I am using the below code to create a weighted list of random numbers within a range.

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])

What I would like to do is (a) list these numbers randomly down the csv column, not sure why they are coming out pre-sorted, (b) list the numbers down the comumn instead of as one entry, and (c) create and save multiple histrograms at defined intervals, such as the first 100 numbers, then first 250 numbers, then first 500 numbers, ... to the end

For (c) I would like to create multiple pictures such as this for various cutoffs of the data list.

直方图示例

Attempt at histogram

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()

As for the third part of your question, take a look at matplotlib (and numpy.loadtxt() for reading your data). There are many examples to help you learn the basics, as well as advanced features. Here's a quick example of plotting a histogram of a random normal distribution:

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()

which gives you a nice image:

Matplotlib直方图示例

You can make loops to generate multiple images using different ranges of your data, and save the generated figures in a number of formats, with or without previewing them first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM