mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.18k stars 1.89k forks source link

Violinplot breaks when `x` parameter is removed #3710

Closed mlldantas closed 3 weeks ago

mlldantas commented 3 weeks ago

Description

I encountered an issue while using the sns.violinplot function from Seaborn. When I remove the x parameter, the function breaks and throws an error. It is worth mentioning this code was working a few days ago and is now breaking.

A self-contained example that works:

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("tips")

plt.figure(figsize=(10, 6))
sns.violinplot(x='day', y='total_bill', hue='sex', data=data, split=True, inner="quart", 
                 palette={"Male": "blue", "Female": "pink"}, bw=.3, cut=1, linewidth=1, alpha=0.8, saturation=0.9)

plt.xlabel('Day')
plt.ylabel('Total bill')

plt.show()

By only removing the x` parameter, i.e.:

sns.violinplot(y='total_bill', hue='sex', data=data, split=True, inner="quart", 
                 palette={"Male": "blue", "Female": "pink"}, bw=.3, cut=1, linewidth=1, alpha=0.8, saturation=0.9)

I get the following error:


StopIteration Traceback (most recent call last) Cell In[2], line 9 7 # Create a split violin plot 8 plt.figure(figsize=(10, 6)) ----> 9 sns.violinplot(y='total_bill', hue='sex', data=data, 10 split=True, inner="quart", palette={"Male": "blue", "Female": "pink"}, 11 bw=.3, cut=1, linewidth=1, alpha=0.8, saturation=0.9) 13 # Add titles and labels 14 plt.title('Distribution of Total Bill by Day and Gender')

File ~/anaconda3/lib/python3.11/site-packages/seaborn/categorical.py:2305, in violinplot(data, x, y, hue, order, hue_order, bw, cut, scale, scale_hue, gridsize, width, inner, split, dodge, orient, linewidth, color, palette, saturation, ax, *kwargs) 2297 def violinplot( 2298 data=None, , x=None, y=None, hue=None, order=None, hue_order=None, 2299 bw="scott", cut=2, scale="area", scale_hue=True, gridsize=100, (...) 2302 ax=None, **kwargs, 2303 ): -> 2305 plotter = _ViolinPlotter(x, y, hue, data, order, hue_order, 2306 bw, cut, scale, scale_hue, gridsize, 2307 width, inner, split, dodge, orient, linewidth, 2308 color, palette, saturation) 2310 if ax is None: 2311 ax = plt.gca()

File ~/anaconda3/lib/python3.11/site-packages/seaborn/categorical.py:902, in _ViolinPlotter.init(self, x, y, hue, data, order, hue_order, bw, cut, scale, scale_hue, gridsize, width, inner, split, dodge, orient, linewidth, color, palette, saturation) 896 def init(self, x, y, hue, data, order, hue_order, 897 bw, cut, scale, scale_hue, gridsize, 898 width, inner, split, dodge, orient, linewidth, 899 color, palette, saturation): 901 self.establish_variables(x, y, hue, data, orient, order, hue_order) --> 902 self.establish_colors(color, palette, saturation) 903 self.estimate_densities(bw, cut, scale, scale_hue, gridsize) 905 self.gridsize = gridsize

File ~/anaconda3/lib/python3.11/site-packages/seaborn/categorical.py:696, in _CategoricalPlotter.establish_colors(self, color, palette, saturation) 693 levels = self.hue_names 694 palette = [palette[l] for l in levels] --> 696 colors = color_palette(palette, n_colors) 698 # Desaturate a bit because these are patches 699 if saturation < 1:

File ~/anaconda3/lib/python3.11/site-packages/seaborn/palettes.py:246, in color_palette(palette, n_colors, desat, as_cmap) 242 if not as_cmap: 243 244 # Always return as many colors as we asked for 245 pal_cycle = cycle(palette) --> 246 palette = [next(palcycle) for in range(n_colors)] 248 # Always return in r, g, b tuple format 249 try:

File ~/anaconda3/lib/python3.11/site-packages/seaborn/palettes.py:246, in (.0) 242 if not as_cmap: 243 244 # Always return as many colors as we asked for 245 pal_cycle = cycle(palette) --> 246 palette = [next(palcycle) for in range(n_colors)] 248 # Always return in r, g, b tuple format 249 try:

StopIteration:

I understand that when the x parameter is removed, there should only be one split violinplot, instead of several split by categories in the x parameter.

Python version: 3.11.7

Seaborn version: 0.13.2

Matplotlib version: 3.8.4

OS: Ubuntu 22.04.1

mwaskom commented 3 weeks ago

I'm not able to replicate:

image

Based on your traceback (which references some internal objects that don't exist anymore), you're not actually using seaborn 0.13.2 in this environment.

mlldantas commented 3 weeks ago

Oh gosh, this is embarrassing. I had already upgraded my Seaborn version, but I guess Conda is automatically downgrading it when I update other packages. At least, this is what seems to be happening. I forced the update once again via GitHub and a specific version (0.13.2) and it worked. Thanks for the help!