Skip to content

Commit f76ca3c

Browse files
committed
OWLinePlot: legible bottom axis labels
1 parent 735ced5 commit f76ca3c

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

Orange/widgets/visualize/owlineplot.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ class LinePlotStyle:
9191
MEAN_DARK_FACTOR = 110
9292

9393

94+
class LinePlotAxisItem(pg.AxisItem):
95+
def __init__(self, *args, **kwargs):
96+
super().__init__(*args, **kwargs)
97+
self._ticks = None
98+
99+
def set_ticks(self, ticks):
100+
self._ticks = dict(enumerate(ticks, 1)) if ticks else None
101+
102+
def tickStrings(self, values, scale, spacing):
103+
if not self._ticks:
104+
return []
105+
strings = []
106+
for v in values:
107+
v = v * scale
108+
if float(v).is_integer():
109+
strings.append(self._ticks.get(int(v), ""))
110+
return strings
111+
112+
94113
class LinePlotViewBox(ViewBox):
95114
selection_changed = Signal(np.ndarray)
96115

@@ -172,8 +191,10 @@ def reset(self):
172191

173192
class LinePlotGraph(pg.PlotWidget):
174193
def __init__(self, parent):
194+
self.bottom_axis = LinePlotAxisItem(orientation="bottom")
175195
super().__init__(parent, viewBox=LinePlotViewBox(),
176-
background="w", enableMenu=False)
196+
background="w", enableMenu=False,
197+
axisItems={"bottom": self.bottom_axis})
177198
self.view_box = self.getViewBox()
178199
self.selection = set()
179200
self.legend = self._create_legend(((1, 0), (1, 0)))
@@ -213,7 +234,7 @@ def reset(self):
213234
self.selection = set()
214235
self.view_box.reset()
215236
self.clear()
216-
self.getAxis('bottom').setTicks(None)
237+
self.getAxis('bottom').set_ticks(None)
217238
self.legend.hide()
218239

219240
def select_button_clicked(self):
@@ -593,8 +614,8 @@ def setup_plot(self):
593614
if self.data is None:
594615
return
595616

596-
ticks = [[(i, a.name) for i, a in enumerate(self.graph_variables, 1)]]
597-
self.graph.getAxis('bottom').setTicks(ticks)
617+
ticks = [a.name for a in self.graph_variables]
618+
self.graph.getAxis("bottom").set_ticks(ticks)
598619
self.plot_groups()
599620
self.apply_selection()
600621
self.graph.view_box.enableAutoRange()

Orange/widgets/visualize/tests/test_owlineplot.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,7 @@ def test_lines_intersection(self):
290290
for i in range(y.shape[1])])
291291
i = line_intersects_profiles(a, b, table)
292292
np.testing.assert_array_equal(np.array([False, True, True, True]), i)
293+
294+
295+
if __name__ == "__main__":
296+
unittest.main()

0 commit comments

Comments
 (0)