Visualizations (Viz)¶
valency-anndata methods¶
valency_anndata.viz.schematic_diagram ¶
schematic_diagram(
adata: Optional[AnnData] = None,
*,
diff_from: Optional[AnnData] | Literal[False] = False,
filename: Optional[str] = None,
)
Render a schematic diagram of an AnnData object, optionally highlighting structural differences relative to a snapshot.
This function supports two usage modes: render mode and context-manager mode.
1. Render mode
Render a diagram of adata immediately.
Examples
adata_snapshot = adata.copy()
val.tools.some_transformation(adata, inplace=True)
val.viz.schematic_diagram(adata, diff_from=adata_snapshot)
Behavior
- Visualizes
adatastructure (X,obs,var,layers,obsm). - If
diff_fromis provided:- Highlights additions and removals relative to
diff_from.
- Highlights additions and removals relative to
- If
diff_fromisNone:- Highlights all entries as additions (diff from empty AnnData).
- If
diff_fromisFalse:- No diff highlighting is applied.
- The diagram is displayed inline (notebooks) or in a browser (script).
2. Context-manager mode
Capture a snapshot on entering a with block, rendering a diff on exit.
Examples
Behavior
diff_frommust be provided;adatamust be omitted.- On entry, a snapshot of
diff_fromis recorded. - On exit, a diff diagram between the snapshot and current
adatais rendered. - Exceptions inside the
withblock prevent rendering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
Optional[AnnData]
|
The AnnData object to visualize (required in render mode, must be omitted in context-manager mode). |
None
|
diff_from
|
Optional[AnnData] | Literal[False]
|
Determines the snapshot to diff against: (must be AnnData in context-manager mode)
- |
False
|
filename
|
Optional[str]
|
Optional filename hint used in cell output metadata. This is used when during generation of documentation website, when nbconvert extracts images from notebooks. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
In render mode, the diagram is displayed; nothing is returned. |
_SchematicDiagramContext
|
In context-manager mode, a context manager for automatic diff rendering. |
Notes
- Explicit diff rendering always takes precedence over context-manager snapshots.
- Snapshots are stored internally to allow nested diff scopes.
- This function does not mutate
adata.
Source code in src/valency_anndata/viz/schematic_diagram/__init__.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
valency_anndata.viz.voter_vignette_browser ¶
voter_vignette_browser(adata: AnnData) -> None
Interactive browser for quickly surveying many voting timelines of random participants alongside statements they authored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
An AnnData object loaded from a Polis conversation. |
required |
Assumptions
-
Votes are stored in
adata.uns["votes"]with columns:voter-idvote(-1, 0, 1)timestamp(seconds since epoch)
-
Statements are stored in
adata.varwith columns:participant_id_authoredcreated_date(milliseconds since epoch)contentmoderation_state(optional, -1/0/1)
Behavior
- Renders a dropdown to select a user, with buttons for random voter or commenter.
- Plots votes over time with colors (red/neutral/green).
- Draws vertical bars for authored statements with moderation-state coloring.
- Displays statements below the plot in submission order.
- Warns if vote or statement timestamps appear out of expected ranges.
Examples:
adata = val.datasets.polis.load("https://pol.is/report/r29kkytnipymd3exbynkd", translate_to="en")
val.viz.voter_vignette_browser(adata)
Source code in src/valency_anndata/viz/_voter_vignette.py
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
valency_anndata.viz.jscatter ¶
jscatter(
adata: AnnData,
use_reps: list[str] = [],
color: str | Iterable[str] | None = None,
height: int = 640,
dark_mode: bool = True,
nrows: Optional[int] = None,
ncols: Optional[int] = None,
return_objs: bool = False,
) -> list[Scatter] | None
Interactive Jupyter-Scatter view showing one or more embeddings. [Lekschas et al., 2024]
A button is created for each projected representation, and clicking will animate points into that projection.
Passing multiple color keys will display mulitple linked views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
An AnnData object with some projected representations stored in
|
required |
use_reps
|
list[str]
|
One or more keys for projected representations of the data stored in
|
[]
|
color
|
str | Iterable[str] | None
|
None
|
|
height
|
int
|
Pixel height of the scatter widget in output cell. |
640
|
dark_mode
|
bool
|
Whether to set the plot background dark. |
True
|
nrows
|
Optional[int]
|
Number of rows to display the scatter plots in. |
None
|
ncols
|
Optional[int]
|
Number of columns to display the scatter plots in. |
None
|
return_objs
|
bool
|
Whether to return the Scatter object(s). |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
scatters |
list[Scatter] | None
|
A list of |
Examples:
Plotting multiple representations in one view, colored with discrete categorical values.

