7
$\begingroup$

I'm trying to plot a violin plot with a split based on Sex ( like in the fourth example in the doccumentation but with Sex)

Example plot from  the doccumentation

I can produce a categorical scatter plot and split it by Sex. However, when i attempt the same but as a violin plot; it throws an error.

Traceback (most recent call last):

  File "<ipython-input-868-0599b976fd6c>", line 1, in <module>
    sns.catplot(x="Batch", y="Age", hue = 'Sex', data = ages,  kind='violin')

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/seaborn/categorical.py", line 3755, in catplot
    g.map_dataframe(plot_func, x, y, hue, **plot_kws)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/seaborn/axisgrid.py", line 820, in map_dataframe
    self._facet_plot(func, ax, args, kwargs)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/seaborn/axisgrid.py", line 838, in _facet_plot
    func(*plot_args, **plot_kwargs)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/seaborn/categorical.py", line 2387, in violinplot
    color, palette, saturation)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/seaborn/categorical.py", line 564, in __init__
    self.estimate_densities(bw, cut, scale, scale_hue, gridsize)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/seaborn/categorical.py", line 679, in estimate_densities
    kde, bw_used = self.fit_kde(kde_data, bw)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/seaborn/categorical.py", line 719, in fit_kde
    kde = stats.gaussian_kde(x)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/scipy/stats/kde.py", line 208, in __init__
    self.set_bandwidth(bw_method=bw_method)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/scipy/stats/kde.py", line 540, in set_bandwidth
    self._compute_covariance()

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/scipy/stats/kde.py", line 551, in _compute_covariance
    aweights=self.weights))

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py", line 2427, in cov
    avg, w_sum = average(X, axis=1, weights=w, returned=True)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py", line 419, in average
    scl = wgt.sum(axis=axis, dtype=result_dtype)

  File "/home/tasty/anaconda3/lib/python3.7/site-packages/numpy/core/_methods.py", line 36, in _sum
    return umr_sum(a, axis, dtype, out, keepdims, initial)

TypeError: No loop matching the specified signature and casting
was found for ufunc add

My code is:

>>> print(ages.head())
            Age     Sex  Batch
PassengerId                   
852          74    male  Train
86           33  female  Train
161          44    male  Train
812          39    male  Train
837          21    male  Train

>>> sns.catplot(x="Batch", y="Age", hue = 'Sex', data = ages,  kind='violin')

Removing the kind argument produces the following scatterplot: Scatter plot that i can correctly produce

How do I get rid of the error to display the data as a violin plot?

Thanks in advance

edit: Seaborn version: 0.9.0 Numpy version: 1.16.2 Python version: 3.7.3

$\endgroup$

2 Answers 2

7
$\begingroup$

I had tried the suggestion from @foxthatruns's answer to no avail. I found that changing my numeric column to float64 solved the problem (reference).

df['my_column']=df['my_column'].astype('float64')

This was done with Python 3.7, seaborn 0.9.0, numpy 1.16.4.

$\endgroup$
2
  • 2
    $\begingroup$ This worked for me. It turns out that the offending column had type 'object', but recasting it as 'float' solved the problem. Interestingly, the same column worked fine in a boxplot. (seaborn 0.10.1, numpy 1.18.5) $\endgroup$
    – ELNJ
    Commented Sep 23, 2020 at 18:21
  • $\begingroup$ @ELNJ exactly my problem and I am glad I found a solution. I hope this gets resolved in future versions $\endgroup$
    – Tim Stack
    Commented Feb 19, 2021 at 11:38
0
$\begingroup$

I ran into the same error in a different context. Maybe this will help debugging? I was trying to show a vertical violin plot with a continuous vs categorical comparison. I produced the same error when I put the categorical variable in the y argument and also set orient='v'. Obviously this doesn't make sense. I fixed it by swapping the arguments. Maybe there's a hidden orientation issue? Try forcing the orientation? seaborn.violinplot(x='Batch', y='Age', hue='Sex', data=ages, orient='v')

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.