[Matplotlib] Ploting an exponential distribution frequency curve

Mario Figueiredo marfig at gmail.com
Sun Apr 26 01:15:44 CEST 2015

Ok. Ermm, it seems I needed to ask to finally have an epiphany. The
problem is that defaultdict is unordered. Once I get the data ordered,
I can finally plot the curve. Although this presents another

import decimal
from random import expovariate
from collections import defaultdict

decimal.getcontext().prec = 4
Dec = decimal.Decimal

samples = 100000   # 100,000

def generate(lambd):
    res = defaultdict(int)
    for _ in range(samples):
        res[Dec(expovariate(lambd)).quantize(Dec('0.01'))] += 1
    return sorted(res.items())

results = generate(1)
x, y = zip(*results)
plot(x, y)

This works as intended. But plots a jagged curve due to the small
discrepancies normal of a random number generation.

Other than replacing the random module with the probability density
function for the exponential distribution, do you have a suggestion of
how I could smooth the curve?

More information about the Python-list mailing list