0

The hue feature is not working when I am using pairplot.

Here is my data frame: df

Here is the code that doesn't work:

sns.pairplot(activities, hue="Day")

If I remove the hue option it works. Also if I change the hue to a numerical column (such as Distance) it works, but it is not working with the Day column for some reason. Here's the error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_121/1783531066.py in <module>
----> 1 sns.pairplot(activities, hue="Day")

/opt/conda/lib/python3.7/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

/opt/conda/lib/python3.7/site-packages/seaborn/axisgrid.py in pairplot(data, hue, hue_order, palette, vars, x_vars, y_vars, kind, diag_kind, markers, height, aspect, corner, dropna, plot_kws, diag_kws, grid_kws, size)
   2020     elif diag_kind == "kde":
   2021         diag_kws.setdefault("fill", True)
-> 2022         grid.map_diag(kdeplot, **diag_kws)
   2023 
   2024     # Maybe plot on the off-diagonals

/opt/conda/lib/python3.7/site-packages/seaborn/axisgrid.py in map_diag(self, func, **kwargs)
   1400             plot_kwargs.setdefault("hue_order", self._hue_order)
   1401             plot_kwargs.setdefault("palette", self._orig_palette)
-> 1402             func(x=vector, **plot_kwargs)
   1403             self._clean_axis(ax)
   1404 

/opt/conda/lib/python3.7/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

/opt/conda/lib/python3.7/site-packages/seaborn/distributions.py in kdeplot(x, y, shade, vertical, kernel, bw, gridsize, cut, clip, legend, cumulative, shade_lowest, cbar, cbar_ax, cbar_kws, ax, weights, hue, palette, hue_order, hue_norm, multiple, common_norm, common_grid, levels, thresh, bw_method, bw_adjust, log_scale, color, fill, data, data2, **kwargs)
   1733             legend=legend,
   1734             estimate_kws=estimate_kws,
-> 1735             **plot_kws,
   1736         )
   1737 

/opt/conda/lib/python3.7/site-packages/seaborn/distributions.py in plot_univariate_density(self, multiple, common_norm, common_grid, fill, legend, estimate_kws, **plot_kws)
    914             common_grid,
    915             estimate_kws,
--> 916             log_scale,
    917         )
    918 

/opt/conda/lib/python3.7/site-packages/seaborn/distributions.py in _compute_univariate_density(self, data_variable, common_norm, common_grid, estimate_kws, log_scale)
    314 
    315             # Estimate the density of observations at this level
--> 316             density, support = estimator(observations, weights=weights)
    317 
    318             if log_scale:

/opt/conda/lib/python3.7/site-packages/seaborn/_statistics.py in __call__(self, x1, x2, weights)
    185         """Fit and evaluate on univariate or bivariate data."""
    186         if x2 is None:
--> 187             return self._eval_univariate(x1, weights)
    188         else:
    189             return self._eval_bivariate(x1, x2, weights)

/opt/conda/lib/python3.7/site-packages/seaborn/_statistics.py in _eval_univariate(self, x, weights)
    144         support = self.support
    145         if support is None:
--> 146             support = self.define_support(x, cache=False)
    147 
    148         kde = self._fit(x, weights)

/opt/conda/lib/python3.7/site-packages/seaborn/_statistics.py in define_support(self, x1, x2, weights, cache)
    117         """Create the evaluation grid for a given data set."""
    118         if x2 is None:
--> 119             support = self._define_support_univariate(x1, weights)
    120         else:
    121             support = self._define_support_bivariate(x1, x2, weights)

/opt/conda/lib/python3.7/site-packages/seaborn/_statistics.py in _define_support_univariate(self, x, weights)
     89     def _define_support_univariate(self, x, weights):
     90         """Create a 1D grid of evaluation points."""
---> 91         kde = self._fit(x, weights)
     92         bw = np.sqrt(kde.covariance.squeeze())
     93         grid = self._define_support_grid(

/opt/conda/lib/python3.7/site-packages/seaborn/_statistics.py in _fit(self, fit_data, weights)
    135             fit_kws["weights"] = weights
    136 
--> 137         kde = stats.gaussian_kde(fit_data, **fit_kws)
    138         kde.set_bandwidth(kde.factor * self.bw_adjust)
    139 

/opt/conda/lib/python3.7/site-packages/scipy/stats/kde.py in __init__(self, dataset, bw_method, weights)
    204             self._neff = 1/sum(self._weights**2)
    205 
--> 206         self.set_bandwidth(bw_method=bw_method)
    207 
    208     def evaluate(self, points):

/opt/conda/lib/python3.7/site-packages/scipy/stats/kde.py in set_bandwidth(self, bw_method)
    552             raise ValueError(msg)
    553 
--> 554         self._compute_covariance()
    555 
    556     def _compute_covariance(self):

/opt/conda/lib/python3.7/site-packages/scipy/stats/kde.py in _compute_covariance(self)
    564                                                bias=False,
    565                                                aweights=self.weights))
--> 566             self._data_inv_cov = linalg.inv(self._data_covariance)
    567 
    568         self.covariance = self._data_covariance * self.factor**2

/opt/conda/lib/python3.7/site-packages/scipy/linalg/basic.py in inv(a, overwrite_a, check_finite)
    937 
    938     """
--> 939     a1 = _asarray_validated(a, check_finite=check_finite)
    940     if len(a1.shape) != 2 or a1.shape[0] != a1.shape[1]:
    941         raise ValueError('expected square matrix')

/opt/conda/lib/python3.7/site-packages/scipy/_lib/_util.py in _asarray_validated(a, check_finite, sparse_ok, objects_ok, mask_ok, as_inexact)
    294     if not objects_ok:
    295         if a.dtype is np.dtype('O'):
--> 296             raise ValueError('object arrays are not supported')
    297     if as_inexact:
    298         if not np.issubdtype(a.dtype, np.inexact):

ValueError: object arrays are not supported

Any ideas why hue isn't working?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Does the column type affect the ability to use Hue? The Day column is a category type. – Mister Jones Nov 21 '21 at 21:14
  • I think I figured it out, one of the numerical columns was set as an object. changing that to a float resolved it. – Mister Jones Nov 21 '21 at 21:20
  • 1
    Maybe you should also leave out the `Date` column – JohanC Nov 21 '21 at 21:23
  • Does this answer your question? [Seaborn pairplot hue parameter not working as expected](https://stackoverflow.com/questions/52103408/seaborn-pairplot-hue-parameter-not-working-as-expected) – Trenton McKinney Nov 21 '21 at 23:17
  • **[Don't Post Screenshots](https://meta.stackoverflow.com/questions/303812/)**. This question needs a [SSCCE](http://sscce.org/). Always provide a [mre], with **code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. It's likely the question will be down-voted and closed. You're discouraging assistance, as no one wants to retype data/code, and screenshots are often illegible. [edit] the question and **add text**. Plots are okay. See [How to provide a reproducible dataframe](https://stackoverflow.com/questions/52413246). – Trenton McKinney Nov 21 '21 at 23:18

1 Answers1

0

You can see the error:

ValueError: object arrays are not supported

Means the variable needs to be numerical.

Franco Piccolo
  • 6,845
  • 8
  • 34
  • 52