Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boxplot errors when dodge=True without hue #3553

Closed
Asif-Iqbal-Bhatti opened this issue Nov 8, 2023 · 12 comments · Fixed by #3605
Closed

Boxplot errors when dodge=True without hue #3553

Asif-Iqbal-Bhatti opened this issue Nov 8, 2023 · 12 comments · Fixed by #3605

Comments

@Asif-Iqbal-Bhatti
Copy link

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
Copy link
Owner

mwaskom commented Nov 8, 2023

I cannot help without a reproducible example.

@Asif-Iqbal-Bhatti
Copy link
Author

Okay i will provide the test case in few moments

@Asif-Iqbal-Bhatti
Copy link
Author

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
Copy link
Owner

mwaskom commented Nov 8, 2023

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

@mwaskom
Copy link
Owner

mwaskom commented Nov 8, 2023

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
Copy link
Contributor

thuiop commented Nov 8, 2023

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
Copy link
Author

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
Copy link
Contributor

thuiop commented Nov 8, 2023

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 mwaskom changed the title bug in boxplot Boxplot errors when dodge=True without hue Nov 9, 2023
@mwaskom mwaskom added this to the v0.13.1 milestone Nov 9, 2023
@mwaskom
Copy link
Owner

mwaskom commented Nov 9, 2023

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
Copy link
Contributor

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
Copy link
Owner

mwaskom commented Nov 15, 2023

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
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants