Esempio n. 1
0
    def test_RelativeArrowPatch(self):

        fig, ax = new_fig(aspect=1, scale=3)
        ax.set_xlim(0, 1)
        ax.set_ylim(0, 1)
        arrow1 = RelativeFancyArrow(0.5,
                                    0.5,
                                    0.0,
                                    0.5,
                                    width=1,
                                    head_length=0.5,
                                    head_width=2)
        arrow2 = RelativeFancyArrow(0.5,
                                    0.5,
                                    0.5,
                                    0.0,
                                    width=1,
                                    head_length=0.5,
                                    head_width=2)
        ax.add_patch(arrow1)
        ax.add_patch(arrow2)

        ax.plot(0.5,
                0.5,
                marker='o',
                markersize=size_units2points(size=0.5, ax=ax),
                alpha=0.5,
                zorder=100)
        self.assertTrue(True)
Esempio n. 2
0
def hist_vlines(x,
                name,
                bins=100,
                hl_idx=None,
                hl_color=None,
                hl_name=None,
                lower_perc=None,
                upper_perc=None):

    if lower_perc is not None:
        _range = (np.percentile(x, lower_perc), np.percentile(x, upper_perc))
    else:
        _range = None
    fig, ax = new_fig(title=f'Histogram: {name}', scale=2)
    hist = ax.hist(x, bins=bins, range=_range)

    perc_i = []
    if hl_idx is not None:
        hl_idx, hl_color, hl_name = safe_scalar2array(hl_idx,
                                                      hl_color,
                                                      hl_name,
                                                      shape=np.size(hl_idx))
        for i, c, n in zip(hl_idx, hl_color, hl_name):
            perc_i.append(np.sum(x[i] > x))
            label = None if n is None else f"{n} | {perc_i[-1]} / {len(x)}"
            ax.vlines(x[i], ymin=0, ymax=len(x), lw=4, color=c, label=label)

    ax.set_ylim(0, hist[0].max() * 1.02)

    if lower_perc is not None:
        ax.set_xlim(np.percentile(x, lower_perc), np.percentile(x, upper_perc))

    if hl_name is not None:
        ax.legend()
    return ax, perc_i
Esempio n. 3
0
    def test_add_ticks(self):
        fig, ax = new_fig()

        set_ticks_position(ax=ax, position='all')
        set_labels_position(ax=ax, position='all')
        plt.pause(0.1)

        add_ticks(ax=ax, ticks=0.55, labels='wow', axis='x')
        add_ticks(ax=ax, ticks=(0.3, 0.5), labels=('wtf', 'ftw'), axis='y')
Esempio n. 4
0
    def test_AbsoluteFancyBboxPatch(self):
        from wzk.mpl import new_fig, patches

        fig, ax = new_fig(aspect=1)
        ax.add_patch(
            FancyBbox(xy=(0.1, 0.1),
                      boxstyle='Round4',
                      height=0.5,
                      width=0.5,
                      pad=0.1,
                      corner_size=0))
Esempio n. 5
0
    def test_set_ticks_position(self):
        fig, ax = new_fig()
        positions = [
            'left', 'top', 'right', 'bottom', 'all', 'none', 'default',
            'inverse', ('bottom', 'right'), ('top', 'left'), ('bottom', 'top'),
            ('left', 'right'), ('left', 'bottom', 'right'),
            ('top', 'bottom', 'right'), ('top', 'left', 'right'),
            ('top', 'left', 'bottom')
        ]

        for p in positions:
            set_ticks_position(ax=ax, position=p)
            set_labels_position(ax=ax, position=p)
            ax.cla()
            ax.text(0.5, 0.5, ''.join(p))
            plt.pause(0.1)
        self.assertTrue(True)
Esempio n. 6
0
    def test_change_tick_appearance(self):

        fig, ax = new_fig()
        set_ticks_position(ax=ax, position='all')
        set_labels_position(ax=ax, position='all')

        plt.pause(0.05)
        change_tick_appearance(ax,
                               position='bottom',
                               v=3,
                               size=40,
                               color='red')
        change_tick_appearance(ax,
                               position='top',
                               v=0.4,
                               size=30,
                               color='blue')

        plt.pause(0.05)
        change_tick_appearance(ax,
                               position='left',
                               v=2,
                               size=40,
                               color='green')
        change_tick_appearance(ax,
                               position='right',
                               v=0.6,
                               size=30,
                               color='magenta')
        ax.text(0.5,
                0.5,
                'Each Axis should have one larger tick in different colors',
                ha='center',
                weight='bold')
        ax.annotate('bottom', (0.6, 0), (0.6, 0.4),
                    arrowprops=dict(arrowstyle='->'))
        ax.annotate('top', (0.4, 1), (0.4, 0.6),
                    arrowprops=dict(arrowstyle='->'))
        ax.annotate('left', (0, 0.4), (0.4, 0.4),
                    arrowprops=dict(arrowstyle='->'))
        ax.annotate('right', (1, 0.6), (0.6, 0.6),
                    arrowprops=dict(arrowstyle='->'))
        self.assertTrue(True)
Esempio n. 7
0
def correlation_plot(a,
                     b,
                     name_a,
                     name_b,
                     regression_line=True,
                     lower_perc=0,
                     upper_perc=100,
                     labels=None,
                     colors=None,
                     markers='o',
                     markersizes=None,
                     alphas=None,
                     zorders=None,
                     ax=None,
                     verbose=1,
                     **kwargs):

    if ax is None:
        fig, ax = new_fig(width=10, title=f"Correlation: {name_a} | {name_b}")

    a, b = atleast_tuple(a, b, convert=False)

    a_all = np.concatenate(a)
    b_all = np.concatenate(b)

    limits = ((np.percentile(a_all,
                             lower_perc), np.percentile(a_all, upper_perc)),
              (np.percentile(b_all,
                             lower_perc), np.percentile(b_all, upper_perc)))

    limits_1 = add_safety_limits(limits=limits, factor=0.01)
    limits_2 = add_safety_limits(limits=limits, factor=0.02)

    if regression_line:
        s, i, r, p, _ = linregress(a_all, b_all)
        x_reg = np.linspace(limits_1[0, 0], limits_1[0, 1], 3)
        y_reg = x_reg * s + i
        a += (x_reg, )
        b += (y_reg, )
        if verbose > 0:
            print("slope: {:.4} | correlation: {:.4} | p {:.4}".format(
                s, r, p))
    labels, colors, markers, markersizes, alphas, zorders = \
        safe_scalar2array(labels, colors, markers, markersizes, alphas, zorders, shape=len(a))

    for i, (aa, bb, l, c, m, ms, al, z) in enumerate(
            zip(a, b, labels, colors, markers, markersizes, alphas, zorders)):

        if regression_line and i + 1 == len(a):
            l = None if l is None else l.format(r)
            ax.plot(aa, bb, color=c, label=l, ls=m, lw=ms, alpha=al, zorder=z)
        else:
            ax.plot(aa,
                    bb,
                    ls='',
                    marker=m,
                    color=c,
                    label=l,
                    markersize=ms,
                    alpha=al,
                    zorder=z,
                    **kwargs)

    set_ax_limits(ax=ax, limits=limits_1)

    if labels is not None:
        ax.legend()

    ax.set_xlabel(name_a)
    ax.set_ylabel(name_b)

    return ax
Esempio n. 8
0
    def test_remove_ticks(self):
        fig, ax = new_fig()

        plt.pause(0.1)
        remove_ticks(ax=ax, v=[0, 0.4], axis='both')
        self.assertTrue(True)