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) -> '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)
-
label(bool, default:True) –add centroid labels for each group
-
label_size(Optional[float], default:None) –font size for centroid labels (default: scales with the theme)
-
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
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 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 | |
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: Optional[float] = None, max_cutoff: Optional[float] = 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(Optional[float], default:None) –clip expression below this percentile (e.g. "q05")
-
max_cutoff(Optional[float], default:None) –clip expression above this percentile
-
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
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 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 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 | |
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)
-
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
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 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 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 | |
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
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 1528 1529 1530 1531 1532 1533 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 | |
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).
-
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
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 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 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 | |
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, default:None) – -
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) -> '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.
Source code in truecell/plotting.py
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 | |
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
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 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 | |
dim_heatmap
¶
dim_heatmap(obj, reduction: str = 'pca', dims: Union[int, list[int]] = 1, cells: int = 500, balanced: bool = True, ncol: Optional[int] = None, figsize: Optional[tuple] = None) -> 'Figure'
Heatmap of gene loadings for selected principal components.
Shows the most extreme cells (highest / lowest scores) and 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, default:500) –number of extreme cells to show per PC
-
balanced(bool, default:True) –if True, take equal numbers from both extremes of the PC score
Source code in truecell/plotting.py
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 1315 1316 1317 1318 1319 1320 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 | |
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)
-
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
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 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 | |
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
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 | |
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
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 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 | |
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
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 | |
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
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 2209 2210 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 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 | |
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
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 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 | |