From adcfaaf7aba712ba79de7dd2682b6f3e92e6a663 Mon Sep 17 00:00:00 2001 From: Julien Lerat Date: Wed, 5 Feb 2025 12:20:08 +1100 Subject: [PATCH] fix: added ylim to violin draw --- .gitignore | 2 +- .../plot/tests/test_hyplot_violinplot.py | 16 ++++++++ src/hydrodiy/plot/violinplot.py | 38 ++++++++++++------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index c9ab71e..ad93a21 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,7 @@ src/hydrodiy/gis/tests/grid_test.bil src/hydrodiy/data/c_hydrodiy_data.c src/hydrodiy/stat/c_hydrodiy_stat.c src/hydrodiy/gis/c_hydrodiy_gis.c -src/hydrodiy.egg-info +hydrodiy.egg-info nosetests.xml nohup.out listmembers.csv diff --git a/src/hydrodiy/plot/tests/test_hyplot_violinplot.py b/src/hydrodiy/plot/tests/test_hyplot_violinplot.py index 79d4e83..e828038 100644 --- a/src/hydrodiy/plot/tests/test_hyplot_violinplot.py +++ b/src/hydrodiy/plot/tests/test_hyplot_violinplot.py @@ -94,6 +94,22 @@ def test_violin_colors(): fig.savefig(fp) +def test_violin_draw_ylim(): + plt.close("all") + vl = Violin(data=DATA2) + fig, ax = plt.subplots() + y0 = DATA2.max().max() + y1 = DATA2.min().min() + dy = y1-y0 + y0 += dy*0.1 + y1 -= dy*0.1 + + vl.draw(ax=ax, ylim=(y0, y1)) + fp = FIMG / "violin_plot_extremes.png" + fig.savefig(fp) + + + def test_violin_draw_extremes(): plt.close("all") vl = Violin(data=DATA2) diff --git a/src/hydrodiy/plot/violinplot.py b/src/hydrodiy/plot/violinplot.py index 1e6d851..8e79e65 100644 --- a/src/hydrodiy/plot/violinplot.py +++ b/src/hydrodiy/plot/violinplot.py @@ -234,19 +234,27 @@ def _compute(self): self._kde_x = kde_x self._kde_y = kde_y - def draw(self, ax=None): + def draw(self, ax=None, ylim=None): """ Draw the boxplot Parameters ----------- ax : matplotlib.axes - Axe to draw the boxplot on + Axe to draw the boxplot on. + ylim : tuple + Boundary on y axis limits. """ if ax is None: self._ax = plt.gca() else: self._ax = ax + if ylim is not None: + y0, y1 = ylim + if y0 >= y1: + errmess = f"Expected ylim[0]= self.stat_extremes_low[colname])\ & (x <= self.stat_extremes_high[colname]) - if ix.sum()>0: + if ix.sum() > 0: uu1, uu2 = i-y[ix]*vw/2, i+y[ix]*vw/2 vv = x[ix] uc = np.concatenate([uu1, uu2[::-1], [uu1.iloc[0]]]) @@ -299,7 +307,7 @@ def draw(self, ax=None): item = self.center ix = (x >= self.stat_center_low[colname])\ & (x <= self.stat_center_high[colname]) - if ix.sum()>0: + if ix.sum() > 0: uu1, uu2 = i-y[ix]*vw/2, i+y[ix]*vw/2 vv = x[ix] uc = np.concatenate([uu1, uu2[::-1], [uu1.iloc[0]]]) @@ -335,15 +343,16 @@ def draw(self, ax=None): valuetext = f"{value:{item.number_format}}" xshift = 0 - txt = ax.text(i+xshift, - value, - valuetext, - fontweight=item.fontweight, - fontsize=item.fontsize, - color=item.fontcolor, - va=item.va, ha=item.ha, - alpha=item.alpha) - colelement[statname+"-text"] = txt + if ylim is None or (value >= ylim[0] and value <= ylim[1]): + txt = ax.text(i+xshift, + value, + valuetext, + fontweight=item.fontweight, + fontsize=item.fontsize, + color=item.fontcolor, + va=item.va, ha=item.ha, + alpha=item.alpha) + colelement[statname+"-text"] = txt # Store self.elements[colname] = colelement @@ -355,3 +364,6 @@ def draw(self, ax=None): ncols = kde_x.shape[1] xlim = (-vw, ncols-1+vw) ax.set_xlim(xlim) + + if ylim is not None: + ax.set_ylim(ylim)