Skip to content

Commit

Permalink
fix: added ylim to violin draw
Browse files Browse the repository at this point in the history
  • Loading branch information
jlerat committed Feb 5, 2025
1 parent 440a102 commit adcfaaf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions src/hydrodiy/plot/tests/test_hyplot_violinplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 25 additions & 13 deletions src/hydrodiy/plot/violinplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]<ylim[1], got {ylim}."
raise ValueError(errmess)

ax = self._ax
kde_x, kde_y = self.kde_x, self.kde_y
ncols = kde_x.shape[1]
Expand Down Expand Up @@ -279,7 +287,7 @@ def draw(self, ax=None):
item = self.extremes
ix = (x >= 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]]])
Expand All @@ -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]]])
Expand Down Expand Up @@ -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
Expand All @@ -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)

0 comments on commit adcfaaf

Please sign in to comment.