Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added ylim to violin draw #36

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 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,23 @@ def test_violin_colors():
fig.savefig(fp)


def test_violin_draw_ylim():
plt.close("all")
vl = Violin(data=DATA2)
fig, ax = plt.subplots()

msg = "Expected ylim"
with pytest.raises(ValueError, match=msg):
vl.draw(ax=ax, ylim=(10, 1))

meds = DATA2.median()
y0 = 0
y1 = 2
vl.draw(ax=ax, ylim=(y0, y1))
fp = FIMG / "violin_plot_ylim.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)