# Plot graph structures

This functions show how to plot graph structures, such as the transition matrix.

```import cellrank as cr

import numpy as np

```

Out:

```AnnData object with n_obs × n_vars = 2531 × 2000
obs: 'day', 'proliferation', 'G2M_score', 'S_score', 'phase', 'clusters_coarse', 'clusters', 'clusters_fine', 'louvain_Alpha', 'louvain_Beta', 'initial_size_unspliced', 'initial_size_spliced', 'initial_size', 'n_counts', 'velocity_self_transition', 'dpt_pseudotime'
var: 'highly_variable_genes', 'gene_count_corr', 'means', 'dispersions', 'dispersions_norm', 'fit_r2', 'fit_alpha', 'fit_beta', 'fit_gamma', 'fit_t_', 'fit_scaling', 'fit_std_u', 'fit_std_s', 'fit_likelihood', 'fit_u0', 'fit_s0', 'fit_pval_steady', 'fit_steady_u', 'fit_steady_s', 'fit_variance', 'fit_alignment_scaling', 'velocity_genes'
uns: 'clusters_colors', 'clusters_fine_colors', 'diffmap_evals', 'iroot', 'louvain_Alpha_colors', 'louvain_Beta_colors', 'neighbors', 'pca', 'recover_dynamics', 'velocity_graph', 'velocity_graph_neg', 'velocity_params'
obsm: 'X_diffmap', 'X_pca', 'X_umap', 'velocity_umap'
varm: 'PCs', 'loss'
layers: 'Ms', 'Mu', 'fit_t', 'fit_tau', 'fit_tau_', 'spliced', 'unspliced', 'velocity', 'velocity_u'
obsp: 'connectivities', 'distances'
```

First, we create a forward transition matrix using the high-level pipeline.

```cr.tl.transition_matrix(
)
```

Out:

```/home/docs/checkouts/readthedocs.org/user_builds/cellrank/checkouts/latest/examples/plotting/plot_graph.py:18: DeprecationWarning: `cellrank.tl.transition_matrix` will be removed in version `2.0`. Please use the `cellrank.kernels` or `cellrank.estimators` interface instead.
cr.tl.transition_matrix(

(0.8 * VelocityKernel + 0.2 * ConnectivityKernel)
```

We can now plot the transition matrix. Below we don’t show any arrows, which dramatically speeds up the plotting.

```cr.pl.graph(
"T_fwd",
edge_alpha=0.1,
node_size=5,
arrows=False,
keys="clusters",
keylocs="obs",
)
``` Out:

```/home/docs/checkouts/readthedocs.org/user_builds/cellrank/checkouts/latest/cellrank/pl/_graph.py:328: DeprecationWarning:

The scipy.sparse array containers will be used instead of matrices
in Networkx 3.0. Use `from_scipy_sparse_array` instead.
nx.from_scipy_sparse_matrix(gdata, create_using=nx.DiGraph)
```

To further illustrate the functionalities, let us only consider the ‘Delta’ cluster. We can also filter the edges by their weights, as shown below. Only transitions with probability at least 0.1 are plotted.

```ixs = np.where(adata.obs["clusters"] == "Delta")
cr.pl.graph(adata, "T_fwd", ixs=ixs, arrows=True, node_size=200, filter_edges=(0.1, 1))
``` Out:

```/home/docs/checkouts/readthedocs.org/user_builds/cellrank/checkouts/latest/cellrank/pl/_graph.py:328: DeprecationWarning:

The scipy.sparse array containers will be used instead of matrices
in Networkx 3.0. Use `from_scipy_sparse_array` instead.
nx.from_scipy_sparse_matrix(gdata, create_using=nx.DiGraph)
```

Lastly, we can visualize different edge aggregations, such as minimum or maximum. Here we take at most 3 outgoing edges restricted to `ixs` for each node in descending order and color the nodes by the maximum outgoing weights. Aggregated values are always computed before any filtering happens, such as shown above.

Here we also specify `edge_reductions_restrict_to_ixs` (by default, it is the same as `ixs`) that computes the statistic between the cells marked with `ixs` and `edge_reductions_restrict_to_ixs`.

Below we compare the maximum transition from each of the “Delta” cells to any of the “Beta” cells.

```cr.pl.graph(
"T_fwd",
ixs=ixs,
edge_alpha=0.5,
node_size=200,
keys="outgoing",
arrows=False,
top_n_edges=(3, False, "outgoing"),
title="outgoing to Beta",
edge_reductions=np.max,
)
``` Out:

```/home/docs/checkouts/readthedocs.org/user_builds/cellrank/checkouts/latest/cellrank/pl/_graph.py:328: DeprecationWarning:

The scipy.sparse array containers will be used instead of matrices
in Networkx 3.0. Use `from_scipy_sparse_array` instead.
nx.from_scipy_sparse_matrix(gdata, create_using=nx.DiGraph)
```

Total running time of the script: ( 0 minutes 17.085 seconds)

Estimated memory usage: 406 MB

Gallery generated by Sphinx-Gallery
{"ft_lang_label":"__label__en","ft_lang_prob":0.5094364,"math_prob":0.5603928,"size":4340,"snap":"2022-05-2022-21","text_gpt3_token_len":1187,"char_repetition_ratio":0.11185424,"word_repetition_ratio":0.12441315,"special_character_ratio":0.25990784,"punctuation_ratio":0.26737967,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98666865,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,3,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-18T03:24:42Z\",\"WARC-Record-ID\":\"<urn:uuid:eea8c6d2-3b0c-45b6-8c63-e4cc8baf285a>\",\"Content-Length\":\"26173\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:197163de-50cd-49a3-93d1-2a9b9e2b2faa>\",\"WARC-Concurrent-To\":\"<urn:uuid:ec443ec7-da01-4dbc-98ab-c0f3a4e0942b>\",\"WARC-IP-Address\":\"104.17.33.82\",\"WARC-Target-URI\":\"https://cellrank.readthedocs.io/en/latest/auto_examples/plotting/plot_graph.html\",\"WARC-Payload-Digest\":\"sha1:ON7HWNGAEJ7HPOJQ6TSIQVDUB6D272TY\",\"WARC-Block-Digest\":\"sha1:KBJJNJT6TNTTVG4HKWLR2FEWFWK3IUFE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662521041.0_warc_CC-MAIN-20220518021247-20220518051247-00258.warc.gz\"}"}