From 189af8b904f65b821dd3ab246867128b71c69648 Mon Sep 17 00:00:00 2001 From: Blaise Li <blaise.li__git@nsup.org> Date: Tue, 5 Apr 2022 23:19:51 +0200 Subject: [PATCH] Fix violin order. Legend issues. --- libcodonusage/__init__.py | 2 +- libcodonusage/libcodonusage.py | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/libcodonusage/__init__.py b/libcodonusage/__init__.py index 93cf75e..7ea81e1 100644 --- a/libcodonusage/__init__.py +++ b/libcodonusage/__init__.py @@ -1,6 +1,6 @@ __copyright__ = "Copyright (C) 2022 Blaise Li" __licence__ = "GNU GPLv3" -__version__ = "0.19" +__version__ = "0.20" from .libcodonusage import ( aa2colour, aa_usage, diff --git a/libcodonusage/libcodonusage.py b/libcodonusage/libcodonusage.py index 8f1e0a6..fbf886e 100644 --- a/libcodonusage/libcodonusage.py +++ b/libcodonusage/libcodonusage.py @@ -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 -- GitLab