Repository: mwaskom/seaborn

Issue:
Supress Warnings in library
Thanks for a great library. please suppress the warnings below. 

**Example :** 

`sns.pairplot(df_centroids, hue="CUST_ID_NEW",palette="bright", height=2, aspect=1)`

seaborn version : 0.11.1
matplotlib:3.2.2


```
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:306: UserWarning: Dataset has 0 variance; skipping density estimate.
  warnings.warn(msg, UserWarning)
```





Discussion:

Patch:
diff --git a/doc/releases/v0.12.0.txt b/doc/releases/v0.12.0.txt
index fba59d17..eee607ed 100644
--- a/doc/releases/v0.12.0.txt
+++ b/doc/releases/v0.12.0.txt
@@ -20,6 +20,8 @@ A paper describing seaborn was published in the `Journal of Open Source Software
 
 - |Enhancement| In :func:`histplot`, added `stat="percent"` as an option for normalization such that bar heights sum to 100 (:pr:`2461`).
 
+- |Enhancement| In :func:`kdeplot`, added the `warn_singular` parameter to allow silencing of the warning about data with zero variance (:pr:`2566`).
+
 - |Enhancement| |Fix| Improved integration with the matplotlib color cycle in most axes-level functions (:pr:`2449`).
 
 - |Fix| In :func:`lineplot, allowed the `dashes` keyword to set the style of a line without mapping a `style` variable (:pr:`2449`).
