Skip to content

Commit

Permalink
fix: fixed violin when data is all nan (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlerat authored Aug 30, 2024
1 parent 9c40f15 commit 27fbbc0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 11 additions & 0 deletions hydrodiy/plot/tests/test_hyplot_violinplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def test_violin_missing():
fig.savefig(fp)


def test_violin_allnan():
plt.close("all")
df = DATA1.copy()
df.iloc[:, 0] = np.nan
vl = Violin(data=df)
fig, ax = plt.subplots()
vl.draw(ax=ax)
fp = FIMG / "violin_allnan.png"
fig.savefig(fp)


def test_violin_log():
plt.close("all")
vl = Violin(data=DATA2)
Expand Down
14 changes: 11 additions & 3 deletions hydrodiy/plot/violinplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ def _compute(self):
# Compute kde
for cn, se in data.items():
notnull = se.notnull()
if notnull.sum() <= 2:
errmess = f"Less than 2 valid data in {cn}."
raise ValueError(errmess)
if notnull.sum()<=2:
kde_x.loc[:, cn] = np.nan
kde_y.loc[:, cn] = np.nan
continue

sen = se[notnull]
kernel = gaussian_kde(sen.values)
Expand Down Expand Up @@ -253,6 +254,8 @@ def draw(self, ax=None):
for i, colname in enumerate(kde_x.columns):
# Get kde data
x = kde_x.loc[:, colname]
if x.isnull().all():
continue
y = kde_y.loc[:, colname]

# initialise boxplot elements
Expand Down Expand Up @@ -344,3 +347,8 @@ def draw(self, ax=None):
xt = np.arange(ncols)
ax.set_xticks(xt)
ax.set_xticklabels(kde_x.columns)

ncols = kde_x.shape[1]
xlim = (-vw, ncols-1+vw)
ax.set_xlim(xlim)

0 comments on commit 27fbbc0

Please sign in to comment.