Skip to content

Commit f8275e7

Browse files
authored
fix: added ylim to violin draw (#36)
* fix: added ylim to violin draw * test: fixed violin test
1 parent 440a102 commit f8275e7

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ src/hydrodiy/gis/tests/grid_test.bil
3434
src/hydrodiy/data/c_hydrodiy_data.c
3535
src/hydrodiy/stat/c_hydrodiy_stat.c
3636
src/hydrodiy/gis/c_hydrodiy_gis.c
37-
src/hydrodiy.egg-info
37+
hydrodiy.egg-info
3838
nosetests.xml
3939
nohup.out
4040
listmembers.csv

src/hydrodiy/plot/tests/test_hyplot_violinplot.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ def test_violin_colors():
9494
fig.savefig(fp)
9595

9696

97+
def test_violin_draw_ylim():
98+
plt.close("all")
99+
vl = Violin(data=DATA2)
100+
fig, ax = plt.subplots()
101+
102+
msg = "Expected ylim"
103+
with pytest.raises(ValueError, match=msg):
104+
vl.draw(ax=ax, ylim=(10, 1))
105+
106+
meds = DATA2.median()
107+
y0 = 0
108+
y1 = 2
109+
vl.draw(ax=ax, ylim=(y0, y1))
110+
fp = FIMG / "violin_plot_ylim.png"
111+
fig.savefig(fp)
112+
113+
97114
def test_violin_draw_extremes():
98115
plt.close("all")
99116
vl = Violin(data=DATA2)

src/hydrodiy/plot/violinplot.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,27 @@ def _compute(self):
234234
self._kde_x = kde_x
235235
self._kde_y = kde_y
236236

237-
def draw(self, ax=None):
237+
def draw(self, ax=None, ylim=None):
238238
""" Draw the boxplot
239239
240240
Parameters
241241
-----------
242242
ax : matplotlib.axes
243-
Axe to draw the boxplot on
243+
Axe to draw the boxplot on.
244+
ylim : tuple
245+
Boundary on y axis limits.
244246
"""
245247
if ax is None:
246248
self._ax = plt.gca()
247249
else:
248250
self._ax = ax
249251

252+
if ylim is not None:
253+
y0, y1 = ylim
254+
if y0 >= y1:
255+
errmess = f"Expected ylim[0]<ylim[1], got {ylim}."
256+
raise ValueError(errmess)
257+
250258
ax = self._ax
251259
kde_x, kde_y = self.kde_x, self.kde_y
252260
ncols = kde_x.shape[1]
@@ -279,7 +287,7 @@ def draw(self, ax=None):
279287
item = self.extremes
280288
ix = (x >= self.stat_extremes_low[colname])\
281289
& (x <= self.stat_extremes_high[colname])
282-
if ix.sum()>0:
290+
if ix.sum() > 0:
283291
uu1, uu2 = i-y[ix]*vw/2, i+y[ix]*vw/2
284292
vv = x[ix]
285293
uc = np.concatenate([uu1, uu2[::-1], [uu1.iloc[0]]])
@@ -299,7 +307,7 @@ def draw(self, ax=None):
299307
item = self.center
300308
ix = (x >= self.stat_center_low[colname])\
301309
& (x <= self.stat_center_high[colname])
302-
if ix.sum()>0:
310+
if ix.sum() > 0:
303311
uu1, uu2 = i-y[ix]*vw/2, i+y[ix]*vw/2
304312
vv = x[ix]
305313
uc = np.concatenate([uu1, uu2[::-1], [uu1.iloc[0]]])
@@ -335,15 +343,16 @@ def draw(self, ax=None):
335343
valuetext = f"{value:{item.number_format}}"
336344
xshift = 0
337345

338-
txt = ax.text(i+xshift,
339-
value,
340-
valuetext,
341-
fontweight=item.fontweight,
342-
fontsize=item.fontsize,
343-
color=item.fontcolor,
344-
va=item.va, ha=item.ha,
345-
alpha=item.alpha)
346-
colelement[statname+"-text"] = txt
346+
if ylim is None or (value >= ylim[0] and value <= ylim[1]):
347+
txt = ax.text(i+xshift,
348+
value,
349+
valuetext,
350+
fontweight=item.fontweight,
351+
fontsize=item.fontsize,
352+
color=item.fontcolor,
353+
va=item.va, ha=item.ha,
354+
alpha=item.alpha)
355+
colelement[statname+"-text"] = txt
347356

348357
# Store
349358
self.elements[colname] = colelement
@@ -355,3 +364,6 @@ def draw(self, ax=None):
355364
ncols = kde_x.shape[1]
356365
xlim = (-vw, ncols-1+vw)
357366
ax.set_xlim(xlim)
367+
368+
if ylim is not None:
369+
ax.set_ylim(ylim)

0 commit comments

Comments
 (0)