Skip to content

[ENH] Boxplot no longer stretches bars when this is uninformative #4176

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

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Orange/widgets/visualize/owboxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ def eventFilter(self, obj, event):

return super().eventFilter(obj, event)

@property
def show_stretched(self):
return self.stretched and self.group_var is not self.attribute

def reset_attrs(self):
domain = self.dataset.domain
self.attrs[:] = [
Expand Down Expand Up @@ -448,6 +452,7 @@ def reset_all_data(self):
self.update_display_box()

def grouping_changed(self):
self.controls.stretched.setDisabled(self.group_var is self.attribute)
self.apply_attr_sorting()
self.update_graph()

Expand All @@ -459,6 +464,7 @@ def select_box_items(self):
[c.conditions for c in temp_cond])

def attr_changed(self):
self.controls.stretched.setDisabled(self.group_var is self.attribute)
self.apply_group_sorting()
self.update_graph()

Expand Down Expand Up @@ -623,7 +629,7 @@ def _display_changed_disc(self):
self.attr_labels = [QGraphicsSimpleTextItem(lab)
for lab in self.label_txts_all]

if not self.stretched:
if not self.show_stretched:
if self.group_var:
self.labels = [
QGraphicsTextItem("{}".format(int(sum(cont))))
Expand Down Expand Up @@ -657,7 +663,7 @@ def _display_changed_disc(self):
bars, labels = box[::2], box[1::2]

self.__draw_group_labels(y, box_index)
if not self.stretched:
if not self.show_stretched:
self.__draw_row_counts(y, box_index)
if self.show_labels and self.attribute is not self.group_var:
self.__draw_bar_labels(y, bars, labels)
Expand Down Expand Up @@ -909,7 +915,7 @@ def draw_axis_disc(self):
Draw the horizontal axis and sets self.scale_x for discrete attributes
"""
assert not self.is_continuous
if self.stretched:
if self.show_stretched:
if not self.attr_labels:
return
step = steps = 10
Expand All @@ -936,7 +942,7 @@ def draw_axis_disc(self):
self.label_width = lab_width

right_offset = 0 # offset for the right label
if not self.stretched and self.labels:
if not self.show_stretched and self.labels:
if self.group_var:
rows = list(zip(self.conts, self.labels))
else:
Expand All @@ -959,7 +965,7 @@ def draw_axis_disc(self):
l.setZValue(100)
t = self.box_scene.addSimpleText(str(val), self._axis_font)
t.setPos(val * scale_x - t.boundingRect().width() / 2, 8)
if self.stretched:
if self.show_stretched:
self.scale_x *= 100

def label_group(self, stat, attr, mean_lab):
Expand Down Expand Up @@ -1071,7 +1077,7 @@ def strudel(self, dist, group_val_index=None):
for i, v in enumerate(dist):
if v < 1e-6:
continue
if self.stretched:
if self.show_stretched:
v /= ss
v *= self.scale_x
cond = [FilterDiscrete(attr, [i])]
Expand All @@ -1080,7 +1086,7 @@ def strudel(self, dist, group_val_index=None):
rect = FilterGraphicsRectItem(cond, cum + 1, -6, v - 2, 12)
rect.setBrush(QBrush(QColor(*colors[i])))
rect.setPen(QPen(Qt.NoPen))
if self.stretched:
if self.show_stretched:
tooltip = "{}: {:.2f}%".format(
values[i],
100 * dist[i] / sum(dist))
Expand Down
16 changes: 16 additions & 0 deletions Orange/widgets/visualize/tests/test_owboxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,22 @@ def test_unconditional_commit_on_new_signal(self):
self.send_signal(self.widget.Inputs.data, self.zoo)
apply.assert_called()

def test_stretching(self):
self.send_signal(self.widget.Inputs.data, self.heart)
enabled = self.widget.controls.stretched.isEnabled

self.__select_variable("chest pain")
self.__select_group("gender")
self.assertTrue(enabled())

self.__select_variable("gender")
self.__select_group("gender")
self.assertFalse(enabled())

self.__select_variable("gender")
self.__select_group("chest pain")
self.assertTrue(enabled())


class TestUtils(unittest.TestCase):
def test(self):
Expand Down