Skip to content

Commit b424872

Browse files
authored
Merge pull request #4189 from PrimozGodec/pca-legend
OWPCA: add legend
2 parents fba5245 + 02fdac3 commit b424872

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

Orange/widgets/unsupervised/owdbscan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def __init__(self):
109109
self.plot = SliderGraph(
110110
x_axis_label="Data items sorted by score",
111111
y_axis_label="Distance to the k-th nearest neighbour",
112-
background="w", callback=self._on_cut_changed
112+
callback=self._on_cut_changed
113113
)
114114

115115
self.mainArea.layout().addWidget(self.plot)

Orange/widgets/unsupervised/owpca.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
# Maximum number of PCA components that we can set in the widget
1818
MAX_COMPONENTS = 100
19+
LINE_NAMES = ["component variance", "cumulative variance"]
1920

2021

2122
class OWPCA(widget.OWWidget):
@@ -83,13 +84,13 @@ def __init__(self):
8384
self.variance_spin.setSuffix("%")
8485

8586
form.addRow("Components:", self.components_spin)
86-
form.addRow("Variance covered:", self.variance_spin)
87+
form.addRow("Explained variance:", self.variance_spin)
8788

8889
# Options
8990
self.options_box = gui.vBox(self.controlArea, "Options")
9091
self.normalize_box = gui.checkBox(
9192
self.options_box, self, "normalize",
92-
"Normalize data", callback=self._update_normalize
93+
"Normalize variables", callback=self._update_normalize
9394
)
9495

9596
self.maxp_spin = gui.spin(
@@ -194,7 +195,7 @@ def _setup_plot(self):
194195

195196
self.plot.update(
196197
numpy.arange(1, p+1), [explained_ratio[:p], explained[:p]],
197-
[Qt.red, Qt.darkYellow], cutpoint_x=cutpos)
198+
[Qt.red, Qt.darkYellow], cutpoint_x=cutpos, names=LINE_NAMES)
198199

199200
self._update_axis()
200201

Orange/widgets/utils/slidergraph.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class SliderGraph(PlotWidget):
2424
Plot background color
2525
"""
2626

27-
def __init__(self, x_axis_label, y_axis_label, callback, background="w"):
28-
super().__init__(background=background)
27+
def __init__(self, x_axis_label, y_axis_label, callback):
28+
super().__init__(background="w")
2929

3030
axis = self.getAxis("bottom")
3131
axis.setLabel(x_axis_label)
@@ -49,34 +49,51 @@ def __init__(self, x_axis_label, y_axis_label, callback, background="w"):
4949
self.selection_limit = None
5050
self.data_increasing = None # true if data mainly increasing
5151

52-
def update(self, x, y, colors, cutpoint_x=None, selection_limit=None):
52+
def update(self, x, y, colors, cutpoint_x=None, selection_limit=None,
53+
names=None):
5354
"""
5455
Function replots a graph.
5556
5657
Parameters
5758
----------
5859
x : np.ndarray
5960
One-dimensional array with X coordinates of the points
60-
y : list
61+
y : array-like
6162
List of np.ndarrays that contains an array of Y values for each
6263
sequence.
63-
colors : list
64+
colors : array-like
6465
List of Qt colors (eg. Qt.red) for each sequence.
6566
cutpoint_x : int, optional
6667
A starting cutpoint - the location of the vertical line.
6768
selection_limit : tuple
6869
The tuple of two values that limit the range for selection.
70+
names : array-like
71+
The name of each sequence that shows in the legend, if None
72+
legend is not shown.
73+
legend_anchor : array-like
74+
The anchor of the legend in the graph
6975
"""
7076
self.clear_plot()
77+
if names is None:
78+
names = [None] * len(y)
79+
7180
self.sequences = y
7281
self.x = x
7382
self.selection_limit = selection_limit
7483

7584
self.data_increasing = [np.sum(d[1:] - d[:-1]) > 0 for d in y]
7685

7786
# plot sequence
78-
for s, c in zip(y, colors):
79-
self.plot(x, s, pen=mkPen(QColor(c), width=2), antialias=True)
87+
for s, c, n, inc in zip(y, colors, names, self.data_increasing):
88+
c = QColor(c)
89+
self.plot(x, s, pen=mkPen(c, width=2), antialias=True)
90+
91+
if n is not None:
92+
label = TextItem(
93+
text=n, anchor=(0, 1), color=QColor(0, 0, 0, 128))
94+
label.setPos(x[-1], s[-1])
95+
self._set_anchor(label, len(x) - 1, inc)
96+
self.addItem(label)
8097

8198
self._plot_cutpoint(cutpoint_x)
8299
self.setRange(xRange=(x.min(), x.max()),

0 commit comments

Comments
 (0)