1
$\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$
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$
0
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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