From 14de21d236929b3b4b6ea6c8a08bdf229e19532a Mon Sep 17 00:00:00 2001 From: Blaise Li <blaise.li__git@nsup.org> Date: Thu, 7 Apr 2022 21:36:22 +0200 Subject: [PATCH] Forward arguments to violin plot. --- libcodonusage/libcodonusage.py | 62 ++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/libcodonusage/libcodonusage.py b/libcodonusage/libcodonusage.py index 252ea02..47bd4b7 100644 --- a/libcodonusage/libcodonusage.py +++ b/libcodonusage/libcodonusage.py @@ -1089,6 +1089,11 @@ def to_long_form(usage_table, ylabel, others=None): See https://pandas.pydata.org/docs/user_guide/10min.html#stack and https://seaborn.pydata.org/generated/seaborn.violinplot.html + + *y_label* is the column header you want to set for the usage values. + + *others* should be a list of extra information you want to extract + from the index (which is expected to be a pandas MultiIndex). """ col_nb_levels = usage_table.columns.nlevels row_nb_levels = usage_table.index.nlevels @@ -1141,7 +1146,9 @@ def format_codon_labels(codons): for (x, y, codon) in map(attrgetter("_x", "_y", "_text"), codons)] -def violin_usage(usage_table, variable, ylabel, hue="aa", axis=None): +def violin_usage( + usage_table, variable, ylabel, + hue="aa", axis=None, **violin_kwargs): """ Plot violin plots of codon usage biases. @@ -1157,15 +1164,20 @@ def violin_usage(usage_table, variable, ylabel, hue="aa", axis=None): palette = aa2colour else: palette = None - # if variable == "aa": - # order = list(columns_by_aa.keys()) - # elif variable == "codon": - # order = [codon for (_, codon) in concat(columns_by_aa.values())] - # else: - # raise ValueError("variable can only be 'aa' or 'codon'.\n") - sns.violinplot(x=variable, y=ylabel, order=variable2order(variable), - hue=hue, palette=palette, dodge=False, - data=long_form, ax=axis, orient="v", scale="count") + if hue in {"aa", "codon"}: + dodge = False + else: + dodge = True + kwargs = { + "x": variable, "y": ylabel, "order": variable2order(variable), + "hue": hue, "palette": palette, "dodge": dodge, + "data": long_form, "ax": axis, "orient": "v", "scale": "count"} + kwargs.update(violin_kwargs) + sns.violinplot(**kwargs) + # sns.violinplot(x=variable, y=ylabel, order=variable2order(variable), + # hue=hue, palette=palette, dodge=dodge, + # data=long_form, ax=axis, orient="v", scale="count", + # **violin_kwargs) if do_legend: plt.legend(bbox_to_anchor=(1.01, 1), borderaxespad=0) if variable == "codon": @@ -1176,7 +1188,9 @@ def violin_usage(usage_table, variable, ylabel, hue="aa", axis=None): return axis -def violin_usage_vertical(usage_table, variable, ylabel, hue="aa", axis=None): +def violin_usage_vertical( + usage_table, variable, ylabel, + hue="aa", axis=None, **violin_kwargs): """ Plot vertically stacked violin plots of codon usage biases. @@ -1189,16 +1203,22 @@ def violin_usage_vertical(usage_table, variable, ylabel, hue="aa", axis=None): palette = aa2colour else: palette = None - # if variable == "aa": - # order = list(columns_by_aa.keys()) - # elif variable == "codon": - # order = [codon for (_, codon) in concat(columns_by_aa.values())] - # else: - # raise ValueError("variable can only be 'aa' or 'codon'.\n") - sns.violinplot( - y=variable, x=ylabel, order=variable2order(variable), - hue=hue, palette=palette, dodge=False, - data=long_form, ax=axis, orient="h", scale="count") + if hue in {"aa", "codon"}: + dodge = False + else: + dodge = True + kwargs = { + "y": variable, "x": ylabel, + "order": variable2order(variable), + "hue": hue, "palette": palette, "dodge": dodge, + "data": long_form, "ax": axis, "orient": "h", "scale": "count"} + kwargs.update(violin_kwargs) + sns.violinplot(**kwargs) + # sns.violinplot( + # y=variable, x=ylabel, order=variable2order(variable), + # hue=hue, palette=palette, dodge=dodge, + # data=long_form, ax=axis, orient="h", scale="count", + # **violin_kwargs) if variable == "codon": ticklabels = format_codon_labels(axis.get_yticklabels()) else: -- GitLab