seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

[Boxplot] Gap Inside Grouped Boxes

Open schulzp opened this issue 6 years ago • 2 comments

Hi,

thanks for the seaborn library, it's a great tool!

I'd like to suggest an aesthetic improvement: A configurable gap between boxes inside a group of boxes: Boxplot with Gap

Since I'm currently writing my thesis, I'm a bit short on time, all I've got for now is a patch. If you consider this feature worthwhile, I'd create a proper pull request, of course.

Index: categorical.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- categorical.py	(date 1528960332000)
+++ categorical.py	(date 1528960332000)
@@ -366,6 +366,7 @@
         n_levels = len(self.hue_names)
         if self.dodge:
-            each_width = self.width / n_levels
+            each_width = (self.width - (self.gap * (n_levels - 1))) / n_levels
             offsets = np.linspace(0, self.width - each_width, n_levels)
             offsets -= offsets.mean()
         else:
@@ -436,13 +437,14 @@
 
     def __init__(self, x, y, hue, data, order, hue_order,
                  orient, color, palette, saturation,
-                 width, dodge, fliersize, linewidth):
+                 width, gap, dodge, fliersize, linewidth):
 
         self.establish_variables(x, y, hue, data, orient, order, hue_order)
         self.establish_colors(color, palette, saturation)
 
         self.dodge = dodge
         self.width = width
+        self.gap = gap
         self.fliersize = fliersize
 
         if linewidth is None:
@@ -2203,12 +2205,12 @@
 
 def boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None,
             orient=None, color=None, palette=None, saturation=.75,
-            width=.8, dodge=True, fliersize=5, linewidth=None,
+            width=.8, gap=0.1, dodge=True, fliersize=5, linewidth=None,
             whis=1.5, notch=False, ax=None, **kwargs):
 
     plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
                           orient, color, palette, saturation,
-                          width, dodge, fliersize, linewidth)
+                          width, gap, dodge, fliersize, linewidth)
 
     if ax is None:
         ax = plt.gca()

Please let me know what you think.

Cheers, Peter

schulzp avatar Jun 14 '18 07:06 schulzp

I think my preferred way of allowing this would not be adding a new parameter but instead letting dodge take a continuous value (in addition to True which would maintain current behavior).

A complete PR would need to be extended to other related functions along with tests and documentation.

mwaskom avatar Jun 15 '18 14:06 mwaskom

I also don't think that dodge/gap should affect the width of the plot elements; it should only influence where they are placed.

mwaskom avatar Jun 15 '18 14:06 mwaskom