diff --git a/seaborn/axisgrid.py b/seaborn/axisgrid.py
index 89dc9801..2d33f75b 100644
--- a/seaborn/axisgrid.py
+++ b/seaborn/axisgrid.py
@@ -2012,6 +2012,7 @@ def pairplot(
         grid.map_diag(histplot, **diag_kws)
     elif diag_kind == "kde":
         diag_kws.setdefault("fill", True)
+        diag_kws.setdefault("warn_singular", False)
         grid.map_diag(kdeplot, **diag_kws)
 
     # Maybe plot on the off-diagonals
@@ -2028,6 +2029,7 @@ def pairplot(
         plotter(regplot, **plot_kws)
     elif kind == "kde":
         from .distributions import kdeplot  # Avoid circular import
+        plot_kws.setdefault("warn_singular", False)
         plotter(kdeplot, **plot_kws)
     elif kind == "hist":
         from .distributions import histplot  # Avoid circular import
@@ -2131,6 +2133,7 @@ def jointplot(
             marg_func = histplot
         else:
             marg_func = kdeplot
+            marginal_kws.setdefault("warn_singular", False)
             marginal_kws.setdefault("fill", True)
 
         marginal_kws.setdefault("color", color)
@@ -2163,6 +2166,7 @@ def jointplot(
     elif kind.startswith("kde"):
 
         joint_kws.setdefault("color", color)
+        joint_kws.setdefault("warn_singular", False)
         grid.plot_joint(kdeplot, **joint_kws)
 
         marginal_kws.setdefault("color", color)
diff --git a/seaborn/distributions.py b/seaborn/distributions.py
index 54d4fa46..a60d650a 100644
--- a/seaborn/distributions.py
+++ b/seaborn/distributions.py
@@ -298,6 +298,7 @@ class _DistributionPlotter(VectorPlotter):
         common_grid,
         estimate_kws,
         log_scale,
+        warn_singular=True,
     ):
 
         # Initialize the estimator object
@@ -321,8 +322,12 @@ class _DistributionPlotter(VectorPlotter):
 
             observation_variance = observations.var()
             if math.isclose(observation_variance, 0) or np.isnan(observation_variance):
-                msg = "Dataset has 0 variance; skipping density estimate."
-                warnings.warn(msg, UserWarning)
+                msg = (
+                    "Dataset has 0 variance; skipping density estimate. "
+                    "Pass `warn_singular=False` to disable this warning."
+                )
+                if warn_singular:
+                    warnings.warn(msg, UserWarning)
                 continue
 
             # Extract the weights for this subset of observations
@@ -430,6 +435,7 @@ class _DistributionPlotter(VectorPlotter):
                 common_bins,
                 kde_kws,
                 log_scale,
+                warn_singular=False,
             )
 
         # First pass through the data to compute the histograms
@@ -872,6 +878,7 @@ class _DistributionPlotter(VectorPlotter):
         multiple,
         common_norm,
         common_grid,
+        warn_singular,
         fill,
         color,
         legend,
@@ -908,6 +915,7 @@ class _DistributionPlotter(VectorPlotter):
             common_grid,
             estimate_kws,
             log_scale,
+            warn_singular,
         )
 
         # Adjust densities based on the `multiple` rule
@@ -1009,6 +1017,7 @@ class _DistributionPlotter(VectorPlotter):
         color,
         legend,
         cbar,
+        warn_singular,
         cbar_ax,
         cbar_kws,
         estimate_kws,
@@ -1041,8 +1050,12 @@ class _DistributionPlotter(VectorPlotter):
             # Check that KDE will not error out
             variance = observations[["x", "y"]].var()
             if any(math.isclose(x, 0) for x in variance) or variance.isna().any():
-                msg = "Dataset has 0 variance; skipping density estimate."
-                warnings.warn(msg, UserWarning)
+                msg = (
+                    "Dataset has 0 variance; skipping density estimate. "
+                    "Pass `warn_singular=False` to disable this warning."
+                )
+                if warn_singular:
+                    warnings.warn(msg, UserWarning)
                 continue
 
             # Estimate the density of observations at this level
@@ -1584,6 +1597,9 @@ def kdeplot(
     # Renamed params
     data=None, data2=None,
 
+    # New in v0.12
+    warn_singular=True,
+
     **kwargs,
 ):
 
@@ -1705,6 +1721,7 @@ def kdeplot(
             fill=fill,
             color=color,
             legend=legend,
+            warn_singular=warn_singular,
             estimate_kws=estimate_kws,
             **plot_kws,
         )
@@ -1718,6 +1735,7 @@ def kdeplot(
             thresh=thresh,
             legend=legend,
             color=color,
+            warn_singular=warn_singular,
             cbar=cbar,
             cbar_ax=cbar_ax,
             cbar_kws=cbar_kws,
@@ -1813,6 +1831,9 @@ fill : bool or None
     If True, fill in the area under univariate density curves or between
     bivariate contours. If None, the default depends on ``multiple``.
 {params.core.data}
+warn_singular : bool
+    If True, issue a warning when trying to estimate the density of data
+    with zero variance.
 kwargs
     Other keyword arguments are passed to one of the following matplotlib
     functions:


Test patch:
diff --git a/seaborn/tests/test_distributions.py b/seaborn/tests/test_distributions.py
index a11b2aa9..029b5b7b 100644
--- a/seaborn/tests/test_distributions.py
+++ b/seaborn/tests/test_distributions.py
@@ -390,6 +390,10 @@ class TestKDEPlotUnivariate(SharedAxesLevelTests):
             ax = kdeplot(x=[5])
         assert not ax.lines
 
+        with pytest.warns(None) as record:
+            ax = kdeplot(x=[5], warn_singular=False)
+        assert not record
+
     def test_variable_assignment(self, long_df):
 
         f, ax = plt.subplots()
@@ -888,6 +892,10 @@ class TestKDEPlotBivariate:
             ax = dist.kdeplot(x=[5], y=[6])
         assert not ax.lines
 
+        with pytest.warns(None) as record:
+            ax = kdeplot(x=[5], y=[7], warn_singular=False)
+        assert not record
+
     def test_fill_artists(self, long_df):
 
         for fill in [True, False]:
@@ -1477,12 +1485,14 @@ class TestHistPlotUnivariate(SharedAxesLevelTests):
 
     def test_kde_singular_data(self):
 
-        with pytest.warns(UserWarning):
+        with pytest.warns(None) as record:
             ax = histplot(x=np.ones(10), kde=True)
+        assert not record
         assert not ax.lines
 
-        with pytest.warns(UserWarning):
+        with pytest.warns(None) as record:
             ax = histplot(x=[5], kde=True)
+        assert not record
         assert not ax.lines
 
     def test_element_default(self, long_df):