import math


def entropy(data):
    """
    Calculate entropy, used by entropy_graph()
    """
    h = 0
    bins = [0 for x in range(0, 256)]
    length = len(data)

    for v in data:
        bins[ord(v)] += 1

    for x in range(0, 256):
        p_x = float(bins[x]) / length
        if p_x > 0:
            h += - p_x * math.log(p_x, 2)

    return h