Plotting¶
Every function here returns a matplotlib Figure, so you save it, compose it, or
let a notebook display it:
fig = truecell.dim_plot(pbmc, reduction="umap", label=True)
fig.savefig("umap.png", dpi=150, bbox_inches="tight")
matplotlib is optional — it lives in the analysis extra, and import truecell
works without it. That is why the Figure return annotation is an
if TYPE_CHECKING: import: it documents the return type without making the import
mandatory.
Theme¶
Every plot on this page scales its text from one base size and takes its group colours from one palette. Set them once instead of passing the same overrides to each call:
import truecell as tc
tc.set_theme(base_size=13, style="seurat") # bigger text, cowplot's look
fig = tc.dim_plot(pbmc)
with tc.theme_context(base_size=8): # scoped to the block
panel = tc.feature_plot(pbmc, ["MS4A1", "LYZ"])
The default (base_size=10, no style) leaves matplotlib's rcParams untouched and
reproduces the sizes the module used before the theme existed.
set_theme
¶
set_theme(base_size: Optional[float] = None, palette: Optional[list] = None, font: Optional[str] = None, dpi: Optional[float] = None, style: Optional[str] = None) -> dict
Set the look of every plot this module draws.
The Python counterpart of applying a ggplot theme once and having Seurat's plots pick it up, rather than passing the same overrides to every call.
Only the arguments you pass are changed; the rest keep their current values.
Use reset_theme to go back to the defaults, or theme_context
to scope a change to a with block.
Parameters:
-
base_size(Optional[float], default:None) –float, optional Base font size in points (default 10.0). Every text element scales from it, so this is the one knob for "make the labels bigger" — a figure destined for a slide wants 13-14, a dense multi-panel figure wants 8.
-
palette(Optional[list], default:None) –list, optional Categorical colours to use instead of ggplot's
hue_pal. Ignored for any plot with more groups than the list has entries, which falls back tohue_palrather than repeating a colour. -
font(Optional[str], default:None) –str, optional matplotlib font family, e.g.
"Helvetica","DejaVu Sans". -
dpi(Optional[float], default:None) –float, optional Figure and savefig dpi.
-
style(Optional[str], default:None) –str, optional
"seurat"(cowplot's look — no grid, black spines) or"minimal"(faint grid, grey spines).Noneleaves matplotlib's rcParams untouched.
Returns:
-
dict–The theme as applied, so a caller can stash it and restore it later.
Examples:
Source code in truecell/plotting.py
theme_context
¶
Apply a theme for the duration of a with block, then restore it.
with theme_context(base_size=14, style="seurat"): # doctest: +SKIP ... fig = dim_plot(pbmc)
Source code in truecell/plotting.py
get_theme
¶
reset_theme
¶
Restore the default theme and matplotlib's own rcParams.
Source code in truecell/plotting.py
Colour¶
hue_pal
¶
n evenly-spaced HCL hues — ggplot2's default discrete colour scale.
The Python counterpart of scales::hue_pal()(n), which is what Seurat's
DimPlot/VlnPlot/DoHeatmap colour groups with. Verified exact
against R for n = 1-6, 8 and 9 in tests/test_plotting_theme.py.
Computing the ramp beats carrying a fixed list, for two reasons. The palette
depends on n — ggplot spreads the same hue circle across however many
groups there are, so the colours for 9 clusters are not the colours for 8
plus one more — and a fixed list therefore only matches Seurat at the one
length it was copied from. It also cannot run out or repeat.
Source code in truecell/plotting.py
Plots¶
dim_plot
¶
dim_plot(obj, reduction: str = 'umap', group_by: Optional[str] = None, label: bool = True, label_size: Optional[float] = None, pt_size: float = 4.0, alpha: float = 0.7, figsize: tuple = (7, 6), palette: Optional[list] = None, title: Optional[str] = None, raster: Optional[bool] = None, split_by: Optional[str] = None, ncol: Optional[int] = None, repel: bool = False) -> 'Figure'
Plot cells in a reduced-dimension embedding coloured by identity.
Mirrors R's DimPlot(pbmc, reduction = "umap", label = TRUE).
Parameters:
-
reduction(str, default:'umap') –which reduction to use ("umap", "pca", …)
-
group_by(Optional[str], default:None) –metadata column for colouring (default: active idents). A categorical column is drawn in its category order, as R draws a factor's levels.
-
label(bool, default:True) –label each group at the median of its cells' coordinates, in each panel, as Seurat's
LabelClustersdoes -
label_size(Optional[float], default:None) –font size for the group labels (default: scales with the theme)
-
repel(bool, default:False) –move the labels so that none overlaps another, runs off the panel, or covers another group's median or more than half of any group's cells, keeping each as close to its median as that allows. A line joins a label to its group when it has moved away, going round the other labels where there is room. Seurat's
repeldoes this with ggrepel, which starts from random jitter; this placement is deterministic. It is worked out again whenever the figure is drawn, so it still holds after the figure is resized or saved at another size. -
pt_size(float, default:4.0) –scatter point size
-
alpha(float, default:0.7) –point transparency
-
raster(Optional[bool], default:None) –draw the cells as a raster layer instead of vector paths.
None(default) rasterises above 100,000 cells, the same rule and threshold Seurat uses. -
split_by(Optional[str], default:None) –metadata column to facet on — one panel per level, holding only that level's cells. Colours and axis limits are shared across panels, matching
facet_wrap's fixed scales: the panels are only comparable if a position and a colour mean the same thing in each. -
ncol(Optional[int], default:None) –panels per row when
split_byis set.
Source code in truecell/plotting.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 | |
feature_plot
¶
feature_plot(obj, features: Union[str, list[str]], reduction: str = 'umap', assay: Optional[str] = None, layer: Optional[str] = None, ncol: Optional[int] = None, order: bool = True, min_cutoff: float | str | None = None, max_cutoff: float | str | None = None, colormap: str = 'YlOrRd', pt_size: float = 3.0, figsize: Optional[tuple] = None, raster: Optional[bool] = None, split_by: Optional[str] = None) -> 'Figure'
Visualise feature expression on a dimensionality reduction embedding.
Mirrors R's FeaturePlot(pbmc, features = c("MS4A1", "LYZ")).
Parameters:
-
features(Union[str, list[str]]) –gene name(s) or metadata column(s) to plot
-
reduction(str, default:'umap') –which reduction to use ("umap", "pca", …)
-
order(bool, default:True) –plot cells with highest expression on top
-
min_cutoff(float | str | None, default:None) –expression at or below this is drawn as not expressed (grey). A number, or a quantile such as
"q05"; default 0. -
max_cutoff(float | str | None, default:None) –expression above this takes the top of the colour scale. A number, or a quantile such as
"q95"; default the feature's maximum. As in Seurat, a quantile is taken over the cells that express the feature (values above zero), so"q05"/"q95"suit a gene detected in a few percent of cells. If the two cutoffs leave the scale no range, a warning says so and the feature is drawn over its own range instead. -
colormap(str, default:'YlOrRd') –matplotlib colormap name for expression
-
pt_size(float, default:3.0) –scatter point size
-
raster(Optional[bool], default:None) –draw the cells as a raster layer instead of vector paths.
None(default) rasterises above 100,000 cells, the same rule and threshold Seurat uses. -
split_by(Optional[str], default:None) –metadata column to facet on. Lays out one row per feature and one column per level, as Seurat does. The colour scale and the axis limits are computed once per feature over all cells and shared down the row, so a colour means the same expression in every panel — per-panel scales would make the levels look alike however different they are.
Source code in truecell/plotting.py
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 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 | |
vln_plot
¶
vln_plot(obj, features: Union[str, list[str]], group_by: Optional[str] = None, assay: Optional[str] = None, layer: Optional[str] = None, pt_size: Optional[float] = None, ncol: Optional[int] = None, figsize: Optional[tuple] = None, palette: Optional[list] = None, violin_width: float = 0.8, jitter_seed: Optional[int] = 0, raster: Optional[bool] = None, split_by: Optional[str] = None) -> 'Figure'
Violin plot of feature expression per cluster/identity.
Mirrors R's VlnPlot(pbmc, features = c("LYZ", "CD3D")), including the
three things that make a Seurat violin the shape it is: the density is
trimmed to the observed range, smoothed with R's nrd0 bandwidth, and
scaled so every group reaches the same maximum width.
Parameters:
-
obj–Truecell object
-
features(Union[str, list[str]]) –gene name(s) or metadata column(s)
-
group_by(Optional[str], default:None) –metadata column used for grouping (default: active idents). A categorical column is drawn in its category order, as R draws a factor's levels.
-
pt_size(Optional[float], default:None) –marker area for the jittered points, in matplotlib's units.
None(default) follows Seurat'sAutoPointSize, which shows points and shrinks them as the cell count grows;0omits them. -
ncol(Optional[int], default:None) –number of columns in subplot grid
-
figsize(Optional[tuple], default:None) –figure size in inches; auto-computed if None
-
palette(Optional[list], default:None) –list of colours per group
-
violin_width(float, default:0.8) –width of a full violin in x-axis units
-
jitter_seed(Optional[int], default:0) –seed for the point jitter, so a figure redraws identically.
Nonedraws fresh jitter each call. -
raster(Optional[bool], default:None) –rasterise the jittered points.
Nonefollows the same 100,000-point rule as the other plots. -
split_by(Optional[str], default:None) –metadata column to split each group by. Matches Seurat's
split.plot = FALSEdefault — the levels are dodged side-by-side within each group's x position and coloured by level, not drawn as one split violin and not faceted into separate panels.
Source code in truecell/plotting.py
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 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 | |
ridge_plot
¶
ridge_plot(obj, features: Union[str, list[str]], group_by: Optional[str] = None, assay: Optional[str] = None, layer: Optional[str] = None, ncol: Optional[int] = None, figsize: Optional[tuple] = None, palette: Optional[list] = None) -> 'Figure'
Ridgeline (joy) plots of feature expression per group.
Mirrors R's RidgePlot(pbmc, features = c("LYZ", "CD3D")).
Requires scipy for KDE smoothing.
Group labels sit outside the axes, so a panel needs more width than its axes
alone. The default figsize allows for that. Passing one that is too small
makes matplotlib draw the panels over each other instead of erroring, so an
explicit figsize is checked and warns if the panels end up colliding.
Source code in truecell/plotting.py
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 | |
dot_plot
¶
dot_plot(obj, features: Union[str, list[str]], group_by: Optional[str] = None, assay: Optional[str] = None, layer: str = 'data', cols: tuple = ('lightgrey', 'blue'), col_min: float = -2.5, col_max: float = 2.5, dot_min: float = 0.0, dot_scale: float = 6.0, scale: bool = True, figsize: Optional[tuple] = None) -> 'Figure'
Dot plot of feature expression across groups.
Mirrors R's DotPlot(pbmc, features = c("LYZ", "CD3D")). For each
(feature, group) the dot size encodes the fraction of cells in the group
expressing the feature (counts > 0) and the colour encodes the average
expression (z-scored across groups when scale=True, matching Seurat).
Parameters:
-
features(Union[str, list[str]]) –gene name(s) to plot (x-axis).
-
group_by(Optional[str], default:None) –metadata column for grouping (default: active idents, y-axis). A categorical column is drawn in its category order, as R draws a factor's levels.
-
scale(bool, default:True) –z-score each feature's average expression across groups.
-
col_min(float, default:-2.5) – -
dot_min(float, default:0.0) – -
dot_scale(float, default:6.0) –
Source code in truecell/plotting.py
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 | |
feature_scatter
¶
feature_scatter(obj, feature1: str, feature2: str, group_by: Optional[str] = None, assay: Optional[str] = None, layer: Optional[str] = None, pt_size: float = 4.0, alpha: float = 0.6, figsize: tuple = (6, 5), palette: Optional[list] = None, raster: Optional[bool] = None) -> 'Figure'
Scatter plot of two features coloured by identity.
Mirrors R's FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "percent.mt").
Parameters:
-
feature1(str) – -
group_by(column for colouring; default: active idents. A categorical column is, default:None) –coloured in its category order, as R colours a factor's levels. -
raster(Optional[bool], default:None) –``None`` (default) rasterises above 100,000 cells.
Source code in truecell/plotting.py
elbow_plot
¶
Rank principal components by standard deviation.
Mirrors R's ElbowPlot(pbmc).
Parameters:
-
reduction(str, default:'pca') –name of the PCA-like reduction
-
ndims(int, default:20) –number of PCs to show
Source code in truecell/plotting.py
variable_feature_plot
¶
variable_feature_plot(obj, assay: Optional[str] = None, log: bool = True, label: bool = True, n_label: int = 10, figsize: tuple = (9, 5), raster: Optional[bool] = None, repel: bool = False) -> 'Figure'
Plot mean expression vs dispersion and highlight variable features.
Mirrors R's VariableFeaturePlot(pbmc).
Parameters:
-
log(bool, default:True) –use log10 axes
-
label(bool, default:True) –annotate the top n_label HVGs by name
-
n_label(int, default:10) –number of top HVGs to label
-
raster(Optional[bool], default:None) –draw the points as a raster layer instead of vector paths.
None(default) rasterises above 100,000 points. The points here are genes, not cells, so the default rarely engages. -
repel(bool, default:False) –move the names apart and off every named point, as Seurat's
LabelPoints(repel = TRUE)does; a line joins a name that has moved away to its point. Without it each name sits just above and to the right of its point, and names in a dense region overlap.
Source code in truecell/plotting.py
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 | |
viz_dim_loadings
¶
viz_dim_loadings(obj, reduction: str = 'pca', dims: Union[int, list[int]] = [1, 2], n_features: int = 15, ncol: Optional[int] = None, figsize: Optional[tuple] = None) -> 'Figure'
Horizontal bar charts of top positive and negative loading genes per PC.
Mirrors R's VizDimLoadings(pbmc, dims = 1:2, reduction = "pca").
Parameters:
-
dims(Union[int, list[int]], default:[1, 2]) –PC indices (1-based) to visualise
-
n_features(int, default:15) –number of top genes to show per direction (positive + negative)
Source code in truecell/plotting.py
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 | |
dim_heatmap
¶
dim_heatmap(obj, reduction: str = 'pca', dims: Union[int, list[int]] = 1, cells: int | None = None, balanced: bool = True, ncol: Optional[int] = None, figsize: Optional[tuple] = None) -> 'Figure'
Heatmap of gene loadings for selected principal components.
Shows cells in order of their score, highest on the left, against the top
loading genes for each PC — mirrors R's DimHeatmap(pbmc, dims = 1:6).
Parameters:
-
dims(Union[int, list[int]], default:1) –PC index (1-based int) or list of indices
-
cells(int | None, default:None) –how many cells to show per PC, picked the way DimHeatmap picks them.
None(the default) shows every cell, as Seurat does; the PBMC 3k vignette passes 500. -
balanced(bool, default:True) –if True, take equal numbers from both extremes of the PC score
Source code in truecell/plotting.py
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 | |
do_heatmap
¶
do_heatmap(obj, features: list[str], group_by: Optional[str] = None, assay: Optional[str] = None, layer: str = 'scale.data', label: bool = True, cells: Optional[list[str]] = None, figsize: Optional[tuple] = None, palette: Optional[list] = None) -> 'Figure'
Expression heatmap of selected genes across cells, sorted by cluster.
Mirrors R's DoHeatmap(pbmc, features = top10$gene).
Parameters:
-
features(list[str]) –list of gene names to show as rows
-
group_by(Optional[str], default:None) –column used to sort and colour cells (default: active idents). A categorical column is ordered by its categories, as R orders a factor's levels.
-
layer(str, default:'scale.data') –which data layer to use (default: "scale.data")
-
label(bool, default:True) –annotate cluster boundaries with group names
-
cells(Optional[list[str]], default:None) –restrict to these cells and show them in this exact order, rather than the default group-sorted order. Mirrors R's
cellsargument;mixscape_heatmapuses it to order cells by knockout probability.
Source code in truecell/plotting.py
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 | |
Spatial¶
image_dim_plot
¶
image_dim_plot(obj, group_by: Optional[str] = None, image: Optional[Union[str, list]] = None, size: float = 1.0, cols: Optional[dict] = None, ncol: Optional[int] = None, flip_y: bool = True, figsize: Optional[tuple] = None, raster: Optional[bool] = None) -> 'Figure'
Plot cell centroids in physical space, coloured by a grouping variable.
Matplotlib equivalent of Seurat's ImageDimPlot — drawn directly from
centroids (obj.get_tissue_coordinates), so it is immune to the
ggplot2-4.x blank-render issue. One panel per image.
Parameters:
-
group_by(Optional[str], default:None) –metadata column (default: active idents) for point colour.
-
image(Optional[Union[str, list]], default:None) –image name(s) to draw (default: all).
-
cols(Optional[dict], default:None) –optional
{group: colour}mapping. -
flip_y(bool, default:True) –invert the y-axis so images match Seurat's orientation.
-
raster(Optional[bool], default:None) –draw the points as a raster layer instead of vector paths.
None(default) rasterises above 100,000 points.
Source code in truecell/plotting.py
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 | |
image_feature_plot
¶
image_feature_plot(obj, feature: str, image: Optional[Union[str, list]] = None, size: float = 1.0, cmap: str = 'viridis', assay: Optional[str] = None, layer: Optional[str] = None, ncol: Optional[int] = None, flip_y: bool = True, figsize: Optional[tuple] = None, raster: Optional[bool] = None) -> 'Figure'
Plot cell centroids in physical space, coloured by a feature's expression.
Matplotlib equivalent of Seurat's ImageFeaturePlot. One panel per image.
raster : draw the points as a raster layer instead of vector paths.
None (default) rasterises above 100,000 points.
Source code in truecell/plotting.py
spatial_dim_plot
¶
spatial_dim_plot(obj, group_by: Optional[str] = None, image: Optional[Union[str, list]] = None, cols: Optional[dict] = None, pt_size_factor: float = 1.6, size: float = 12.0, alpha: float = 1.0, image_alpha: float = 1.0, resolution: Optional[str] = None, crop: bool = True, ncol: Optional[int] = None, figsize: Optional[tuple] = None, raster: Optional[bool] = None) -> 'Figure'
Plot spots on the tissue image, coloured by a grouping variable.
Matplotlib equivalent of Seurat's SpatialDimPlot. Each panel draws the
H&E photo held by a VisiumV2 image and
overlays the spots at their true diameter. An image slot with no photo (a
plain FOV, or a Visium bundle loaded with image=False) degrades to a
bare scatter of the same spots — the plot still works, it just has no
tissue underneath.
Parameters:
-
group_by(Optional[str], default:None) –metadata column (default: active idents) for spot colour.
-
image(Optional[Union[str, list]], default:None) –image name(s) to draw (default: all).
-
cols(Optional[dict], default:None) –optional
{group: colour}mapping. -
pt_size_factor(float, default:1.6) –scales the spots relative to their real diameter, as in Seurat. Ignored when the image has no
scalefactors_json.json, in which casesize(a plain scatter point size) applies instead. -
image_alpha(float, default:1.0) –opacity of the tissue photo — drop it to make spots pop.
-
resolution(Optional[str], default:None) –"hires"/"lowres"; defaults to whatever was loaded. -
crop(bool, default:True) –zoom to the spots rather than showing the whole slide.
-
raster(Optional[bool], default:None) –draw the points as a raster layer instead of vector paths.
None(default) rasterises above 100,000 points.
Source code in truecell/plotting.py
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 | |
spatial_feature_plot
¶
spatial_feature_plot(obj, feature: str, image: Optional[Union[str, list]] = None, cmap: str = 'viridis', pt_size_factor: float = 1.6, size: float = 12.0, alpha: float = 1.0, image_alpha: float = 1.0, assay: Optional[str] = None, layer: Optional[str] = None, resolution: Optional[str] = None, crop: bool = True, ncol: Optional[int] = None, figsize: Optional[tuple] = None, raster: Optional[bool] = None) -> 'Figure'
Plot spots on the tissue image, coloured by a feature's expression.
Matplotlib equivalent of Seurat's SpatialFeaturePlot. Behaves exactly
like spatial_dim_plot — same tissue background, same true-to-scale
spots, same fallback when no image is stored — but colours the spots by a
continuous value on a shared scale across panels.
raster : draw the points as a raster layer instead of vector paths.
None (default) rasterises above 100,000 points.
Source code in truecell/plotting.py
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 | |
Mixscape diagnostics¶
plot_perturb_score
¶
plot_perturb_score(obj, target_gene_ident: str, target_gene_class: str = 'gene', mixscape_class: str = 'mixscape_class', col: str = 'orange2', split_by: Optional[str] = None, before_mixscape: bool = False, prtb_type: str = 'KO', assay: str = 'PRTB', seed: int = 0, figsize: Optional[tuple] = None) -> 'Figure'
Density of one target gene's perturbation scores (Seurat's PlotPerturbScore).
Mirrors PlotPerturbScore(object, target.gene.ident = "IFNGR2",
target.gene.class = "gene", mixscape.class = "mixscape_class"). This is the
diagnostic that shows why mixscape split a guide the way it did: the score
is each cell's projection onto the gene's perturbation axis, and the plot
overlays the NT control density against the guide's own. A guide with a real
effect is bimodal — one lobe sitting on the NT curve (the escapers) and one
shifted away from it (the knockouts) — which is exactly the structure the
mixture model is asked to find.
Two views, per R's before.mixscape:
before_mixscape=False(default) — colour bymixscape_class, i.e. after the call: NT,"<gene> NP", and"<gene> KO"get their own curves, so you see where mixscape actually drew the line.before_mixscape=True— colour by the raw guide label only, the view you would have had without mixscape: NT against the whole guide population.
Cells are also drawn as a jittered strip — controls above the axis, the target gene below — so single-cell density is visible where the curves overlap.
Parameters:
-
target_gene_ident(str) –the target gene to plot (must be one mixscape tested).
-
target_gene_class(str, default:'gene') –metadata column of per-cell guide class (default
"gene"). -
mixscape_class(str, default:'mixscape_class') –metadata column of mixscape classifications.
-
col(str, default:'orange2') –colour for the target gene / knockout class. Controls and non-perturbed cells are fixed greys, as in R.
-
split_by(Optional[str], default:None) –metadata column to facet on, for screens spanning more than one cell type.
-
before_mixscape(bool, default:False) –colour by raw guide label instead of the mixscape call.
-
prtb_type(str, default:'KO') –perturbation label used by
run_mixscape("KO"). -
assay(str, default:'PRTB') –perturbation-signature assay the scores were stored under.
-
seed(int, default:0) –random state for the jitter strip (determinism).
Returns:
-
matplotlib Figure–
Source code in truecell/plotting.py
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 | |
mixscape_heatmap
¶
mixscape_heatmap(obj, ident_1: str, ident_2: Optional[str] = None, balanced: bool = True, logfc_threshold: float = 0.25, assay: str = 'RNA', max_genes: int = 100, test_use: str = 'wilcox', max_cells_group: Optional[int] = None, order_by_prob: bool = True, mixscape_class: str = 'mixscape_class', prtb_type: str = 'KO', fc_name: str = 'avg_log2FC', pval_cutoff: float = 0.05, seed: int = 0, **kwargs) -> 'Figure'
DE heatmap with cells ordered by knockout probability (MixscapeHeatmap).
Mirrors MixscapeHeatmap(object, ident.1 = "NT", ident.2 = "IFNGR2 KO",
balanced = TRUE, max.genes = 20). Where plot_perturb_score shows the
one-dimensional score mixscape split on, this shows the genes underneath it:
the DE genes between the two classes, with every cell ordered left-to-right by
its knockout posterior. Read together with the class colour bar, a clean
screen shows the expression block turning on in step with the probability —
the escapers at the low-probability end still looking like control.
ident_1 / ident_2 are mixscape_class levels (e.g. "NT",
"IFNGR2 KO", "IFNGR2 NP"), which run_mixscape also leaves as
the active identity.
Parameters:
-
ident_1(str) –the two classes to contrast (
ident_2=None→ all others). -
ident_2(str) –the two classes to contrast (
ident_2=None→ all others). -
balanced(bool, default:True) –take up to
max_genesgenes from each direction of the fold change; otherwise only up-regulated ones. -
max_genes(int, default:100) –cap on DE genes per direction.
-
max_cells_group(Optional[int], default:None) –downsample each class to this many cells.
-
order_by_prob(bool, default:True) –order cells by
<mixscape_class>_p_<type>, highest first. If False, cells are shuffled (as R does). -
mixscape_class(str, default:'mixscape_class') –metadata column of mixscape classifications; also names the posterior column read for the ordering.
-
prtb_type(str, default:'KO') –perturbation label used by
run_mixscape("KO"). -
fc_name(str, default:'avg_log2FC') –fold-change column in the DE table (
"avg_log2FC"). -
seed(int, default:0) –random state for downsampling / shuffling (determinism).
-
**kwargs–forwarded to
do_heatmap.
Returns:
-
matplotlib Figure–
Source code in truecell/plotting.py
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 | |