Skip to content

Commit 27fbbc0

Browse files
authored
fix: fixed violin when data is all nan (#25)
1 parent 9c40f15 commit 27fbbc0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

hydrodiy/plot/tests/test_hyplot_violinplot.py

+11
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ def test_violin_missing():
115115
fig.savefig(fp)
116116

117117

118+
def test_violin_allnan():
119+
plt.close("all")
120+
df = DATA1.copy()
121+
df.iloc[:, 0] = np.nan
122+
vl = Violin(data=df)
123+
fig, ax = plt.subplots()
124+
vl.draw(ax=ax)
125+
fp = FIMG / "violin_allnan.png"
126+
fig.savefig(fp)
127+
128+
118129
def test_violin_log():
119130
plt.close("all")
120131
vl = Violin(data=DATA2)

hydrodiy/plot/violinplot.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ def _compute(self):
210210
# Compute kde
211211
for cn, se in data.items():
212212
notnull = se.notnull()
213-
if notnull.sum() <= 2:
214-
errmess = f"Less than 2 valid data in {cn}."
215-
raise ValueError(errmess)
213+
if notnull.sum()<=2:
214+
kde_x.loc[:, cn] = np.nan
215+
kde_y.loc[:, cn] = np.nan
216+
continue
216217

217218
sen = se[notnull]
218219
kernel = gaussian_kde(sen.values)
@@ -253,6 +254,8 @@ def draw(self, ax=None):
253254
for i, colname in enumerate(kde_x.columns):
254255
# Get kde data
255256
x = kde_x.loc[:, colname]
257+
if x.isnull().all():
258+
continue
256259
y = kde_y.loc[:, colname]
257260

258261
# initialise boxplot elements
@@ -344,3 +347,8 @@ def draw(self, ax=None):
344347
xt = np.arange(ncols)
345348
ax.set_xticks(xt)
346349
ax.set_xticklabels(kde_x.columns)
350+
351+
ncols = kde_x.shape[1]
352+
xlim = (-vw, ncols-1+vw)
353+
ax.set_xlim(xlim)
354+

0 commit comments

Comments
 (0)