From e332b2829851fbd848a2a6d2a75f623f656abd79 Mon Sep 17 00:00:00 2001 From: Blaise Li <blaise.li__git@nsup.org> Date: Thu, 6 Jun 2019 15:41:48 +0200 Subject: [PATCH] Moved optional plotting to static methods. --- plot_scatterplot.py | 148 +++++++++++++++++++++++++++----------------- 1 file changed, 90 insertions(+), 58 deletions(-) diff --git a/plot_scatterplot.py b/plot_scatterplot.py index 94bb79b..9149ae8 100755 --- a/plot_scatterplot.py +++ b/plot_scatterplot.py @@ -83,6 +83,85 @@ class Scatterplot: Intended to be used with *DataFrame.apply*.""" return "_".join(map(str, row.values)) + @staticmethod + def put_quadrants(axis, x_annot_loc, y_annot_loc, line_style): + """Delineate quadrants of absolute fold changes over 2. + + *axis* is the :class:`matplotlib.axes.Axes` on which to work. + + *x_annot_loc* and *y_annot_loc* are the position at which to annotate + the lines delineating the quadrants. + + This works through side effects on *axis*. + """ + axis.axhline(y=1, **line_style) + axis.annotate( + f"y = 1", xy=(x_annot_loc, 1), xycoords="data", + horizontalalignment='left', + verticalalignment='bottom', + size="x-small", + color=line_style["color"]) + axis.axhline(y=-1, **line_style) + axis.annotate( + f"y = -1", xy=(x_annot_loc, -1), xycoords="data", + horizontalalignment='left', + verticalalignment='top', + size="x-small", + color=line_style["color"]) + axis.axvline(x=1, **line_style) + axis.annotate( + f"x = -1", xy=(-1, y_annot_loc), xycoords="data", + horizontalalignment='right', + verticalalignment='bottom', + rotation=90, size="x-small", + color=line_style["color"]) + axis.axvline(x=-1, **line_style) + axis.annotate( + f"x = 1", xy=(1, y_annot_loc), xycoords="data", + horizontalalignment='left', + verticalalignment='bottom', + rotation=90, size="x-small", + color=line_style["color"]) + + @staticmethod + def write_quadrant_counts(axis, + annot_up_up, annot_up_down, + annot_down_up, annot_down_down, + text_color): + """Annotate quadrants of absolute fold changes over 2. + + *axis* is the :class:`matplotlib.axes.Axes` on which to work. + + *annot_up_up*, *annot_up_down*, *annot_down_up* and *annot_down_down* + are the annotations to put in each quadrant. + + This works through side effects on *axis*. + """ + axis.annotate( + annot_up_up, + xy=(0.95, 0.95), xycoords="axes fraction", + size="x-small", color=text_color, + horizontalalignment="right", + verticalalignment="top") + axis.annotate( + annot_up_down, + xy=(0.95, 0.05), xycoords="axes fraction", + size="x-small", color=text_color, + horizontalalignment="right", + verticalalignment="bottom") + axis.annotate( + annot_down_up, + xy=(0.05, 0.95), xycoords="axes fraction", + size="x-small", color=text_color, + horizontalalignment="left", + verticalalignment="top") + axis.annotate( + annot_down_down, + xy=(0.05, 0.05), xycoords="axes fraction", + size="x-small", color=text_color, + horizontalalignment="left", + verticalalignment="bottom") + def __init__(self, x_input_file, y_input_file, @@ -152,7 +231,11 @@ class Scatterplot: if gene_id in chose_from] return [gene_id for gene_id in self.data.query(selector).index] - def plot_maker(self, annotate_folds=True, grouping=None, group2colour=None, **kwargs): + def plot_maker(self, + annotate_folds=True, + grouping=None, + group2colour=None, + **kwargs): """Builds a plotting function that can colour dots based on them belonging to a group defined by *grouping*. If *annotate_folds* is True, lines indicating 2-fold thresholds @@ -188,43 +271,9 @@ class Scatterplot: (y_annot_loc, _) = kwargs["y_range"] else: y_annot_loc = min(self.data.y) - axis.axhline(y=1, **line_style) - axis.annotate( - f"y = 1", xy=(x_annot_loc, 1), xycoords="data", - horizontalalignment='left', - verticalalignment='bottom', - size="x-small", - color=line_style["color"]) - axis.axhline(y=-1, **line_style) - axis.annotate( - f"y = -1", xy=(x_annot_loc, -1), xycoords="data", - horizontalalignment='left', - verticalalignment='top', - size="x-small", - color=line_style["color"]) - axis.axvline(x=1, **line_style) - axis.annotate( - f"x = -1", xy=(-1, y_annot_loc), xycoords="data", - horizontalalignment='right', - verticalalignment='bottom', - rotation=90, size="x-small", - color=line_style["color"]) - axis.axvline(x=-1, **line_style) - axis.annotate( - f"x = 1", xy=(1, y_annot_loc), xycoords="data", - horizontalalignment='left', - verticalalignment='bottom', - rotation=90, size="x-small", - color=line_style["color"]) - # Number of genes beyond lfc thresholds, in each quadrant - # up_up = 100 * len(self.data.query( - # f"x > 1 & y > 1")) / len(self.data) - # up_down = 100 * len(self.data.query( - # f"x > 1 & y < 1")) / len(self.data) - # down_up = 100 * len(self.data.query( - # f"x < 1 & y > 1")) / len(self.data) - # down_down = 100 * len(self.data.query( - # f"x < 1 & y < 1")) / len(self.data) + Scatterplot.put_quadrants( + axis, x_annot_loc, y_annot_loc, line_style) + # Genes beyond lfc thresholds, in each quadrant up_up = self.data.query( "x > 1 & y > 1") up_down = self.data.query( @@ -256,30 +305,13 @@ class Scatterplot: ingroup_up_down = "" ingroup_down_up = "" ingroup_down_down = "" - axis.annotate( + Scatterplot.write_quadrant_counts( + axis, f"{len(up_up)}{ingroup_up_up}", - xy=(0.95, 0.95), xycoords="axes fraction", - size="x-small", color=line_style["color"], - horizontalalignment="right", - verticalalignment="top") - axis.annotate( f"{len(up_down)}{ingroup_up_down}", - xy=(0.95, 0.05), xycoords="axes fraction", - size="x-small", color=line_style["color"], - horizontalalignment="right", - verticalalignment="bottom") - axis.annotate( f"{len(down_up)}{ingroup_down_up}", - xy=(0.05, 0.95), xycoords="axes fraction", - size="x-small", color=line_style["color"], - horizontalalignment="left", - verticalalignment="top") - axis.annotate( f"{len(down_down)}{ingroup_down_down}", - xy=(0.05, 0.05), xycoords="axes fraction", - size="x-small", color=line_style["color"], - horizontalalignment="left", - verticalalignment="bottom") + line_style["color"]) axis.set_xlabel(self.x_label, fontsize=17) axis.set_ylabel(self.y_label, fontsize=17) # This doesn't work with plt.axis("equal") -- GitLab