Skip to content
chart.py 1.46 KiB
Newer Older
def count_substrings(substrings, strings):
    from collections import defaultdict
    results = defaultdict(int)

    if strings == []:
        return []

    for sub in substrings:
        results[sub] = 0
        for string in strings:
            if sub.lower() in string.lower():
                results[sub] += 1
    return list(results.items())
def create(file, data, size, y_max, x_max):
Franziska Koehn's avatar
Franziska Koehn committed
    import matplotlib.pyplot as plt

    GNOME_BLUE = '#3A81CC'
    DPI = float(90)
Franziska Koehn's avatar
Franziska Koehn committed
    BAR_WIDTH = 0.8 # width of bars
Franziska Koehn's avatar
Franziska Koehn committed
    BAR_START = 0.1 # start of bars

    w, h = size
    labels = []
    values = []
    for lab, val in data:
        labels.append(lab)
        values.append(val)

Franziska Koehn's avatar
Franziska Koehn committed
    label_pos = []
    x_pos = []
    for x in range(len(labels)):
        label_pos.append(x+BAR_START+BAR_WIDTH/2)
        x_pos.append(x+BAR_START)

Franziska Koehn's avatar
Franziska Koehn committed
    _, ax = plt.subplots()

Franziska Koehn's avatar
Franziska Koehn committed
    if y_max != 0:
        plt.ylim((0,y_max))
        plt.xlim((0,x_max))
    else:
        plt.gca().axes.get_yaxis().set_visible(False)

Franziska Koehn's avatar
Franziska Koehn committed
    bars = plt.bar(left=x_pos, height=values, width=BAR_WIDTH, color=GNOME_BLUE)
Franziska Koehn's avatar
Franziska Koehn committed
    plt.xticks(label_pos, labels)
    plt.ylabel('Count')
Franziska Koehn's avatar
Franziska Koehn committed
#    plt.title('Counts of Substrings')
Franziska Koehn's avatar
Franziska Koehn committed
    plt.gcf().set_size_inches(w/DPI,h/DPI)

Franziska Koehn's avatar
Franziska Koehn committed
    for b in bars:
        height = b.get_height()
        ax.text(b.get_x()+b.get_width()/2., height, '%d'%int(height),
                ha='center', va='bottom', color='black')


Franziska Koehn's avatar
Franziska Koehn committed
    plt.savefig(file, dpi=DPI,transparent=False, pad_inches=0, frameon=None)
Franziska Koehn's avatar
Franziska Koehn committed
    plt.close()