Skip to content

Commit

Permalink
Merge pull request #1366 from janezd/scatter-plot-label-selection
Browse files Browse the repository at this point in the history
[ENH] Scatter plot and MDS: Show labels only for selected points
  • Loading branch information
astaric authored Jun 24, 2016
2 parents 2321c3e + 1007fc3 commit 8b453e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
19 changes: 16 additions & 3 deletions Orange/widgets/unsupervised/owmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class OWMDS(widget.OWWidget):
shape_value = settings.ContextSetting("")
size_value = settings.ContextSetting("")
label_value = settings.ContextSetting("")
label_only_selected = settings.Setting(False)

symbol_size = settings.Setting(8)
symbol_opacity = settings.Setting(230)
Expand Down Expand Up @@ -232,6 +233,9 @@ def __init__(self):
box, self, "label_value", label="Label:",
callback=self._on_label_index_changed, **common_options)
self.cb_label_value.setModel(self.labelvar_model)
gui.checkBox(
gui.indentedBox(box), self, 'label_only_selected',
'Label only selected points', callback=self._on_label_index_changed)

form = QtGui.QFormLayout(
labelAlignment=Qt.AlignLeft,
Expand Down Expand Up @@ -930,9 +934,18 @@ def jitter(x, factor=1, rstate=None):
self.plot.addItem(item)

if self._label_data is not None:
for (x, y), text_item in zip(self.embedding, self._label_data):
self.plot.addItem(text_item)
text_item.setPos(x, y)
if self.label_only_selected:
if self._selection_mask is not None:
for (x, y), text_item, selected \
in zip(self.embedding, self._label_data,
self._selection_mask):
if selected:
self.plot.addItem(text_item)
text_item.setPos(x, y)
else:
for (x, y), text_item in zip(self.embedding, self._label_data):
self.plot.addItem(text_item)
text_item.setPos(x, y)

self._legend_item = LegendItem()
viewbox = self.plot.getViewBox()
Expand Down
7 changes: 5 additions & 2 deletions Orange/widgets/utils/plot/owplotgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,11 @@ def animations_check_box(self, widget):
'''
self._check_box(widget, 'use_animations', 'Use animations', 'update_animations')

def _slider(self, widget, value, label, min_value, max_value, step, cb_name):
gui.hSlider(widget, self._plot, value, label=label, minValue=min_value, maxValue=max_value, step=step, callback=self._get_callback(cb_name))
def _slider(self, widget, value, label, min_value, max_value, step, cb_name,
show_number=False):
gui.hSlider(widget, self._plot, value, label=label, minValue=min_value,
maxValue=max_value, step=step, createLabel=show_number,
callback=self._get_callback(cb_name))

def point_size_slider(self, widget):
'''
Expand Down
12 changes: 8 additions & 4 deletions Orange/widgets/visualize/owscatterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def __init__(self):
vizrank_box = gui.hBox(box)
gui.separator(vizrank_box, width=common_options["labelWidth"])
self.vizrank_button = gui.button(
vizrank_box, self, "Rank Projections", callback=self.vizrank.reshow,
tooltip="Find projections with good class separation")
vizrank_box, self, "Score Plots", callback=self.vizrank.reshow,
tooltip="Find plots with good class separation")
self.vizrank_button.setEnabled(False)
gui.separator(box)

Expand Down Expand Up @@ -144,11 +144,15 @@ def __init__(self):

box = gui.vBox(self.controlArea, "Plot Properties")
g.add_widgets([g.ShowLegend, g.ShowGridLines], box)
gui.checkBox(box, self, value='graph.tooltip_shows_all',
label='Show all data on mouse hover')
gui.checkBox(
box, self, value='graph.tooltip_shows_all',
label='Show all data on mouse hover')
self.cb_class_density = gui.checkBox(
box, self, value='graph.class_density', label='Show class density',
callback=self.update_density)
gui.checkBox(
box, self, 'graph.label_only_selected',
'Label only selected points', callback=self.graph.update_labels)

self.zoom_select_toolbar = g.zoom_select_toolbar(
gui.vBox(self.controlArea, "Zoom/Select"), nomargin=True,
Expand Down
17 changes: 14 additions & 3 deletions Orange/widgets/visualize/owscatterplotgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ def _define_symbols():
class OWScatterPlotGraph(gui.OWComponent, ScaleScatterPlotData):
attr_color = ContextSetting("", ContextSetting.OPTIONAL)
attr_label = ContextSetting("", ContextSetting.OPTIONAL)
label_only_selected = Setting(False)
attr_shape = ContextSetting("", ContextSetting.OPTIONAL)
attr_size = ContextSetting("", ContextSetting.OPTIONAL)

Expand Down Expand Up @@ -842,7 +843,8 @@ def create_labels(self):
self.labels.append(ti)

def update_labels(self):
if not self.attr_label:
if not self.attr_label or \
self.label_only_selected and self.selection is None:
for label in self.labels:
label.setText("")
return
Expand All @@ -852,8 +854,13 @@ def update_labels(self):
formatter = self.raw_data.domain[self.attr_label].str_val
label_data = map(formatter, label_column)
black = pg.mkColor(0, 0, 0)
for label, text in zip(self.labels, label_data):
label.setText(text, black)
if self.label_only_selected:
for label, text, selected \
in zip(self.labels, label_data, self.selection):
label.setText(text if selected else "", black)
else:
for label, text in zip(self.labels, label_data):
label.setText(text, black)

def get_shape_index(self):
shape_index = -1
Expand Down Expand Up @@ -991,6 +998,8 @@ def select_by_rectangle(self, value_rect):
def unselect_all(self):
self.selection = None
self.update_colors(keep_colors=True)
if self.label_only_selected:
self.update_labels()
self.master.selection_changed()

def select(self, points):
Expand All @@ -1009,6 +1018,8 @@ def select(self, points):
else: # Handle shift and no modifiers
self.selection[indices] = True
self.update_colors(keep_colors=True)
if self.label_only_selected:
self.update_labels()
self.master.selection_changed()

def get_selection(self):
Expand Down

0 comments on commit 8b453e9

Please sign in to comment.