plotting functions
Various plotting functions.
bar_classifier_f1
bar_classifier_f1(adata, ground_truth='celltype', class_prediction='SCN_class', bar_height=0.8)
Plots a bar graph of F1 scores per class based on ground truth and predicted classifications.
Parameters:
-
adata
(AnnData
) –Annotated data matrix.
-
ground_truth
(str
, default:'celltype'
) –The column name in
adata.obs
containing the true class labels. Defaults to "celltype". -
class_prediction
(str
, default:'SCN_class'
) –The column name in
adata.obs
containing the predicted class labels. Defaults to "SCN_class".
Returns:
-
–
None
Source code in src/pySingleCellNet/plotting/bar.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
|
bar_compare_celltype_composition
bar_compare_celltype_composition(adata1, adata2, celltype_col, min_delta, colors=None, metric='log_ratio')
Compare cell type proportions between two AnnData objects and plot either log-ratio or differences for significant changes.
Parameters:
-
adata1
(AnnData
) –First AnnData object.
-
adata2
(AnnData
) –Second AnnData object.
-
celltype_col
(str
) –Column name in
.obs
indicating cell types. -
min_delta
(float
) –Minimum absolute difference in percentages to include in the plot.
-
colors
(dict
, default:None
) –Dictionary with cell types as keys and colors as values for the bars.
-
metric
(str
, default:'log_ratio'
) –"log_ratio" (default) or "difference" to specify which metric to plot.
Returns:
-
None
–Displays the bar plot.
Source code in src/pySingleCellNet/plotting/bar.py
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 |
|
heatmap_clustering_eval
heatmap_clustering_eval(df, index_col='label_col', metrics=('n_clusters', 'unique_strict_genes', 'unique_naive_genes', 'frac_pairs_with_at_least_n_strict'), bar_sum_cols=('unique_strict_genes', 'unique_naive_genes'), cmap_eval='viridis', scale_eval='zscore', linewidth=0.5, value_fmt=None, title='Clustering parameter sweep (select best rows)', render=True, set_default_font=True)
Marsilea heatmap to guide clustering parameter selection.
Left: textual columns for parsed parameters (pc, k, res) Center: eval heatmap with raw numbers printed in cells (includes n_clusters as first column) Right: bar = unique_strict_genes + unique_naive_genes; rows sorted descending by this score
Row names (index strings) are NOT shown.
Source code in src/pySingleCellNet/plotting/heatmap.py
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 |
|
heatmap_gsea
heatmap_gsea(gmat, clean_signatures=False, clean_cells=False, column_colors=None, figsize=(8, 6), label_font_size=7, cbar_pos=[0.2, 0.92, 0.6, 0.02], dendro_ratio=(0.3, 0.1), cbar_title='NES', col_cluster=False, row_cluster=False)
Generates a heatmap with hierarchical clustering for gene set enrichment analysis (GSEA) results.
Parameters:
-
gmat
(DataFrame
) –A matrix of GSEA scores with gene sets as rows and samples as columns.
-
clean_signatures
(bool
, default:False
) –If True, removes gene sets with zero enrichment scores across all samples. Defaults to False.
-
clean_cells
(bool
, default:False
) –If True, removes samples with zero enrichment scores across all gene sets. Defaults to False.
-
column_colors
(Series or DataFrame
, default:None
) –Colors to annotate columns, typically representing sample groups. Defaults to None.
-
figsize
(tuple
, default:(8, 6)
) –Figure size in inches (width, height). Defaults to (8, 6).
-
label_font_size
(int
, default:7
) –Font size for axis and colorbar labels. Defaults to 7.
-
cbar_pos
(list
, default:[0.2, 0.92, 0.6, 0.02]
) –Position of the colorbar [left, bottom, width, height]. Defaults to [0.2, 0.92, 0.6, 0.02] for a horizontal top placement.
-
dendro_ratio
(tuple
, default:(0.3, 0.1)
) –Proportion of the figure allocated to the row and column dendrograms. Defaults to (0.3, 0.1).
-
cbar_title
(str
, default:'NES'
) –Title of the colorbar. Defaults to 'NES'.
-
col_cluster
(bool
, default:False
) –If True, performs hierarchical clustering on columns. Defaults to False.
-
row_cluster
(bool
, default:False
) –If True, performs hierarchical clustering on rows. Defaults to False.
Returns:
-
–
None
Displays
A heatmap with optional hierarchical clustering and a horizontal colorbar at the top.
Source code in src/pySingleCellNet/plotting/heatmap.py
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 |
|
heatmap_scores
heatmap_scores(adata, groupby, vmin=0, vmax=1, obsm_name='SCN_score', order_by=None, figure_subplot_bottom=0.4)
Plots a heatmap of single cell scores, grouping cells according to a specified .obs column and optionally ordering within each group.
Parameters:
-
adata
(AnnData
) –An AnnData object containing the single cell data.
-
groupby
(str
) –The name of the column in .obs used for grouping cells in the heatmap.
-
vmin
(float
, default:0
) –Minimum value for color scaling. Defaults to 0.
-
vmax
(float
, default:1
) –Maximum value for color scaling. Defaults to 1.
-
obsm_name
(str
, default:'SCN_score'
) –The key in .obsm to retrieve the matrix for plotting. Defaults to 'SCN_score'.
-
order_by
(str
, default:None
) –The name of the column in .obs used for ordering cells within each group. Defaults to None.
Returns:
-
None
–The function plots a heatmap and does not return any value.
Source code in src/pySingleCellNet/plotting/heatmap.py
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 |
|
make_bivariate_cmap
make_bivariate_cmap(c00='#f0f0f0', c10='#e31a1c', c01='#1f78b4', c11='#ffff00', n=128)
Create a bivariate colormap by bilinear‐interpolating four corner colors.
This builds an (n × n) grid of RGB colors, blending smoothly between the specified corner colors: - c00 at (low, low) - c10 at (high, low) - c01 at (low, high) - c11 at (high, high)
Parameters:
-
c00
(str
, default:'#f0f0f0'
) –Matplotlib color spec (hex, name, or RGB tuple) for the low/low corner.
-
c10
(str
, default:'#e31a1c'
) –Color for the high/low corner.
-
c01
(str
, default:'#1f78b4'
) –Color for the low/high corner.
-
c11
(str
, default:'#ffff00'
) –Color for the high/high corner.
-
n
(int
, default:128
) –Resolution per axis. The total length of the returned colormap is n*n.
Returns:
-
ListedColormap
(ListedColormap
) –A colormap with n*n entries blending between the four corners.
Source code in src/pySingleCellNet/plotting/helpers.py
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 |
|
scatter_genes_oneper
scatter_genes_oneper(adata, genes, embedding_key='X_spatial', spot_size=2, alpha=0.9, clip_percentiles=(0, 99.5), log_transform=True, cmap='Reds', figsize=None, panel_width=4.0, n_rows=1)
Plot expression of multiple genes on a 2D embedding arranged in a grid.
Each gene is optionally log-transformed, percentile-clipped, and rescaled to [0,1].
Cells are plotted on the embedding, colored by expression, with highest values
drawn on top. A single colorbar is placed to the right of the grid.
If figsize
is None, each panel has width panel_width
and height
proportional to the embedding's aspect ratio; total figure dims reflect
n_rows
and computed columns.
Parameters:
-
adata
(AnnData
) –AnnData containing the embedding in
adata.obsm[embedding_key]
. -
embedding_key
(str
, default:'X_spatial'
) –Key in
.obsm
for an (n_obs, 2) coordinate array. -
genes
(Sequence[str]
) –List of gene names to plot (must be in
adata.var_names
). -
spot_size
(float
, default:2
) –Marker size for scatter plots. Default 2.
-
alpha
(float
, default:0.9
) –Transparency for markers. Default 0.9.
-
clip_percentiles
(tuple
, default:(0, 99.5)
) –(low_pct, high_pct) to clip expression before rescaling.
-
log_transform
(bool
, default:True
) –If True, apply
np.log1p
to raw expression. -
cmap
(Union[str, Colormap]
, default:'Reds'
) –Colormap or name for all plots.
-
figsize
(Optional[tuple]
, default:None
) –(width, height) of entire figure. If None, computed from
panel_width
,n_rows
, and embedding aspect ratio. -
panel_width
(float
, default:4.0
) –Width (in inches) of each panel when
figsize
is None. -
n_rows
(int
, default:1
) –Number of rows in the grid. Default 1.
Raises:
-
ValueError
–If embedding is missing/malformed or genes not found.
Source code in src/pySingleCellNet/plotting/spatial.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 |
|
scatter_qc_adata
scatter_qc_adata(adata, title_suffix='')
Creates a figure with two scatter plot panels for visualizing data from an AnnData object.
The first panel shows 'total_counts' vs 'n_genes_by_counts', colored by 'pct_counts_mt'. The second panel shows 'n_genes_by_counts' vs 'pct_counts_mt'. An optional title suffix can be added to customize the axis titles.
Parameters:
-
adata
(AnnData
) –The AnnData object containing the dataset. Must contain 'total_counts', 'n_genes_by_counts', and 'pct_counts_mt' in
adata.obs
. -
title_suffix
(str
, default:''
) –A string to append to the axis titles, useful for specifying experimental conditions (e.g., "C11 day 2"). Defaults to an empty string.
Returns:
-
None
–The function displays a matplotlib figure with two scatter plots.
Example
plot_scatter_with_contours(adata, title_suffix="C11 day 2")
Source code in src/pySingleCellNet/plotting/scatter.py
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 |
|
spatial_contours
spatial_contours(adata, genes, spatial_key='spatial', summary_func=np.mean, spot_size=30, alpha=0.8, log_transform=True, clip_percentiles=(1, 99), cmap='viridis', contour_kwargs=None, scatter_kwargs=None)
Scatter spatial expression of one or more genes with smooth contour overlay.
If multiple genes are provided, each is preprocessed (log1p → clip
→ normalize), then combined per cell via summary_func
(e.g. mean, sum,
max) on the normalized values. A smooth contour of the summarized signal
is overlaid onto the spatial scatter.
Parameters:
-
adata
(AnnData
) –AnnData with spatial coordinates in
adata.obsm[spatial_key]
. -
genes
(Union[str, Sequence[str]]
) –Single gene name or list of gene names to plot (must be in
adata.var_names
). -
spatial_key
(str
, default:'spatial'
) –Key in
.obsm
for an (n_obs, 2) coords array. -
summary_func
(Callable[[ndarray], ndarray]
, default:mean
) –Function to combine multiple normalized gene arrays (takes an (n_obs, n_genes) array, returns length-n_obs array). Defaults to
np.mean
. -
spot_size
(float
, default:30
) –Scatter marker size.
-
alpha
(float
, default:0.8
) –Scatter alpha transparency.
-
log_transform
(bool
, default:True
) –If True, apply
np.log1p
to raw expression before clipping. -
clip_percentiles
(tuple
, default:(1, 99)
) –Tuple
(low_pct, high_pct)
percentiles to clip each gene. -
cmap
(str
, default:'viridis'
) –Colormap name for the scatter (e.g. 'viridis').
-
contour_kwargs
(dict
, default:None
) –Dict of parameters for smoothing & contouring: - levels: int or list of levels (default 6) - grid_res: int grid resolution (default 200) - smooth_sigma: float Gaussian blur sigma (default 2) - contour_kwargs: dict of line style kwargs (default {'colors':'k','linewidths':1})
-
scatter_kwargs
(dict
, default:None
) –Extra kwargs passed to
ax.scatter
.
Raises:
-
ValueError
–If any gene is missing or spatial coords are malformed.
Source code in src/pySingleCellNet/plotting/spatial.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
|
spatial_two_genes
spatial_two_genes(adata, gene1, gene2, cmap, spot_size=2, alpha=0.9, spatial_key='X_spatial', log_transform=False, clip_percentiles=(0, 99.5), priority_metric='sum', show_xcoords=False, show_ycoords=False, show_bbox=False, show_legend=True, width_ratios=(10, 1))
Plot two‐gene spatial expression with a bivariate colormap.
Parameters:
-
adata
(AnnData
) –AnnData with spatial coords in
adata.obsm[spatial_key]
. -
gene1
(str
) –First gene name (must be in
adata.var_names
). -
gene2
(str
) –Second gene name.
-
cmap
(ListedColormap
) –Bivariate colormap from
make_bivariate_cmap
(n×n LUT). -
spot_size
(float
, default:2
) –Scatter point size.
-
alpha
(float
, default:0.9
) –Point alpha transparency.
-
spatial_key
(str
, default:'X_spatial'
) –Key in
adata.obsm
for an (n_obs, 2) coords array. -
log_transform
(bool
, default:False
) –If True, apply
np.log1p
to raw expression. -
clip_percentiles
(tuple
, default:(0, 99.5)
) –Tuple
(low_pct, high_pct)
to clip each gene. -
priority_metric
(str
, default:'sum'
) –Which metric to sort drawing order by: - 'sum': u + v (default) - 'gene1': u only - 'gene2': v only
-
show_xcoords
(bool
, default:False
) –Whether to display x-axis ticks and labels.
-
show_ycoords
(bool
, default:False
) –Whether to display y-axis ticks and labels.
-
show_bbox
(bool
, default:False
) –Whether to display the bounding box (spines).
-
show_legend
(bool
, default:True
) –Whether to display the legend/colorbar.
-
width_ratios
(Tuple[float, float]
, default:(10, 1)
) –2‐tuple giving the relative widths of (scatter_panel, legend_panel). Defaults to (3,1).
Raises:
-
ValueError
–If spatial coords are missing/malformed or if
priority_metric
is invalid.
Source code in src/pySingleCellNet/plotting/spatial.py
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 |
|
stackedbar_composition
stackedbar_composition(adata, groupby, obs_column='SCN_class', labels=None, bar_width=0.75, color_dict=None, ax=None, order_by_similarity=False, similarity_metric='correlation', include_legend=True, legend_rows=10)
Plots a stacked bar chart of cell type proportions for a single AnnData object grouped by a specified column.
Parameters:
-
adata
(AnnData
) –An AnnData object.
-
groupby
(str
) –The column in
.obs
to group by. -
obs_column
(str
, default:'SCN_class'
) –The name of the
.obs
column to use for categories. Defaults to 'SCN_class'. -
labels
(List[str]
, default:None
) –Custom labels for each group to be displayed on the x-axis. If not provided, the unique values of the groupby column will be used. The length of
labels
must match the number of unique groups. -
bar_width
(float
, default:0.75
) –The width of the bars in the plot. Defaults to 0.75.
-
color_dict
(Dict[str, str]
, default:None
) –A dictionary mapping categories to specific colors. If not provided, default colors will be used.
-
ax
(Axes
, default:None
) –The axis to plot on. If not provided, a new figure and axis will be created.
-
order_by_similarity
(bool
, default:False
) –Whether to order the bars by similarity in composition. Defaults to False.
-
similarity_metric
(str
, default:'correlation'
) –The metric to use for similarity ordering. Defaults to 'correlation'.
-
include_legend
(bool
, default:True
) –Whether to include a legend in the plot. Defaults to True.
-
legend_rows
(int
, default:10
) –The number of rows in the legend. Defaults to 10.
Raises:
-
ValueError
–If the length of
labels
does not match the number of unique groups.
Examples:
>>> stackedbar_composition(adata, groupby='sample', obs_column='your_column_name')
>>> fig, ax = plt.subplots()
>>> stackedbar_composition(adata, groupby='sample', obs_column='your_column_name', ax=ax, include_legend=False, legend_rows=5)
Source code in src/pySingleCellNet/plotting/bar.py
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 |
|
stackedbar_composition_list
stackedbar_composition_list(adata_list, obs_column='SCN_class', labels=None, bar_width=0.75, color_dict=None, legend_loc='outside center right')
Plots a stacked bar chart of category proportions for a list of AnnData objects.
This function takes a list of AnnData objects, and for a specified column in the .obs
attribute,
it plots a stacked bar chart. Each bar represents an AnnData object with segments showing the proportion
of each category within that object.
Parameters:
-
adata_list
(List[AnnData]
) –A list of AnnData objects.
-
obs_column
(str
, default:'SCN_class'
) –The name of the
.obs
column to use for categories. Defaults to 'SCN_class'. -
labels
(List[str]
, default:None
) –Custom labels for each AnnData object to be displayed on the x-axis. If not provided, defaults to 'AnnData {i+1}' for each object. The length of
labels
must match the number of AnnData objects provided. -
bar_width
(float
, default:0.75
) –The width of the bars in the plot. Defaults to 0.75.
-
color_dict
(Dict[str, str]
, default:None
) –A dictionary mapping categories to specific colors. If not provided, default colors will be used.
Raises:
-
ValueError
–If the length of
labels
does not match the number of AnnData objects.
Examples:
>>> plot_cell_type_proportions([adata1, adata2], obs_column='your_column_name', labels=['Sample 1', 'Sample 2'])
Source code in src/pySingleCellNet/plotting/bar.py
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 |
|
umap_scores
umap_scores(adata, scn_classes, obsm_name='SCN_score', alpha=0.75, s=10, display=True)
Plots UMAP projections of scRNA-seq data with specified scores.
Parameters:
-
adata
(AnnData
) –The AnnData object containing the scRNA-seq data.
-
scn_classes
(list
) –A list of SCN classes to visualize on the UMAP.
-
obsm_name
(str
, default:'SCN_score'
) –The name of the obsm key containing the SCN scores. Defaults to 'SCN_score'.
-
alpha
(float
, default:0.75
) –The transparency level of the points on the UMAP plot. Defaults to 0.75.
-
s
(int
, default:10
) –The size of the points on the UMAP plot. Defaults to 10.
-
display
(bool
, default:True
) –If True, the plot is displayed immediately. If False, the axis object is returned. Defaults to True.
Returns:
-
–
matplotlib.axes.Axes or None: If
display
is False, returns the matplotlib axes object. Otherwise, returns None.
Source code in src/pySingleCellNet/plotting/dot.py
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 |
|
umi_counts_ranked
umi_counts_ranked(adata, total_counts_column='total_counts')
Identifies and plors the knee point of the UMI count distribution in an AnnData object.
Parameters:
-
adata
(AnnData
) –The input AnnData object.
-
total_counts_column
(str
, default:'total_counts'
) –Column in
adata.obs
containing total UMI counts. Default is "total_counts". -
show
(bool
) –If True, displays a log-log plot with the knee point. Default is True.
Returns:
-
float
–The UMI count value at the knee point.
Source code in src/pySingleCellNet/plotting/dot.py
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 |
|