Skip to content
chart.py 2.53 KiB
Newer Older
Franziska Koehn's avatar
Franziska Koehn committed
"""

:Author: Franziska Koehn
:Created: 2015/01/13

Franziska Koehn's avatar
Franziska Koehn committed
This module houses functions relating to the chart-creation.
Franziska Koehn's avatar
Franziska Koehn committed

"""

def count_substrings(substrings, strings):
Franziska Koehn's avatar
Franziska Koehn committed
    """
Franziska Koehn's avatar
Franziska Koehn committed
    Returns a dictionary including the counts of its keys.
    This keys are the given list of substrings.
Franziska Koehn's avatar
Franziska Koehn committed

    **Parameters**
Franziska Koehn's avatar
Franziska Koehn committed
        :substrings: list of str, list of substrings to be counted
        :strings:    list of str, strings in which the counts of substrings will be determined
Franziska Koehn's avatar
Franziska Koehn committed
    """
    from collections import defaultdict
Franziska Koehn's avatar
Franziska Koehn committed
    from itertools import product
Franziska Koehn's avatar
Franziska Koehn committed
    subs = list(set(substrings))

Franziska Koehn's avatar
Franziska Koehn committed
    results = defaultdict(int)
Franziska Koehn's avatar
Franziska Koehn committed
    for sub,string in product(subs, strings):
        if sub == '':
            continue
Franziska Koehn's avatar
Franziska Koehn committed
        results[sub] # create key
        if sub.lower() in string.lower():
            results[sub] += 1
    return list(results.items())
Franziska Koehn's avatar
Franziska Koehn committed

Franziska Koehn's avatar
Franziska Koehn committed
def create(data, y_max, x_max, fig):
Franziska Koehn's avatar
Franziska Koehn committed
    """
Franziska Koehn's avatar
Franziska Koehn committed
    creates diagram for given 'data' and max-values ('y_max', 'x_max') and
    renders it as an image, saved as 'file' in given 'size'
Franziska Koehn's avatar
Franziska Koehn committed

    **Parameters**
        :data: dictionary, the key will be shown on the x-axis. the value on the y-axis. if a two-dimensional list will be pass, the values on the first dimension will drawn on the x-axis, the second on the y-axis
        :y_max: max-value for y-axis
        :x_max: max-value for x-axis (= count of keys)
    """
Franziska Koehn's avatar
Franziska Koehn committed

    from matplotlib.figure import Figure
Franziska Koehn's avatar
Franziska Koehn committed

Franziska Koehn's avatar
Franziska Koehn committed
    GNOME_BLUE = '#3A81CC'  # the proper gtk-widgets-colour
    DPI = float(90)         # dots per inch
    BAR_WIDTH = 0.8         # width of bars
    BAR_START = 0.1         # start of bars
Franziska Koehn's avatar
Franziska Koehn committed

    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
    if fig is None:
        fig=Figure()
    fig.clear()

    ax = fig.add_subplot(111)   #111: create a 1 x 1 grid, put the subplot in the 1st cell
Franziska Koehn's avatar
Franziska Koehn committed
    if y_max != 0:
Franziska Koehn's avatar
Franziska Koehn committed
        ax.set_xlim(0, x_max)
        ax.set_ylim(0, y_max)
Franziska Koehn's avatar
Franziska Koehn committed
    else:
Franziska Koehn's avatar
Franziska Koehn committed
        ax.get_yaxis().set_visible(False)
Franziska Koehn's avatar
Franziska Koehn committed
    bars = ax.bar(left=x_pos, height=values, width=BAR_WIDTH, color=GNOME_BLUE)

    ax.get_xaxis().set_ticks(label_pos)
    ax.get_xaxis().set_ticklabels(labels)
    ax.get_yaxis().set_label_text('Count')
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
    ax.plot()
    return fig