Skip to content

Commit 84e1c87

Browse files
authored
Merge pull request #3122 from robertcv/fix_box_plot
[RFC][FIX] BoxPlot: Hide groups with no instances
2 parents 6dce0df + 79486f7 commit 84e1c87

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

Orange/widgets/visualize/owboxplot.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,11 @@ def compute_box_data(self):
407407
dataset, attr, self.group_var)
408408
if self.is_continuous:
409409
self.stats = [BoxData(cont, attr, i, self.group_var)
410-
for i, cont in enumerate(self.conts)]
411-
self.label_txts_all = self.group_var.values
410+
for i, cont in enumerate(self.conts)
411+
if np.sum(cont) > 0]
412+
self.label_txts_all = \
413+
[v for v, c in zip(self.group_var.values, self.conts)
414+
if np.sum(c) > 0]
412415
else:
413416
self.dist = distribution.get_distribution(dataset, attr)
414417
self.conts = []
@@ -528,14 +531,17 @@ def display_changed_disc(self):
528531
if self.group_var:
529532
self.labels = [
530533
QGraphicsTextItem("{}".format(int(sum(cont))))
531-
for cont in self.conts]
534+
for cont in self.conts if np.sum(cont) > 0]
532535
else:
533536
self.labels = [
534537
QGraphicsTextItem(str(int(sum(self.dist))))]
535538

536539
self.draw_axis_disc()
537540
if self.group_var:
538-
self.boxes = [self.strudel(cont, i) for i, cont in enumerate(self.conts)]
541+
self.boxes = \
542+
[self.strudel(cont, i) for i, cont in enumerate(self.conts)
543+
if np.sum(cont) > 0]
544+
self.conts = self.conts[np.sum(np.array(self.conts), axis=1) > 0]
539545
else:
540546
self.boxes = [self.strudel(self.dist)]
541547

@@ -767,6 +773,8 @@ def draw_axis_disc(self):
767773
Draw the horizontal axis and sets self.scale_x for discrete attributes
768774
"""
769775
if self.stretched:
776+
if not self.attr_labels:
777+
return
770778
step = steps = 10
771779
else:
772780
if self.group_var:

Orange/widgets/visualize/tests/test_owboxplot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ def test_label_overlap(self):
162162
QTest.qWait(3000)
163163
self.widget.hide()
164164

165+
def test_empty_groups(self):
166+
"""Test if groups with zero elements are not shown"""
167+
table = Table("cyber-security-breaches")
168+
self.send_signal(self.widget.Inputs.data, table)
169+
self.__select_variable("US State")
170+
self.__select_group("US State")
171+
self.assertEqual(52, len(self.widget.boxes))
172+
173+
# select rows with US State equal to TX or MO
174+
use_indexes = np.array([0, 1, 25, 26, 27])
175+
table.X = table.X[use_indexes]
176+
self.send_signal(self.widget.Inputs.data, table)
177+
self.assertEqual(2, len(self.widget.boxes))
178+
165179
def _select_data(self):
166180
items = [item for item in self.widget.box_scene.items()
167181
if isinstance(item, FilterGraphicsRectItem)]

0 commit comments

Comments
 (0)