def test_mpl_ax_with_ax():
    fig, ax = pyplot.subplots()
    fig1, ax1 = validate.mpl_ax(ax)
    assert isinstance(ax1, pyplot.Axes)
    assert isinstance(fig1, pyplot.Figure)
    assert ax1 is ax
    assert fig1 is fig
def _plot_boundaries(extent_x=None, extent_y=None, extent=None, islands_x=None,
                     islands_y=None, islands_name=None, islands=None, ax=None):
    """
    """

    # setup the figure
    fig, ax = validate.mpl_ax(ax)

    if extent is not None:
        extent_x, extent_y = extent[extent_x], extent[extent_y]

    if extent_x is not None:
        ax.plot(extent_x, extent_y, 'k-', label='extent')

    if islands is not None:
        islands_x, islands_y = islands[islands_x], islands[islands_y]

        if islands_name is not None:
            islands_name = islands[islands_name]

    if islands_x is not None and islands_y is not None:
        for name in np.unique(islands_name):
            subset = islands_name == name
            coords = list(zip(islands_x[subset], islands_y[subset]))
            patch = pyplot.Polygon(coords, facecolor='0.25')
            ax.add_patch(patch)

    ax.margins(0.1, 0.1)
    return fig
def _plot_cells(x, y, mask=None, ax=None, **plot_opts):
    fig, ax = validate.mpl_ax(ax)

    ec = plot_opts.pop('edgecolor', None) or plot_opts.pop('ec', '0.125')
    fc = plot_opts.pop('facecolor', None) or plot_opts.pop('fc', '0.875')
    lw = plot_opts.pop('linewidth', None) or plot_opts.pop('lw', 0.75)

    rows, cols = x.shape
    if mask is None:
        if hasattr(x, 'mask'):
            mask = x.mask
        else:
            mask = np.zeros(x.shape)

    for jj in range(rows - 1):
        for ii in range(cols - 1):
            if mask[jj, ii]:
                coords = None

            else:
                coords = misc.make_poly_coords(
                    x[jj:jj + 2, ii:ii + 2],
                    y[jj:jj + 2, ii:ii + 2],
                )

            if coords is not None:
                rect = pyplot.Polygon(coords, edgecolor=ec, facecolor=fc,
                                      linewidth=lw, **plot_opts)
                ax.add_patch(rect)

    ax.margins(0.1, 0.1)

    return fig
    def test_without_ax(self):
        fig, ax = validate.mpl_ax(None)
        nt.assert_not_equals(self.fig, fig)
        nt.assert_not_equals(self.ax, ax)

        nt.assert_true(isinstance(fig, plt.Figure))
        nt.assert_true(isinstance(ax, plt.Axes))
Exemple #5
0
def _plot_points(x, y, ax=None, **plot_opts):
    """

    """
    fig, ax = validate.mpl_ax(ax)
    dots, = ax.plot(x.flatten(), y.flatten(), 'ko', **plot_opts)
    ax.margins(0.1)
    return fig, {'points': dots}
def _plot_points(x, y, ax=None, **plot_opts):
    """

    """

    fig, ax = validate.mpl_ax(ax)
    ax.plot(x, y, 'ko', **plot_opts)
    ax.margins(0.1, 0.1)
    return fig
def _plot_domain(domain_x=None, domain_y=None, beta=None, data=None, ax=None):
    """ Plot a model grid's domain.

    Parameters
    ----------
    x, y, beta : str or array-like, optional
        Either column labels or sequences representing the x- and
        y-coordinates and beta values of each point defining the domain
        of the model grid.
    data : pandas.DataFrame, optional
        If ``x``, ``y``, and ``beta`` are strings (column labels),
        ``data`` must be a pandas.DataFrame containing those labels.
    ax : matplotib.Axes, optional
        The Axes on which the domain will be drawn. If omitted, a new
        one will be created.

    """
    # setup the figure
    fig, ax = validate.mpl_ax(ax)

    # coerce values into a dataframe if necessary
    if data is not None:
        domain_x, domain_y = data[domain_x], data[domain_y]
        if beta is not None:
            beta = data[beta]

    # plot the boundary as a line
    ax.plot(domain_x, domain_y, 'k-', label='__nolegend__')

    if beta is not None:
        # plot negative turns
        beta_neg1 = beta == -1
        ax.plot(domain_x[beta_neg1], domain_y[beta_neg1], 's',
                linestyle='none', label="negative")

        # plot non-turns
        beta_zero = beta == 0
        ax.plot(domain_x[beta_zero], domain_y[beta_zero], 'o',
                linestyle='none', label="neutral")

        # plot positive turns
        beta_pos1 = beta == 1
        ax.plot(domain_x[beta_pos1], domain_y[beta_pos1], '^',
                linestyle='none', label="positive")

        ax.legend()

    ax.margins(0.1, 0.1)
    return fig
