(
    points,
    ax=None,
    labels=None,
    values=None,
    cmap="Blues",
    color_key=None,
    color_key_cmap="Spectral",
    background="white",
    width=800,
    height=800,
    show_legend=True,
    alpha=255,
)
Use datashader to plot points
Use datashader to plot points
Use datashader to plot points
def _datashade_points(
    points,
    ax=None,
    labels=None,
    values=None,
    cmap="Blues",
    color_key=None,
    color_key_cmap="Spectral",
    background="white",
    width=800,
    height=800,
    show_legend=True,
    alpha=255,
):

    """Use datashader to plot points"""
    extent = _get_extent(points)
    canvas = ds.Canvas(
        plot_width=width,
        plot_height=height,
        x_range=(extent[0], extent[1]),
        y_range=(extent[2], extent[3]),
    )
    data = pd.DataFrame(points, columns=("x", "y"))

    legend_elements = None

    # Color by labels
    if labels is not None:
        if labels.shape[0] != points.shape[0]:
            raise ValueError(
                "Labels must have a label for "
                "each sample (size mismatch: {} {})".format(
                    labels.shape[0], points.shape[0]
                )
            )

        data["label"] = pd.Categorical(labels)
        aggregation = canvas.points(data, "x", "y", agg=ds.count_cat("label"))
        if color_key is None and color_key_cmap is None:
            result = tf.shade(aggregation, how="eq_hist", alpha=alpha)
        elif color_key is None:
            unique_labels = np.unique(labels)
            num_labels = unique_labels.shape[0]
            color_key = _to_hex(
                plt.get_cmap(color_key_cmap)(np.linspace(0, 1, num_labels))
            )
            legend_elements = [
                Patch(facecolor=color_key[i], label=k)
                for i, k in enumerate(unique_labels)
            ]
            result = tf.shade(
                aggregation, color_key=color_key, how="eq_hist", alpha=alpha
            )
        else:
            legend_elements = [
                Patch(facecolor=color_key[k], label=k) for k in color_key.keys()
            ]
            result = tf.shade(
                aggregation, color_key=color_key, how="eq_hist", alpha=alpha
            )

    # Color by values
    elif values is not None:
        if values.shape[0] != points.shape[0]:
            raise ValueError(
                "Values must have a value for "
                "each sample (size mismatch: {} {})".format(
                    values.shape[0], points.shape[0]
                )
            )
        unique_values = np.unique(values)
        if unique_values.shape[0] >= 256:
            min_val, max_val = np.min(values), np.max(values)
            bin_size = (max_val - min_val) / 255.0
            data["val_cat"] = pd.Categorical(
                np.round((values - min_val) / bin_size).astype(np.int16)
            )
            aggregation = canvas.points(data, "x", "y", agg=ds.count_cat("val_cat"))
            color_key = _to_hex(plt.get_cmap(cmap)(np.linspace(0, 1, 256)))
            result = tf.shade(
                aggregation, color_key=color_key, how="eq_hist", alpha=alpha
            )
        else:
            data["val_cat"] = pd.Categorical(values)
            aggregation = canvas.points(data, "x", "y", agg=ds.count_cat("val_cat"))
            color_key_cols = _to_hex(
                plt.get_cmap(cmap)(np.linspace(0, 1, unique_values.shape[0]))
            )
            color_key = dict(zip(unique_values, color_key_cols))
            result = tf.shade(
                aggregation, color_key=color_key, how="eq_hist", alpha=alpha
            )

    # Color by density (default datashader option)
    else:
        aggregation = canvas.points(data, "x", "y", agg=ds.count())
        result = tf.shade(aggregation, cmap=plt.get_cmap(cmap), alpha=alpha)

    if background is not None:
        result = tf.set_background(result, background)

    if ax is not None:
        _embed_datashader_in_an_axis(result, ax)
        if show_legend and legend_elements is not None:
            ax.legend(handles=legend_elements)
        return ax
    else:
        return result
def _datashade_points ( points , ax = None , labels = None , values = None , cmap = "Blues" , color_key = None , color_key_cmap = "Spectral" , background = "white" , width = 800 , height = 800 , show_legend = True , alpha = 255 , ) : extent = _get_extent ( points ) canvas = ds . Canvas ( plot_width = width , plot_height = height , x_range = ( extent [ 0 ] , extent [ 1 ] ) , y_range = ( extent [ 2 ] , extent [ 3 ] ) , ) data = pd . DataFrame ( points , columns = ( "x" , "y" ) ) legend_elements = None # Color by labels if labels is not None : if labels . shape [ 0 ] != points . shape [ 0 ] : raise ValueError ( "Labels must have a label for " "each sample (size mismatch: {} {})" . format ( labels . shape [ 0 ] , points . shape [ 0 ] ) ) data [ "label" ] = pd . Categorical ( labels ) aggregation = canvas . points ( data , "x" , "y" , agg = ds . count_cat ( "label" ) ) if color_key is None and color_key_cmap is None : result = tf . shade ( aggregation , how = "eq_hist" , alpha = alpha ) elif color_key is None : unique_labels = np . unique ( labels ) num_labels = unique_labels . shape [ 0 ] color_key = _to_hex ( plt . get_cmap ( color_key_cmap ) ( np . linspace ( 0 , 1 , num_labels ) ) ) legend_elements = [ Patch ( facecolor = color_key [ i ] , label = k ) for i , k in enumerate ( unique_labels ) ] result = tf . shade ( aggregation , color_key = color_key , how = "eq_hist" , alpha = alpha ) else : legend_elements = [ Patch ( facecolor = color_key [ k ] , label = k ) for k in color_key . keys ( ) ] result = tf . shade ( aggregation , color_key = color_key , how = "eq_hist" , alpha = alpha ) # Color by values elif values is not None : if values . shape [ 0 ] != points . shape [ 0 ] : raise ValueError ( "Values must have a value for " "each sample (size mismatch: {} {})" . format ( values . shape [ 0 ] , points . shape [ 0 ] ) ) unique_values = np . unique ( values ) if unique_values . shape [ 0 ] >= 256 : min_val , max_val = np . min ( values ) , np . max ( values ) bin_size = ( max_val - min_val ) / 255.0 data [ "val_cat" ] = pd . Categorical ( np . round ( ( values - min_val ) / bin_size ) . astype ( np . int16 ) ) aggregation = canvas . points ( data , "x" , "y" , agg = ds . count_cat ( "val_cat" ) ) color_key = _to_hex ( plt . get_cmap ( cmap ) ( np . linspace ( 0 , 1 , 256 ) ) ) result = tf . shade ( aggregation , color_key = color_key , how = "eq_hist" , alpha = alpha ) else : data [ "val_cat" ] = pd . Categorical ( values ) aggregation = canvas . points ( data , "x" , "y" , agg = ds . count_cat ( "val_cat" ) ) color_key_cols = _to_hex ( plt . get_cmap ( cmap ) ( np . linspace ( 0 , 1 , unique_values . shape [ 0 ] ) ) ) color_key = dict ( zip ( unique_values , color_key_cols ) ) result = tf . shade ( aggregation , color_key = color_key , how = "eq_hist" , alpha = alpha ) # Color by density (default datashader option) else : aggregation = canvas . points ( data , "x" , "y" , agg = ds . count ( ) ) result = tf . shade ( aggregation , cmap = plt . get_cmap ( cmap ) , alpha = alpha ) if background is not None : result = tf . set_background ( result , background ) if ax is not None : _embed_datashader_in_an_axis ( result , ax ) if show_legend and legend_elements is not None : ax . legend ( handles = legend_elements ) return ax else : return result