diff --git a/Orange/widgets/visualize/owboxplot.py b/Orange/widgets/visualize/owboxplot.py index 500802cf41c..b977550dd79 100644 --- a/Orange/widgets/visualize/owboxplot.py +++ b/Orange/widgets/visualize/owboxplot.py @@ -64,8 +64,11 @@ def __init__(self, dist): q.append(float(dist[0, i])) thresh_i += 1 if thresh_i == 3: + self.q25, self.median, self.q75 = q break - self.q25, self.median, self.q75, *_ = q + [-1] * 3 + else: + self.q25 = self.q75 = None + self.median = q[1] if len(q) == 2 else None class OWBoxPlot(widget.OWWidget): @@ -429,9 +432,10 @@ def display_changed(self): else: stat = self.stats[box_index] - if self.compare == OWBoxPlot.CompareMedians: + if self.compare == OWBoxPlot.CompareMedians and \ + stat.median is not None: pos = stat.median + 5 / self.scale_x - elif self.compare == OWBoxPlot.CompareMeans: + elif self.compare == OWBoxPlot.CompareMeans or stat.q25 is None: pos = stat.mean + 5 / self.scale_x else: pos = stat.q25 @@ -714,28 +718,31 @@ def move_label(label, frm, to): mean_lab.setPos(m, -22) line(m, -1) - msc = stat.median * self.scale_x - med_t = centered_text(stat.median, msc) - med_box_width2 = med_t.boundingRect().width() - line(msc) - - x = stat.q25 * self.scale_x - t = centered_text(stat.q25, x) - t_box = t.boundingRect() - med_left = msc - med_box_width2 - if x + t_box.width() / 2 >= med_left - 5: - move_label(t, x, med_left - t_box.width() - 5) - else: - line(x) - - x = stat.q75 * self.scale_x - t = centered_text(stat.q75, x) - t_box = t.boundingRect() - med_right = msc + med_box_width2 - if x - t_box.width() / 2 <= med_right + 5: - move_label(t, x, med_right + 5) - else: - line(x) + if stat.median is not None: + msc = stat.median * self.scale_x + med_t = centered_text(stat.median, msc) + med_box_width2 = med_t.boundingRect().width() + line(msc) + + if stat.q25 is not None: + x = stat.q25 * self.scale_x + t = centered_text(stat.q25, x) + t_box = t.boundingRect() + med_left = msc - med_box_width2 + if x + t_box.width() / 2 >= med_left - 5: + move_label(t, x, med_left - t_box.width() - 5) + else: + line(x) + + if stat.q75 is not None: + x = stat.q75 * self.scale_x + t = centered_text(stat.q75, x) + t_box = t.boundingRect() + med_right = msc + med_box_width2 + if x - t_box.width() / 2 <= med_right + 5: + move_label(t, x, med_right + 5) + else: + line(x) return labels @@ -755,17 +762,19 @@ def line(x0, y0, x1, y1, *args): var_line = line(stat.mean - stat.dev, 0, stat.mean + stat.dev, 0, box) var_line.setPen(self._pen_paramet) - mbox = QGraphicsRectItem(stat.q25 * scale_x, -height / 2, - (stat.q75 - stat.q25) * scale_x, height, - box) - mbox.setBrush(self._box_brush) - mbox.setPen(QPen(Qt.NoPen)) - mbox.setZValue(-200) - - median_line = line(stat.median, -height / 2, - stat.median, height / 2, box) - median_line.setPen(self._pen_median) - median_line.setZValue(-150) + if stat.q25 is not None and stat.q75 is not None: + mbox = QGraphicsRectItem(stat.q25 * scale_x, -height / 2, + (stat.q75 - stat.q25) * scale_x, height, + box) + mbox.setBrush(self._box_brush) + mbox.setPen(QPen(Qt.NoPen)) + mbox.setZValue(-200) + + if stat.median is not None: + median_line = line(stat.median, -height / 2, + stat.median, height / 2, box) + median_line.setPen(self._pen_median) + median_line.setZValue(-150) return box