Skip to content

Commit

Permalink
fix: fixed infinite values in violin data (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlerat authored Dec 19, 2024
1 parent 75f5282 commit 069fcd0
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions src/hydrodiy/plot/violinplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def _compute(self):

# Compute kde
for cn, se in data.items():
notnull = se.notnull()
notnull = se.notnull() & np.isfinite(se.values)
if notnull.sum() <= 2:
kde_x.loc[:, cn] = np.nan
kde_y.loc[:, cn] = np.nan
Expand All @@ -222,7 +222,7 @@ def _compute(self):
kernel = gaussian_kde(sen.values)

# blend regular spacing and ecdf spacing
x0, x1 = se.min(), se.max()
x0, x1 = sen.min(), sen.max()
x = np.linspace(x0, x1, (npts-len(sen)))
err = 1e-6*np.random.uniform(-1, 1, len(sen))
x = np.sort(np.concatenate([x, sen.values+err]))
Expand Down Expand Up @@ -279,39 +279,40 @@ def draw(self, ax=None):
item = self.extremes
ix = (x >= self.stat_extremes_low[colname])\
& (x <= self.stat_extremes_high[colname])

uu1, uu2 = i-y[ix]*vw/2, i+y[ix]*vw/2
vv = x[ix]
uc = np.concatenate([uu1, uu2[::-1], [uu1.iloc[0]]])
vc = np.concatenate([vv, vv[::-1], [vv.iloc[0]]])
hatch = None if item.hatch == "none" else item.hatch
epoly = Polygon(np.column_stack([uc, vc]),
edgecolor="none",
facecolor=item.facecolor,
linewidth=item.linewidth,
hatch=hatch,
alpha=item.alpha)
ax.add_patch(epoly)
n = "extreme-polygon"
colelement[n] = epoly
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]]])
vc = np.concatenate([vv, vv[::-1], [vv.iloc[0]]])
hatch = None if item.hatch == "none" else item.hatch
epoly = Polygon(np.column_stack([uc, vc]),
edgecolor="none",
facecolor=item.facecolor,
linewidth=item.linewidth,
hatch=hatch,
alpha=item.alpha)
ax.add_patch(epoly)
n = "extreme-polygon"
colelement[n] = epoly

# Draw center
item = self.center
ix = (x >= self.stat_center_low[colname])\
& (x <= self.stat_center_high[colname])
uu1, uu2 = i-y[ix]*vw/2, i+y[ix]*vw/2
vv = x[ix]
uc = np.concatenate([uu1, uu2[::-1], [uu1.iloc[0]]])
vc = np.concatenate([vv, vv[::-1], [vv.iloc[0]]])
cpoly = Polygon(np.column_stack([uc, vc]),
edgecolor=item.linecolor,
facecolor=item.facecolor,
linewidth=item.linewidth,
hatch=hatch,
alpha=item.alpha)
ax.add_patch(cpoly)
n = "center-polygon"
colelement[n] = cpoly
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]]])
vc = np.concatenate([vv, vv[::-1], [vv.iloc[0]]])
cpoly = Polygon(np.column_stack([uc, vc]),
edgecolor=item.linecolor,
facecolor=item.facecolor,
linewidth=item.linewidth,
hatch=hatch,
alpha=item.alpha)
ax.add_patch(cpoly)
n = "center-polygon"
colelement[n] = cpoly

# Text
for statname in ["median", "centerlow", "centerhigh"]:
Expand Down

0 comments on commit 069fcd0

Please sign in to comment.