From b163b1d0036644aa4afd98f4e3a6adcccd7aaba6 Mon Sep 17 00:00:00 2001 From: Julien Lerat Date: Fri, 30 Aug 2024 17:10:06 +1000 Subject: [PATCH] fix: fixed violin when data is all nan --- hydrodiy/plot/tests/test_hyplot_violinplot.py | 11 +++++++++++ hydrodiy/plot/violinplot.py | 14 +++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/hydrodiy/plot/tests/test_hyplot_violinplot.py b/hydrodiy/plot/tests/test_hyplot_violinplot.py index 9a7f320a..79d4e837 100644 --- a/hydrodiy/plot/tests/test_hyplot_violinplot.py +++ b/hydrodiy/plot/tests/test_hyplot_violinplot.py @@ -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) diff --git a/hydrodiy/plot/violinplot.py b/hydrodiy/plot/violinplot.py index 06d2034c..0077501e 100644 --- a/hydrodiy/plot/violinplot.py +++ b/hydrodiy/plot/violinplot.py @@ -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) @@ -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 @@ -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) +