Exemplo n.º 1
0
def compute_im(data,
               quantity,
               res=16,
               function=None,
               style='heat',
               cmap=grad_red):
    if function is None:
        function = lambda x: x if x is not None else 0
    gamemap = data['gamemap']
    im = np.zeros((gamemap.height * res, gamemap.width * res, 4))
    qmap = {}

    # print(quantity)
    # print(data.keys())
    quantity_map = data.get(
        quantity,
        {square: getattr(square, quantity, None)
         for square in gamemap})

    for square in gamemap:
        qmap[(square.y, square.x)] = float(function(quantity_map[square]))

    qmin, qmax = min(qmap.values()), max(qmap.values())

    for square in gamemap:
        x = square.x
        y = square.y
        if style == 'heat':
            im[y * res:y * res + res, x * res:x * res + res] = cmap(
                (qmap[(y, x)] - qmin) / (qmax - qmin))
        elif style == 'colors':
            im[y * res:y * res + res,
               x * res:x * res + res] = cmap(qmap[(y, x)])
        elif style == 'squares':
            qnormed = (qmap[(y, x)] - qmin) / (qmax - qmin)
            square_size = qnormed * (res - 2) + 1
            start = int((res - square_size) / 2)
            end = int((res - square_size) / 2 + square_size)
            im[y * res + start:y * res + end,
               x * res + start:x * res + end] = (1, 1, 1, 1)
        elif style == 'arrows':
            im[y * res:y * res + res,
               x * res:x * res + res] = arrows[int(qmap[(y, x)])]
    return im
Exemplo n.º 2
0
def plot_quantity(gameMap,
                  quantity,
                  res=16,
                  function=None,
                  style='heat',
                  alpha=1.,
                  cmap=grad_red):
    if function is None:
        function = lambda x: x
    im = np.zeros((gameMap.height * res, gameMap.width * res, 4))
    qmap = {}

    for y in range(gameMap.height):
        for x in range(gameMap.width):
            loc = Location(x, y)
            qmap[(y,
                  x)] = float(function(getattr(gameMap.getSite(loc),
                                               quantity)))

    qmin, qmax = min(qmap.values()), max(qmap.values())

    for y in range(gameMap.height):
        for x in range(gameMap.width):
            if style == 'heat':
                im[y * res:y * res + res, x * res:x * res + res] = cmap(
                    (qmap[(y, x)] - qmin) / qmax)
            elif style == 'colors':
                im[y * res:y * res + res,
                   x * res:x * res + res] = cmap(qmap[(y, x)])
            elif style == 'squares':
                qnormed = (qmap[(y, x)] - qmin) / (qmax - qmin)
                square_size = qnormed * (res - 2) + 1
                start = int((res - square_size) / 2)
                end = int((res - square_size) / 2 + square_size)
                im[y * res + start:y * res + end,
                   x * res + start:x * res + end] = (1, 1, 1, 1)
            elif style == 'arrows':
                im[y * res:y * res + res,
                   x * res:x * res + res] = arrows[int(qmap[(y, x)])]

    plt.imshow(im, alpha=alpha)
Exemplo n.º 3
0
def plotBar(x,
            y1,
            y2=None,
            title='',
            figSize=(8, 4),
            tightLayout=True,
            showMajorGridLines=True,
            showMinorGridLines=False,
            xLbl='xAxis ->',
            yLbl='yAxis ->',
            yStrFmt='%0.0f%%',
            xLogBase=None,
            tight_pad=2.0,
            savefig=False,
            figfilename='errplot.pdf',
            yPercentage=False,
            yLim=None,
            xTicks=None,
            xStrFmt=None):
    '''
        x => str list/array
        y => float list/array
        y2 => float list/array
    '''

    assert len(x) == len(y1)
    if y2 is not None: assert len(y1) == len(y2)

    fig = plt.figure(figsize=figSize)
    lw = 2  # line width

    x_vals = np.arange(len(x))

    cmap = cm.get_cmap('viridis')
    #sns.barplot(x=x,y=y1,hue=y2, ax=plt.gca())#palette=plt.cm.viridis

    ### plots
    #plt.errorbar(x=x_vals, y=means, xerr=None, yerr=stds, marker='o', ms=4, color='green', ls = 'dotted', capsize=6) #cmap=plt.cm.viridis
    w = 0.4
    plt.bar(x_vals - (w / 2), y1, width=w, align='center', color=cmap(0.4))

    if y2 is not None:
        plt.bar(x_vals + (w / 2), y2, width=w, align='center', color=cmap(0.7))

    plt.xticks(x_vals, x, rotation=45)
    plt.title(title, fontsize=16)
    plt.xlabel(xLbl, fontsize=12)
    plt.ylabel(yLbl, fontsize=12)

    ax = plt.gca()
    ax.autoscale(tight=True)

    plt.legend(['Train Accuracy', 'Test Accuracy'], loc=4)

    ymin = y1.min()
    if y2 is not None: ymin = min(ymin, y2.min())

    ymax = y1.max()
    if y2 is not None: ymax = max(ymax, y2.max())

    ax.set_ylim((ymin - 2, ymax + 1))
    ax.yaxis.set_major_formatter(FormatStrFormatter(yStrFmt))

    if showMajorGridLines and showMinorGridLines:
        ax.yaxis.grid(which='both', linestyle='--')
    elif showMajorGridLines:
        ax.yaxis.grid(which='major', linestyle='--')
    elif showMinorGridLines:
        ax.yaxis.grid(which='minor', linestyle='--')

    if tightLayout: plt.tight_layout(pad=tight_pad)

    if savefig:
        fig.tight_layout()
        #fname = os.path.normpath(os.path.join(figdir, figfilename))
        fig.savefig(figfilename,
                    format='pdf',
                    dpi=300,
                    transparent=True,
                    bbox_inches='tight',
                    pad_inches=0.01)

    plt.show()