mwaskom / seaborn

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

Boxplot errors when `dodge=True` without `hue` #3553

Closed Asif-Iqbal-Bhatti closed 10 months ago

Asif-Iqbal-Bhatti commented 11 months ago

Hello,

I want to report a bug when using the latest version of Seaborn. I checked with 0.12.2 there was no error.The bug is: File "/mnt/c/Users/*/Desktop/Gent_presentation_2023/1_Effect_of_Ti_addition/2-formation_energy/all_Tix_analysis/./boxplot.py", line 287, in statplot_cols() File "/mnt/c/Users/**/Desktop/Gent_presentation_2023/1_Effect_of_Ti_addition/2-formation_energy/all_Tix_analysis/./boxplot.py", line 59, in statplot_cols pp = sns.boxplot( File "/home/**/miniconda3/lib/python3.10/site-packages/seaborn/categorical.py", line 1619, in boxplot p.plot_boxes( File "/home/**/miniconda3/lib/python3.10/site-packages/seaborn/categorical.py", line 637, in plot_boxes self._dodge(sub_vars, data) File "/home/**/miniconda3/lib/python3.10/site-packages/seaborn/categorical.py", line 391, in _dodge hue_idx = self._hue_map.levels.index(keys["hue"]) KeyError: 'hue'

Can you check why is that?

mwaskom commented 11 months ago

I cannot help without a reproducible example.

Asif-Iqbal-Bhatti commented 11 months ago

Okay i will provide the test case in few moments

Asif-Iqbal-Bhatti commented 11 months ago

all_Tix_analysis.zip

I have uploaded the zip file which contains the code and the file which you can run on your machine to reproduce the error.

mwaskom commented 11 months ago

Please provide the code here and ideally adapt to one of the example datasets.

mwaskom commented 11 months ago

The contributing guidelines contain a list of what must be included to make a bug report actionable for maintainers: https://github.com/mwaskom/seaborn/blob/master/.github/CONTRIBUTING.md#reporting-bugs

thuiop commented 11 months ago

Didn't try it but I'm pretty sure the issue here is that you set dodge to True without providing a hue parameter.

Asif-Iqbal-Bhatti commented 11 months ago

In previous version 0.12 i did not provide hue and the dodge was set to True and it works but with 0.13 it did not.

thuiop commented 11 months ago

Indeed, as mentioned in the doc dodge should a value of auto to get the same behaviour. I don't really see why you pass the dodge parameter explicitely here anyway since you do not use hue.

mwaskom commented 11 months ago

Thanks @thuiop that suggests a reprex:

sns.boxplot(tips, x="tip", y="day", dodge=True)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In [87], line 1
----> 1 sns.boxplot(tips, x="tip", y="day", dodge=True)

File ~/code/seaborn/seaborn/categorical.py:1617, in boxplot(data, x, y, hue, order, hue_order, orient, color, palette, saturation, fill, dodge, width, gap, whis, linecolor, linewidth, fliersize, hue_norm, native_scale, log_scale, formatter, legend, ax, **kwargs)
   1610 color = _default_color(
   1611     ax.fill_between, hue, color,
   1612     {k: v for k, v in kwargs.items() if k in ["c", "color", "fc", "facecolor"]},
   1613     saturation=saturation,
   1614 )
   1615 linecolor = p._complement_color(linecolor, color, p._hue_map)
-> 1617 p.plot_boxes(
   1618     width=width,
   1619     dodge=dodge,
   1620     gap=gap,
   1621     fill=fill,
   1622     whis=whis,
   1623     color=color,
   1624     linecolor=linecolor,
   1625     linewidth=linewidth,
   1626     fliersize=fliersize,
   1627     plot_kws=kwargs,
   1628 )
   1630 p._add_axis_labels(ax)
   1631 p._adjust_cat_axis(ax, axis=p.orient)

File ~/code/seaborn/seaborn/categorical.py:635, in _CategoricalPlotter.plot_boxes(self, width, dodge, gap, fill, whis, color, linecolor, linewidth, fliersize, plot_kws)
    633 data = pd.DataFrame({self.orient: positions, "width": orig_width})
    634 if dodge:
--> 635     self._dodge(sub_vars, data)
    636 if gap:
    637     data["width"] *= 1 - gap

File ~/code/seaborn/seaborn/categorical.py:391, in _CategoricalPlotter._dodge(self, keys, data)
    389 def _dodge(self, keys, data):
    390     """Apply a dodge transform to coordinates in place."""
--> 391     hue_idx = self._hue_map.levels.index(keys["hue"])
    392     n = len(self._hue_map.levels)
    393     data["width"] /= n

AttributeError: 'NoneType' object has no attribute 'index'

Which looks similar (if not identical) to the reported traceback.

However as noted, dodge=True in v0.12 would have simply been ignored, so this is easy to work around.

abdulwaheedsoudagar commented 11 months ago

This issue is seen in boxenplot and violinplot, Is fix required for this?

sns.boxenplot(tips, x="tip", y="day",dodge=True) sns.violinplot(tips, x="tip", y="day",dodge=True)

mwaskom commented 11 months ago

The dodge operation is undefined when you haven’t assigned a hue variable. There should probably be a fix to handle this more gracefully, but the existing bug is not preventing you from doing anything.

abdulwaheedsoudagar commented 11 months ago

The dodge operation is undefined when you haven’t assigned a hue variable. There should probably be a fix to handle this more gracefully, but the existing bug is not preventing you from doing anything.

What I meant was, Can I submit a pull request to resolve this issue?