Saving Interactions as SVG
Let's first save the results from our homepage's quickstart into a plot object.
from distortions.geometry import Geometry, bind_metric, local_distortions, neighborhoods
from distortions.visualization import dplot
import anndata as ad
import numpy as np
import scanpy as sc
adata = ad.AnnData(np.random.poisson(2, size=(100, 5)))
sc.pp.neighbors(adata, n_neighbors=15)
sc.tl.umap(adata)
geom = Geometry(affinity_kwds={"radius": 2}, adjacency_kwds={"n_neighbors": 15})
_, Hvv, Hs = local_distortions(adata.obsm["X_umap"], adata.X, geom)
embedding = bind_metric(adata.obsm["X_umap"], Hvv, Hs)
N = neighborhoods(adata, outlier_factor=1)
plot = dplot(embedding)\
.mapping(x="embedding_0", y="embedding_1")\
.inter_edge_link(N=N)\
.geom_ellipse()
If you print plot, you will see the figure. Hovering the mouse near a point highlights its neighbors. If you double click the figure, it freezes the interactivity.
plot
dplot(dataset=[{'embedding_0': 12.975311279296875, 'embedding_1': 7.4127984046936035, 'x0': 0.0360943131024686…
To save a specific view, call the save()
method on the plot object. By default, it will save a file plot.svg
to the current notebook directory. You can specify your own path, but it must be an SVG file.
plot.save("output.svg")
One known limitation is that you have to at least hover over the SVG before the plot will properly save. This is because anywidget loads some utilities lazily, and we can't save until some interactions have been made. The first time you run plot.save()
(before any interactions), it might save an empty svg file to the directory.