|
| 1 | +from __future__ import annotations |
| 2 | +from typing import TYPE_CHECKING |
| 3 | + |
| 4 | +if TYPE_CHECKING: |
| 5 | + from typing import Optional, List |
| 6 | + from anndata import AnnData |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +from scanpy.plotting._tools.scatterplots import ( |
| 13 | + _color_vector, |
| 14 | + _get_color_source_vector, |
| 15 | + _add_categorical_legend, |
| 16 | + _get_palette, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def ternary( |
| 21 | + adata: AnnData, |
| 22 | + color: Optional[str] = None, |
| 23 | + ax=None, |
| 24 | + labels: Optional[List[str]] = None, |
| 25 | + show: bool = True, |
| 26 | + colorbar_loc: Optional[str] = None, |
| 27 | + legend_loc: Optional[str] = None, |
| 28 | + legend_fontweight: Optional[str] = None, |
| 29 | + legend_fontsize: Optional[int] = None, |
| 30 | + legend_fontoutline: Optional[str] = None, |
| 31 | + na_in_legend: Optional[bool] = None, |
| 32 | + **kwargs, |
| 33 | +): |
| 34 | + try: |
| 35 | + import mpltern |
| 36 | + except ImportError: |
| 37 | + raise ImportError( |
| 38 | + "mpltern is not installed. Please install it with `pip install mpltern`" |
| 39 | + ) |
| 40 | + if adata.X.shape[1] != 3: |
| 41 | + raise ValueError("Ternary plots requires adata object with 3 samples (columns)") |
| 42 | + if ax is None: |
| 43 | + ax = plt.subplot(projection="ternary") |
| 44 | + if labels is None: |
| 45 | + labels = adata.var_names |
| 46 | + |
| 47 | + csv = _get_color_source_vector(adata, color) |
| 48 | + |
| 49 | + cv, color_type = _color_vector(adata, values_key=color, values=csv, palette=None) |
| 50 | + |
| 51 | + # Make sure that nan values are plottted below the other points |
| 52 | + nan_mask = np.isnan(csv) if isinstance(csv, np.ndarray) else csv.isna() |
| 53 | + if nan_mask.any(): |
| 54 | + nan_points = adata[nan_mask].X |
| 55 | + ax.scatter( |
| 56 | + nan_points[:, 0], |
| 57 | + nan_points[:, 1], |
| 58 | + nan_points[:, 2], |
| 59 | + c=cv[nan_mask], |
| 60 | + **kwargs, |
| 61 | + zorder=0, |
| 62 | + ) |
| 63 | + cax = ax.scatter( |
| 64 | + adata.X[~nan_mask, 0], |
| 65 | + adata.X[~nan_mask, 1], |
| 66 | + adata.X[~nan_mask, 2], |
| 67 | + zorder=1, |
| 68 | + c=cv[~nan_mask], |
| 69 | + **kwargs, |
| 70 | + ) |
| 71 | + ax.taxis.set_label_position("tick1") |
| 72 | + ax.raxis.set_label_position("tick1") |
| 73 | + ax.laxis.set_label_position("tick1") |
| 74 | + ax.set_tlabel(labels[0]) |
| 75 | + ax.set_llabel(labels[1]) |
| 76 | + ax.set_rlabel(labels[2]) |
| 77 | + |
| 78 | + if color_type == "cat": |
| 79 | + _add_categorical_legend( |
| 80 | + ax, |
| 81 | + csv, |
| 82 | + palette=_get_palette(adata, color), |
| 83 | + scatter_array=None, |
| 84 | + legend_loc=legend_loc, |
| 85 | + legend_fontweight=legend_fontweight, |
| 86 | + legend_fontsize=legend_fontsize, |
| 87 | + legend_fontoutline=legend_fontoutline, |
| 88 | + na_color="grey", |
| 89 | + na_in_legend=na_in_legend, |
| 90 | + multi_panel=False, |
| 91 | + ) |
| 92 | + elif colorbar_loc is not None: |
| 93 | + plt.colorbar( |
| 94 | + cax, ax=ax, pad=0.01, fraction=0.08, aspect=30, location=colorbar_loc |
| 95 | + ) |
| 96 | + if show: |
| 97 | + plt.show() |
| 98 | + return ax |
0 commit comments