PlottingMixin#

class scikitplot.annoy.PlottingMixin[source]#

Mixin that adds convenient plotting methods to high-level Annoy wrappers.

The mixin assumes the host class is Annoy-like (implements get_n_items(), get_item_vector(i), and get_nns_by_item(...)). If your wrapper delegates to an internal Annoy instance, override _plotting_backend to return that backend.

See also

plot_annoy_index

Function-level plotting helper.

plot_annoy_knn_edges

Function-level kNN edge overlay helper.

Notes

  • The underlying plotting logic is implemented in scikitplot.cexternals._annoy._plotting.

  • All methods are deterministic given index contents and parameters.

plot_index(labels=None, *, ids=None, projection='pca', dims=(0, 1), center=True, maxabs=False, l2_normalize=False, dtype=<class 'numpy.float32'>, ax=None, title=None, plot_kwargs=None)[source]#

Plot this index as a 2D scatter plot.

This is a thin wrapper around plot_annoy_index that uses _plotting_backend.

Parameters:
labels, ids, projection, dims, center, maxabs, l2_normalize, dtype, ax, title, plot_kwargs

See plot_annoy_index.

Returns:
y2, ids_out, ax

See plot_annoy_index.

Parameters:
Return type:

tuple[ndarray, ndarray, Any]

See also

plot_annoy_index

Low-level plotting helper this method delegates to.

plot_knn_edges

Overlay kNN edges on the returned 2D coordinates.

Notes

  • This method does not mutate the index.

  • Plotting backends (e.g. Matplotlib) are imported lazily and are only required when this method is called.

  • The returned ids_out corresponds to the item id for each row in y2.

Examples

>>> import numpy as np
>>> import scikitplot.annoy as skann
>>> idx = skann.Index(f=10, metric="angular")
>>> # ... add items & build ...
>>> labels = np.zeros(idx.get_n_items(), dtype=int)
>>> y2, ids, ax = idx.plot_index(labels=labels, projection="pca")
plot_knn_edges(y2, *, ids=None, k=10, search_k=-1, ax=None, line_kwargs=None, undirected=True)[source]#

Overlay kNN edges onto an existing 2D index plot.

This is a thin wrapper around plot_annoy_knn_edges that uses _plotting_backend.

Parameters:
y2, ids, k, search_k, ax, line_kwargs, undirected

See plot_annoy_knn_edges.

Returns:
ax

The axes that were drawn on.

Parameters:
Return type:

Any

See also

plot_annoy_knn_edges

Low-level edge overlay helper this method delegates to.

plot_index

Computes the 2D coordinates used as input to this method.

Notes

  • y2 must represent 2D coordinates with shape (n_samples, 2).

  • If ids is provided, it must have length n_samples.

  • This method does not mutate the index; it only performs neighbor queries to draw edges.

Examples

>>> y2, ids, ax = idx.plot_index(labels=np.zeros(idx.get_n_items(), dtype=int))
>>> idx.plot_knn_edges(y2, ids=ids, k=5, line_kwargs={"alpha": 0.15})