示例#1
0
    def __overlay_curves(axis, curve_stack):

        plot_line_family(axis, x_vec, curve_stack, label_prefix='Cluster', cmap=cmap)

        if legend_mode == 0:
            axis.legend(loc='best', fontsize=14)
        elif legend_mode == 1:
            axis.legend(loc='upper left', bbox_to_anchor=(1, 1), fontsize=14)
        else:
            sm = make_scalar_mappable(0, num_clusters - 1, cmap=discrete_cmap(num_clusters, cmap))
            plt.colorbar(sm)
示例#2
0
    def __overlay_curves(axis, curve_stack):

        plot_line_family(axis,
                         x_vec,
                         curve_stack,
                         label_prefix='Cluster',
                         cmap=cmap)

        if legend_mode == 0:
            axis.legend(loc='best', fontsize=14)
        elif legend_mode == 1:
            axis.legend(loc='upper left', bbox_to_anchor=(1, 1), fontsize=14)
        else:
            sm = make_scalar_mappable(0,
                                      num_clusters - 1,
                                      cmap=discrete_cmap(num_clusters, cmap))
            plt.colorbar(sm)
示例#3
0
def plot_cluster_labels(labels_mat, num_clusters=None, x_label=None, y_label=None, title=None, axis=None, **kwargs):
    """
    Plots the cluster labels

    Parameters
    ----------
    labels_mat : numpy.ndarray
        1D or 2D unsigned integer array containing the labels of the clusters
    num_clusters : int, optional
        Number of clusters
    x_label : str, optional
        Label for x axis
    y_label : str, optional
        Label for y axis
    title : str, optional
        Title for the plot
    axis : matplotlib.axes.Axes object, optional.
        Axis to plot this image onto. Will create a new figure by default or will use this axis object to plot into
    kwargs : dict
        will be passed on to plot() or plot_map()

    Returns
    -------
    fig : matplotlib.pyplot.Figure object
        figure object
    axis : matplotlib.Axes object
        axis object
    """
    if not isinstance(labels_mat, np.ndarray):
        raise TypeError('labels_mat should be numpy array')
    if labels_mat.ndim > 2:
        raise ValueError('labels_mat should be a 1D or 2D array')
    if not isinstance(num_clusters, int):
        raise TypeError('num_clusters should be an integer')
    if axis is not None:
        if not isinstance(axis, mpl.axes.Axes):
            raise TypeError('axis must be a matplotlib.axes.Axes object')
    if num_clusters is not None:
        if not isinstance(num_clusters, int) or num_clusters < 1:
            raise TypeError('num_clusters should be a positive integer')
    else:
        num_clusters = np.max(labels_mat)

    if axis is None:
        fig, axis = plt.subplots(figsize=kwargs.pop('figsize', (5.5, 5)))
    else:
        fig = None

    if labels_mat.ndim > 1:
        _, _ = plot_map(axis, labels_mat, clim=[0, num_clusters - 1], aspect='auto', show_xy_ticks=True,
                        cmap=discrete_cmap(num_clusters, kwargs.pop('cmap', default_cmap)), **kwargs)
    else:
        x_vec = kwargs.pop('x_vec', np.arange(labels_mat.size))
        axis.plot(x_vec, labels_mat, **kwargs)

    for var, var_name, func in zip([title, x_label, y_label], ['title', 'x_label', 'y_label'],
                                   [axis.set_title, axis.set_xlabel, axis.set_ylabel]):
        if var is not None:
            if not isinstance(var, (str, unicode)):
                raise TypeError(var_name + ' should be a string')
            func(var)

    if fig is not None:
        fig.tight_layout()

    return fig, axis
示例#4
0
def plot_cluster_labels(labels_mat,
                        num_clusters=None,
                        x_label=None,
                        y_label=None,
                        title=None,
                        axis=None,
                        **kwargs):
    """
    Plots the cluster labels

    Parameters
    ----------
    labels_mat : numpy.ndarray
        1D or 2D unsigned integer array containing the labels of the clusters
    num_clusters : int, optional
        Number of clusters
    x_label : str, optional
        Label for x axis
    y_label : str, optional
        Label for y axis
    title : str, optional
        Title for the plot
    axis : matplotlib.axes.Axes object, optional.
        Axis to plot this image onto. Will create a new figure by default or will use this axis object to plot into
    kwargs : dict
        will be passed on to plot() or plot_map()

    Returns
    -------
    fig : matplotlib.pyplot.Figure object
        figure object
    axis : matplotlib.Axes object
        axis object
    """
    if not isinstance(labels_mat, np.ndarray):
        raise TypeError('labels_mat should be numpy array')
    if labels_mat.ndim > 2:
        raise ValueError('labels_mat should be a 1D or 2D array')
    if not isinstance(num_clusters, int):
        raise TypeError('num_clusters should be an integer')
    if axis is not None:
        if not isinstance(axis, mpl.axes.Axes):
            raise TypeError('axis must be a matplotlib.axes.Axes object')
    if num_clusters is not None:
        if not isinstance(num_clusters, int) or num_clusters < 1:
            raise TypeError('num_clusters should be a positive integer')
    else:
        num_clusters = np.max(labels_mat)

    if axis is None:
        fig, axis = plt.subplots(figsize=kwargs.pop('figsize', (5.5, 5)))
    else:
        fig = None

    if labels_mat.ndim > 1:
        _, _ = plot_map(axis,
                        labels_mat,
                        clim=[0, num_clusters - 1],
                        aspect='auto',
                        show_xy_ticks=True,
                        cmap=discrete_cmap(num_clusters,
                                           kwargs.pop('cmap', default_cmap)),
                        **kwargs)
    else:
        x_vec = kwargs.pop('x_vec', np.arange(labels_mat.size))
        axis.plot(x_vec, labels_mat, **kwargs)

    for var, var_name, func in zip(
        [title, x_label, y_label], ['title', 'x_label', 'y_label'],
        [axis.set_title, axis.set_xlabel, axis.set_ylabel]):
        if var is not None:
            if not isinstance(var, (str, unicode)):
                raise TypeError(var_name + ' should be a string')
            func(var)

    if fig is not None:
        fig.tight_layout()

    return fig, axis
示例#5
0
 def test_cmap_not_str(self):
     with self.assertRaises(ValueError):
         plot_utils.discrete_cmap(num_bins=1, cmap='hello')
示例#6
0
 def test_numbins_is_not_uint(self):
     with self.assertRaises(TypeError):
         plot_utils.discrete_cmap(num_bins='hello')