Skip to content
Snippets Groups Projects
Commit 189af8b9 authored by Blaise Li's avatar Blaise Li
Browse files

Fix violin order. Legend issues.

parent 437a43ec
No related branches found
No related tags found
No related merge requests found
__copyright__ = "Copyright (C) 2022 Blaise Li"
__licence__ = "GNU GPLv3"
__version__ = "0.19"
__version__ = "0.20"
from .libcodonusage import (
aa2colour,
aa_usage,
......
......@@ -19,7 +19,7 @@ import json
from operator import itemgetter
from pathlib import Path
# python3 -m pip install cytoolz
from cytoolz import groupby, unique
from cytoolz import concat, groupby, unique
# To render mardown in a Jupyter notebook on gitlab
from IPython.core.display import display, HTML
# python3 -m pip install markdown
......@@ -575,7 +575,7 @@ methionine (M) and tryptophan (W).
render_md("The following columns contain only NaNs:")
display(all_nan_cols)
render_md("This likely resulted from a division by zero.")
render_md("These columns will be excluded")
render_md("These columns will be excluded.")
return (
standardized_usage_biases.drop(columns=all_nan_cols).fillna(0),
all_nan_cols)
......@@ -1118,15 +1118,24 @@ def violin_usage(usage_table, variable, ylabel, hue="aa", axis=None):
long_form = to_long_form(usage_table, ylabel)
if axis is None:
_, axis = plt.subplots(figsize=(18, 6))
do_legend = True
else:
do_legend = False
if hue == "aa":
palette = aa2colour
else:
palette = None
sns.violinplot(x=variable, y=ylabel,
if variable == "aa":
order = [aa for aa in columns_by_aa.keys()]
elif variable == "codon":
order = [codon for (_, codon) in concat(columns_by_aa.values())]
else:
raise ValueError(f"variable can only be 'aa' or 'codon'.\n")
sns.violinplot(x=variable, y=ylabel, order=order,
hue=hue, palette=palette, dodge=False,
data=long_form, ax=axis, orient="v", scale="count")
plt.legend(bbox_to_anchor=(1.01, 1),
borderaxespad=0)
if do_legend:
plt.legend(bbox_to_anchor=(1.01, 1), borderaxespad=0)
axis.set_xticklabels(axis.get_xticklabels(), rotation=90)
return axis
......@@ -1144,8 +1153,14 @@ def violin_usage_vertical(usage_table, variable, ylabel, hue="aa", axis=None):
palette = aa2colour
else:
palette = None
if variable == "aa":
order = [aa for aa in columns_by_aa.keys()]
elif variable == "codon":
order = [codon for (_, codon) in concat(columns_by_aa.values())]
else:
raise ValueError(f"variable can only be 'aa' or 'codon'.\n")
sns.violinplot(
y=variable, x=ylabel,
y=variable, x=ylabel, order=order,
hue=hue, palette=palette, dodge=False,
data=long_form, ax=axis, orient="h", scale="count")
return axis
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment