Skip to content

Commit

Permalink
fixed several issues related to plotting paga
Browse files Browse the repository at this point in the history
  • Loading branch information
falexwolf committed Oct 5, 2018
1 parent 641af42 commit 1e6fb98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
39 changes: 14 additions & 25 deletions scanpy/plotting/tools/paga.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
def paga_compare(
adata,
basis=None,
edges=False,
edges=True,
color=None,
alpha=None,
groups=None,
components=None,
projection='2d',
legend_loc='on data',
legend_fontsize=None,
legend_fontweight=None,
legend_fontweight='bold',
color_map=None,
palette=None,
frameon=None,
Expand Down Expand Up @@ -93,7 +93,6 @@ def paga_compare(
color_map=color_map,
palette=palette,
frameon=frameon,
right_margin=None,
size=size,
title=title,
ax=axs[0],
Expand All @@ -111,43 +110,32 @@ def paga_compare(
def _paga_scatter(
adata,
basis='tsne',
edges=None,
edges=True,
color=None,
alpha=None,
groups=None,
components=None,
projection='2d',
legend_loc='right margin',
legend_fontsize=None,
legend_fontweight=None,
legend_fontweight='bold',
color_map=None,
palette=None,
frameon=None,
size=None,
title=None,
right_margin=None,
show=None,
save=None,
ax=None):
if color is None:
color = [adata.uns['paga']['groups']]
if not isinstance(color, list): color = [color]
kwds = {}
if 'draw_graph' in basis:
from .scatterplots import draw_graph
scatter_func = draw_graph
kwds['edges'] = True if edges is None else edges
elif basis == 'umap':
from .scatterplots import umap
scatter_func = umap
kwds['edges'] = True if edges is None else edges
else:
from ..anndata import scatter
scatter_func = scatter
kwds['basis'] = basis
axs = scatter_func(
from .scatterplots import plot_scatter
axs = plot_scatter(
adata,
basis=basis,
color=color,
edges=edges,
alpha=alpha,
groups=groups,
components=components,
Expand All @@ -157,12 +145,10 @@ def _paga_scatter(
color_map=color_map,
palette=palette,
frameon=frameon,
right_margin=right_margin,
size=size,
title=title,
ax=ax,
show=False,
**kwds)
show=False)
utils.savefig_or_show('paga_' + basis, show=show, save=save)
if show == False: return axs

Expand Down Expand Up @@ -197,7 +183,7 @@ def paga(
cax=None,
colorbar=None,
cb_kwds={},
frameon=True,
frameon=None,
add_pos=True,
export_to_gexf=False,
use_raw=True,
Expand Down Expand Up @@ -293,7 +279,7 @@ def paga(
Add the positions to `adata.uns['paga']`.
title : `str`, optional (default: `None`)
Provide a title.
frameon : `bool`, optional (default: `False`)
frameon : `bool`, optional (default: `None`)
Draw a frame around the PAGA graph.
show : `bool`, optional (default: `None`)
Show the plot, do not return axis.
Expand Down Expand Up @@ -332,6 +318,9 @@ def paga(
if ((isinstance(colors, Iterable) and len(colors) == len(adata.obs[groups_key].cat.categories))
or colors is None or isinstance(colors, str)):
colors = [colors]

if frameon is None:
frameon = settings._frameon

# labels is a list that contains no lists
if ((isinstance(labels, Iterable) and len(labels) == len(adata.obs[groups_key].cat.categories))
Expand Down
7 changes: 6 additions & 1 deletion scanpy/plotting/tools/scatterplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ def _add_legend_or_colorbar(adata, ax, cax, categorical, value_to_plot, legend_l

if legend_loc == 'on data':
# identify centroids to put labels
for label in categories:
all_pos = np.zeros((len(categories), 2))
for ilabel, label in enumerate(categories):
_scatter = scatter_array[adata.obs[value_to_plot] == label, :]
x_pos, y_pos = np.median(_scatter, axis=0)

Expand All @@ -463,6 +464,10 @@ def _add_legend_or_colorbar(adata, ax, cax, categorical, value_to_plot, legend_l
verticalalignment='center',
horizontalalignment='center',
fontsize=legend_fontsize)

all_pos[ilabel] = [x_pos, y_pos]
# this is temporary storage for access by other tools
utils._tmp_cluster_pos = all_pos
else:
# add colorbar to figure
pl.colorbar(cax, ax=ax, pad=0.01, fraction=0.08, aspect=30)
Expand Down

1 comment on commit 1e6fb98

@falexwolf
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got to add tests for all the paga plotting stuff. I'll do that soon, likely as a new notebook. I got to prettify the current ones, again: https://github.com/theislab/paga... But could be interesting for you, too. You get very nice layouts with it.

Please sign in to comment.