Exemple #8
0
def _plot_domain(domain, betacol='beta', ax=None, show_legend=True):
    """ Plot a model grid's domain.

    Parameters
    ----------
    domain : GeoDataFrame
        Defines the boundary of the model area. Needs to have a column of
        Point geometries and a a column of beta (turning point) values.
    betacol : str
        Label of the column in *domain* that contains the beta (i.e., turning
        point) values of domain. This sum of this column must be 4.
    ax : matplotib.Axes, optional
        The Axes on which the domain will be drawn. If omitted, a new
        one will be created.
    show_legend : bool, default = True

    """
    leg = None

    # setup the figure
    fig, ax = validate.mpl_ax(ax)

    # plot the boundary as a line
    line_artist, = ax.plot(domain.geometry.x,
                           domain.geometry.y,
                           'k-',
                           label='__nolegend__')

    beta_artists = []
    beta_selectors = [
        domain[betacol] < 0, domain[betacol] == 0, domain[betacol] > 0
    ]
    beta_markers = ['s', 'o', '^']
    beta_labels = ['negative', 'neutral', 'positive']
    beta_artists = [
        ax.plot(domain.geometry.x[idx],
                domain.geometry.y[idx],
                marker,
                ls='none',
                label=label)[0] for idx, marker, label in zip(
                    beta_selectors, beta_markers, beta_labels)
    ]
    if show_legend:
        leg = ax.legend()

    ax.autoscale()
    ax.margins(0.1)
    return fig, {'domain': line_artist, 'beta': beta_artists, 'legend': leg}
Exemple #9
0
def _plot_domain(domain_x=None, domain_y=None, beta=None, data=None, ax=None):
    """ Plot a model grid's domain.

    Parameters
    ----------
    x, y, beta : str or array-like, optional
        Either column labels or sequences representing the x- and
        y-coordinates and beta values of each point defining the domain
        of the model grid.
    data : pandas.DataFrame, optional
        If ``x``, ``y``, and ``beta`` are strings (column labels),
        ``data`` must be a pandas.DataFrame containing those labels.
    ax : matplotib.Axes, optional
        The Axes on which the domain will be drawn. If omitted, a new
        one will be created.

    """
    # setup the figure
    fig, ax = validate.mpl_ax(ax)

    # coerce values into a dataframe if necessary
    if data is not None:
        domain_x, domain_y = data[domain_x], data[domain_y]
        if beta is not None:
            beta = data[beta]

    # plot the boundary as a line
    line_artist, = ax.plot(domain_x, domain_y, 'k-', label='__nolegend__')

    beta_artists = []
    if beta is not None:
        beta_selectors = [beta < 0, beta == 0, beta > 0]
        beta_markers = ['s', 'o', '^']
        beta_labels = ['negative', 'neutral', 'positive']
        beta_artists = [
            ax.plot(domain_x[idx],
                    domain_y[idx],
                    marker,
                    ls='none',
                    label=label)[0] for idx, marker, label in zip(
                        beta_selectors, beta_markers, beta_labels)
        ]

        ax.legend()

    ax.autoscale()
    ax.margins(0.1)
    return fig, {'domain': line_artist, 'beta': beta_artists}
Exemple #10
0
def _plot_boundaries(extent_x=None,
                     extent_y=None,
                     extent=None,
                     islands_x=None,
                     islands_y=None,
                     islands_name=None,
                     islands=None,
                     ax=None):
    """
    """

    # setup the figure
    fig, ax = validate.mpl_ax(ax)

    if extent is not None:
        extent_x, extent_y = extent[extent_x], extent[extent_y]

    if extent_x is not None:
        extent, = ax.plot(extent_x, extent_y, 'k-', label='extent')

    if islands is not None:
        islands_x, islands_y = islands[islands_x], islands[islands_y]

        if islands_name is not None:
            islands_name = islands[islands_name]

    polys = []
    if islands_x is not None and islands_y is not None:
        for name in numpy.unique(islands_name):
            _subset = islands_name == name
            _coords = list(zip(islands_x[_subset], islands_y[_subset]))
            _island = patches.Polygon(_coords, facecolor='0.25')
            polys.append(_island)
            ax.add_patch(_island)

    ax.margins(0.1)
    return fig, {'extent': extent, 'islands': polys}
Exemple #11
0
def _plot_cells(x,
                y,
                mask=None,
                colors=None,
                ax=None,
                sticky_edges=False,
                **plot_opts):
    fig, ax = validate.mpl_ax(ax)
    ax.use_sticky_edges = sticky_edges

    if mask is None:
        mask = numpy.zeros(x.shape)[1:, 1:]

    if colors is None:
        colors = numpy.ma.masked_array(mask + 1, mask)
        vmin, vmax = 0, 2
    else:
        vmin = plot_opts.pop('vmin', None)
        vmax = plot_opts.pop('vmax', None)

    cell_colors = numpy.ma.masked_array(data=colors, mask=mask)

    ec = plot_opts.pop('edgecolor', None) or plot_opts.pop('ec', '0.125')
    lw = plot_opts.pop('linewidth', None) or plot_opts.pop('lw', 0.75)
    fc = plot_opts.pop('facecolor', None) or plot_opts.pop('fc', '0.875')
    cmap = plot_opts.pop('cmap', 'Greys')

    cells = ax.pcolor(x,
                      y,
                      cell_colors,
                      edgecolors=ec,
                      lw=lw,
                      vmin=vmin,
                      vmax=vmax,
                      cmap=cmap,
                      **plot_opts)
    return fig, {'cells': cells}
def test_mpl_ax_with_None():
    fig1, ax1 = validate.mpl_ax(None)
    assert isinstance(ax1, pyplot.Axes)
    assert isinstance(fig1, pyplot.Figure)
def test_mpl_ax_invalid():
    with utils.raises(ValueError):
        validate.mpl_ax('junk')
 def test_bad_ax(self):
     validate.mpl_ax('junk')
 def test_with_ax(self):
     fig, ax = validate.mpl_ax(self.ax)
     nt.assert_equal(self.fig, fig)
     nt.assert_equal(self.ax, ax)