From 2635ac245a20ee56a8a79642ffa471c5eda9eafe Mon Sep 17 00:00:00 2001 From: Vesna Tanko Date: Fri, 19 Apr 2019 15:21:26 +0200 Subject: [PATCH 1/2] OWLinePlot: legible bottom axis labels --- Orange/widgets/visualize/owlineplot.py | 29 ++++++++++++++++--- .../visualize/tests/test_owlineplot.py | 4 +++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Orange/widgets/visualize/owlineplot.py b/Orange/widgets/visualize/owlineplot.py index 51661f865e5..566d29c0307 100644 --- a/Orange/widgets/visualize/owlineplot.py +++ b/Orange/widgets/visualize/owlineplot.py @@ -91,6 +91,25 @@ class LinePlotStyle: MEAN_DARK_FACTOR = 110 +class LinePlotAxisItem(pg.AxisItem): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._ticks = None + + def set_ticks(self, ticks): + self._ticks = dict(enumerate(ticks, 1)) if ticks else None + + def tickStrings(self, values, scale, spacing): + if not self._ticks: + return [] + strings = [] + for v in values: + v = v * scale + if float(v).is_integer(): + strings.append(self._ticks.get(int(v), "")) + return strings + + class LinePlotViewBox(ViewBox): selection_changed = Signal(np.ndarray) @@ -172,8 +191,10 @@ def reset(self): class LinePlotGraph(pg.PlotWidget): def __init__(self, parent): + self.bottom_axis = LinePlotAxisItem(orientation="bottom") super().__init__(parent, viewBox=LinePlotViewBox(), - background="w", enableMenu=False) + background="w", enableMenu=False, + axisItems={"bottom": self.bottom_axis}) self.view_box = self.getViewBox() self.selection = set() self.legend = self._create_legend(((1, 0), (1, 0))) @@ -213,7 +234,7 @@ def reset(self): self.selection = set() self.view_box.reset() self.clear() - self.getAxis('bottom').setTicks(None) + self.getAxis('bottom').set_ticks(None) self.legend.hide() def select_button_clicked(self): @@ -593,8 +614,8 @@ def setup_plot(self): if self.data is None: return - ticks = [[(i, a.name) for i, a in enumerate(self.graph_variables, 1)]] - self.graph.getAxis('bottom').setTicks(ticks) + ticks = [a.name for a in self.graph_variables] + self.graph.getAxis("bottom").set_ticks(ticks) self.plot_groups() self.apply_selection() self.graph.view_box.enableAutoRange() diff --git a/Orange/widgets/visualize/tests/test_owlineplot.py b/Orange/widgets/visualize/tests/test_owlineplot.py index 26a6c9d336b..06794f3e60c 100644 --- a/Orange/widgets/visualize/tests/test_owlineplot.py +++ b/Orange/widgets/visualize/tests/test_owlineplot.py @@ -290,3 +290,7 @@ def test_lines_intersection(self): for i in range(y.shape[1])]) i = line_intersects_profiles(a, b, table) np.testing.assert_array_equal(np.array([False, True, True, True]), i) + + +if __name__ == "__main__": + unittest.main() From 99573dcfdd3a128b800a9e70918123c80f74b2fd Mon Sep 17 00:00:00 2001 From: janezd Date: Fri, 17 May 2019 10:06:21 +0200 Subject: [PATCH 2/2] OWLinePlot: Simplify code for tick labels; possibly fix for non-integer labels --- Orange/widgets/visualize/owlineplot.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Orange/widgets/visualize/owlineplot.py b/Orange/widgets/visualize/owlineplot.py index 566d29c0307..2cc1d5489c6 100644 --- a/Orange/widgets/visualize/owlineplot.py +++ b/Orange/widgets/visualize/owlineplot.py @@ -94,20 +94,13 @@ class LinePlotStyle: class LinePlotAxisItem(pg.AxisItem): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self._ticks = None + self._ticks = {} def set_ticks(self, ticks): - self._ticks = dict(enumerate(ticks, 1)) if ticks else None + self._ticks = dict(enumerate(ticks, 1)) if ticks else {} def tickStrings(self, values, scale, spacing): - if not self._ticks: - return [] - strings = [] - for v in values: - v = v * scale - if float(v).is_integer(): - strings.append(self._ticks.get(int(v), "")) - return strings + return [self._ticks.get(v * scale, "") for v in values] class LinePlotViewBox(ViewBox):