Example #1
0
def graph(name, p, Width=110, Height=50, BarWidthPx=2, BarSpacePx=1):
    try:
        XScale = BarWidthPx + BarSpacePx
        # p is [(year,fcnt)]
        Width *= XScale
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, Width, Height)
        cr = cairo.Context(surface)
        cr.set_source_rgba(0,0,0,1)
        cr.select_font_face('Verdana')
        cr.set_font_size(8.0)
        cnts = [cnt for year,cnt in p]
        mi = stats.meani(cnts)
        mval = cnts[mi]
        sd = stats.stddev(cnts)
        (mloi,mhii),pct = stats.range_size(cnts, 30)
        # graph individual years
        for i,(year,fcnt) in enumerate(p):
            cr.set_source_rgba(0,0,0,.3)
            x = (year - 1900) * XScale
            h = max(1, fcnt / 1000)
            if i >= mloi and i <= mhii:
                cr.set_source_rgba(0,0,0,1)
            if i == mi:
                cr.set_source_rgba(1,0,0,1)

                # XXX: not sure about the indentation of these...
                w, = cr.text_extents(str(year))[2:3]
                cr.move_to(max(0, x-w/2.), Height)
                cr.show_text(str(year))

                cr.stroke()
            cr.rectangle(x, Height-h-7, BarWidthPx, h)
            cr.fill()
            cr.stroke()
        # graph 80%
        cr.set_source_rgba(1,0,0,0.6)
        mloi,mhii = stats.range_pct(cnts, 0.80)
        pctrng = mhii - mloi
        cr.rectangle(mloi * XScale, Height-7, pctrng * XScale, 1)
        cr.fill()
        cr.stroke()
        # write file
        surface.write_to_png('name-dob-chart/' + name + '.png')
        surface.finish()
        return (pct, pctrng)
    except:
        sys.stderr.write("Unexpected error:%s\n" % (sys.exc_info()[0]))
        raise
    return (0,0)
Example #2
0
def birthspan_pct(conn, name, pct=80, cc='US'):
    birthcnt = name_birth_totals(conn, name, cc)
    cnts = [cnt for year, cnt in birthcnt]
    ylo, yhi = stats.range_pct(cnts, pct / 100.)
    return (ylo + 1900, yhi + 1900)
Example #3
0
def birthspan_pct(conn, name, pct=80, cc='US'):
    birthcnt = name_birth_totals(conn, name, cc)
    cnts = [cnt for year,cnt in birthcnt]
    ylo,yhi = stats.range_pct(cnts, pct / 100.)
    return (ylo+1900, yhi+1900)