Plotting mulitple .obs keys across multiple views, colored with continuous values.
val.viz.jscatter(
adata,
use_reps=["X_pca_polis", "X_pacmap"],
color=["n_votes", "pct_agree", "pct_pass", "pct_disagree"],
height=320,
)

Source code in src/valency_anndata/viz/_jupyter_scatter.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
valency_anndata.viz.langevitour ¶
langevitour(
adata: AnnData,
*,
use_reps: Optional[Sequence[str]] = None,
color: Optional[str] = None,
scale: Optional[str] = None,
initial_axes: Optional[list[str]] = None,
point_size: int = 2,
**kwargs,
)
Interactive Langevitour visualization over one or more representations. [Harrison, 2022]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
AnnData object. |
required |
use_reps
|
Optional[Sequence[str]]
|
Representations to include, e.g. |
None
|
color
|
Optional[str]
|
obs column for grouping / coloring. |
None
|
scale
|
Optional[str]
|
obs column for point scaling. |
None
|
initial_axes
|
Optional[list[str]]
|
Set up to 3 axes, initially locked in place along XYZ axes (these can be moved). Each must be specified with an exact index, not ranges. e.g. |
None
|
point_size
|
int
|
Base point size. |
2
|
**kwargs
|
Passed through to |
{}
|
Examples:
val.viz.langevitour(
adata,
use_reps=["X_umap", "X_pca[:10]"],
color="leiden",
initial_axes=["X_umap[0]", "X_umap[1]"],
)
Source code in src/valency_anndata/viz/_langevitour.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
valency_anndata.viz.highly_variable_statements ¶
highly_variable_statements(
adata: AnnData,
*,
key: str = "highly_variable",
log: bool = False,
show: bool | None = None,
save: str | None = None,
) -> None
Plot normalized and raw dispersions for statements identified as highly variable.
Analogous to scanpy.pl.highly_variable_genes for single-cell data. Creates a
two-panel scatter plot showing normalized dispersion (left) and raw dispersion (right)
against the binning variable used in val.preprocessing.highly_variable_statements.
Highly variable statements are highlighted in black, others in grey.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
AnnData object that has been processed with
|
required |
key
|
str
|
Key in |
'highly_variable'
|
log
|
bool
|
If True, use log scale for both axes. Default is False. |
False
|
show
|
bool | None
|
If True, display the plot. If None, defaults to Scanpy's |
None
|
save
|
str | None
|
File path to save the figure. If provided, figure is saved instead of shown. |
None
|
Examples:
import valency_anndata as val
adata = val.datasets.aufstehen()
val.preprocessing.highly_variable_statements(adata, n_top_statements=50)
val.viz.highly_variable_statements(adata)
Use log scale for better visibility:
Plot results from a custom key:
val.preprocessing.highly_variable_statements(
adata,
n_top_statements=100,
key_added="highly_variable_top100"
)
val.viz.highly_variable_statements(adata, key="highly_variable_top100")
Source code in src/valency_anndata/viz/_highly_variable_statements.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
scanpy methods (inherited)¶
Note
These methods are simply quick convenience wrappers around methods in scanpy, a tool for single-cell gene expression. They will use terms like "cells", "genes" and "counts", but you can think of these as "participants", "statements" and "votes".
See scanpy.pl for more methods you can experiment with via the val.scanpy.pl namespace.
valency_anndata.viz.pca ¶
pca(
adata: AnnData,
*,
annotate_var_explained: bool = False,
show: bool | None = None,
return_fig: bool | None = None,
save: bool | str | None = None,
**kwargs,
) -> Figure | Axes | list[Axes] | None
Scatter plot in PCA coordinates.
Use the parameter annotate_var_explained to annotate the explained variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotate_var_explained
|
bool
|
|
False
|
Returns:
| Type | Description |
|---|---|
If `show==False` a :class:`~matplotlib.axes.Axes` or a list of it.
|
|
Examples:
.. plot:: :context: close-figs
import scanpy as sc
adata = sc.datasets.pbmc3k_processed()
sc.pl.pca(adata)
Colour points by discrete variable (Louvain clusters).
.. plot:: :context: close-figs
sc.pl.pca(adata, color="louvain")
Colour points by gene expression.
.. plot:: :context: close-figs
sc.pl.pca(adata, color="CST3")
.. currentmodule:: scanpy
See Also
pp.pca
Source code in .venv/lib/python3.10/site-packages/scanpy/plotting/_tools/scatterplots.py
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | |
valency_anndata.viz.umap ¶
Scatter plot in UMAP basis.
Returns:
| Type | Description |
|---|---|
If `show==False` a :class:`~matplotlib.axes.Axes` or a list of it.
|
|
Examples:
.. plot:: :context: close-figs
import scanpy as sc
adata = sc.datasets.pbmc68k_reduced()
sc.pl.umap(adata)
Colour points by discrete variable (Louvain clusters).
.. plot:: :context: close-figs
sc.pl.umap(adata, color="louvain")
Colour points by gene expression.
.. plot:: :context: close-figs
sc.pl.umap(adata, color="HES4")
Plot muliple umaps for different gene expressions.
.. plot:: :context: close-figs
sc.pl.umap(adata, color=["HES4", "TNFRSF4"])
.. currentmodule:: scanpy
See Also
tl.umap
Source code in .venv/lib/python3.10/site-packages/scanpy/plotting/_tools/scatterplots.py
valency_anndata.viz.embedding ¶
embedding(
adata: AnnData,
basis: str,
*,
color: str | Sequence[str] | None = None,
mask_obs: NDArray[bool_] | str | None = None,
gene_symbols: str | None = None,
use_raw: bool | None = None,
sort_order: bool = True,
edges: bool = False,
edges_width: float = 0.1,
edges_color: str
| Sequence[float]
| Sequence[str] = "grey",
neighbors_key: str | None = None,
arrows: bool = False,
arrows_kwds: Mapping[str, Any] | None = None,
groups: str | Sequence[str] | None = None,
components: str | Sequence[str] | None = None,
dimensions: tuple[int, int]
| Sequence[tuple[int, int]]
| None = None,
layer: str | None = None,
projection: Literal["2d", "3d"] = "2d",
scale_factor: float | None = None,
color_map: Colormap | str | None = None,
cmap: Colormap | str | None = None,
palette: str | Sequence[str] | Cycler | None = None,
na_color: ColorLike = "lightgray",
na_in_legend: bool = True,
size: float | Sequence[float] | None = None,
frameon: bool | None = None,
legend_fontsize: float | _FontSize | None = None,
legend_fontweight: int | _FontWeight = "bold",
legend_loc: _LegendLoc | None = "right margin",
legend_fontoutline: int | None = None,
colorbar_loc: str | None = "right",
vmax: VBound | Sequence[VBound] | None = None,
vmin: VBound | Sequence[VBound] | None = None,
vcenter: VBound | Sequence[VBound] | None = None,
norm: Normalize | Sequence[Normalize] | None = None,
add_outline: bool | None = False,
outline_width: tuple[float, float] = (0.3, 0.05),
outline_color: tuple[str, str] = ("black", "white"),
ncols: int = 4,
hspace: float = 0.25,
wspace: float | None = None,
title: str | Sequence[str] | None = None,
show: bool | None = None,
save: bool | str | None = None,
ax: Axes | None = None,
return_fig: bool | None = None,
marker: str | Sequence[str] = ".",
**kwargs,
) -> Figure | Axes | list[Axes] | None
Scatter plot for user specified embedding basis (e.g. umap, pca, etc).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
basis
|
str
|
Name of the |
required |
Returns:
| Type | Description |
|---|---|
If `show==False` a :class:`~matplotlib.axes.Axes` or a list of it.
|
|
Source code in .venv/lib/python3.10/site-packages/scanpy/plotting/_tools/scatterplots.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |