MC102MN

Introdução a Algoritmos e programação de computadores

Sugestão para plot no terminal

def print_histo(bars, n0, nmax):
    s = [[" " for i in xrange(80)] for i in xrange(24)]
    for i in xrange(24):
        s[i][4] = "|"
    for j in xrange(80):
        s[2][j] = '-'
    for i,h in enumerate(bars):
        s[h+3][6+6*i] = s[h+3][6+6*i+1] = s[h+3][6+6*i+2] = s[h+3][6+6*i+3] = "-"
        for j in xrange(3,h+3):
            s[j][6+6*i] = s[j][6+6*i+3] = '|'
    s1 = str(n0)
    for i,c in enumerate(s1):
        s[1][6+i] = c
    smax = str(nmax)
    for i,c in enumerate(smax):
        s[1][72+i] = c
    s.reverse()
    for l in s:
        print ''.join(l)


def print_plot(points, x0, xmax, y0, ymax, a, b):
    s = [[" " for i in xrange(80)] for i in xrange(24)]
    for i in xrange(24):
        s[i][4] = "|"
    for j in xrange(80):
        s[2][j] = '-'
    s1 = str(x0)
    for i,c in enumerate(s1):
        s[1][6+i] = c
    smax = str(xmax)
    for i,c in enumerate(smax):
        s[1][72+i] = c
    s1 = str(y0)
    for i,c in enumerate(s1):
        s[3][i] = c
    sm = str(ymax)
    for i,c in enumerate(sm):
        s[22][i] = c
    for i,(x,y) in enumerate(points):
        px = int(77*(float(x)-x0)/(xmax-x0))
        py = int(21*(float(y)-y0)/(ymax-y0))
        print px,py
        s[min(len(s)-1,3+py)][min(len(s[0])-1,5+px)] = str(i)
    b = int(21*(float(b)-y0)/(ymax-y0))
    a = a*(ymax-y0)/(xmax-x0)
    for i in xrange(75):
        p = int(3+a*i+b)
        print 3+p,5+i
        if i != 0 and p < 0 or p > 21: break
        if a > 0:
            s[3+p][5+i] = "/"
        else:
            s[3+p][5+i] = "\\"
    s.reverse()
    for l in s:
        print ''.join(l)

Author: Alexandre Tachard Passos <alexandre.tp@gmail.com>

Date: 2010-11-11 10:21:28 BRST

HTML generated by org-mode 6.21b in emacs 23