From d879a01efcd6292a7473d84ee07c9a58c125f42c Mon Sep 17 00:00:00 2001 From: jh777916 Date: Wed, 6 Sep 2023 14:11:17 +0200 Subject: [PATCH] Dirac and non dirac barplot seems to be OK. Legend is now clickable. Some bug when superposing curves and bars : legend order, choice of the color (color cycle --- .../src/python/test/TestDesktop_test.py | 53 ++++++++++++-- tools/CurvePlot/src/python/views/CurveView.py | 69 ++++++++++++++----- tools/CurvePlot/src/python/views/XYView.py | 22 +++++- 3 files changed, 120 insertions(+), 24 deletions(-) diff --git a/tools/CurvePlot/src/python/test/TestDesktop_test.py b/tools/CurvePlot/src/python/test/TestDesktop_test.py index cfcc38cbc..0e6340e33 100755 --- a/tools/CurvePlot/src/python/test/TestDesktop_test.py +++ b/tools/CurvePlot/src/python/test/TestDesktop_test.py @@ -77,6 +77,8 @@ class TestDesktop_test(QMainWindow): self.memAction.triggered.connect(self.memPrint) self.perfTestAction.triggered.connect(self.perfTest) self.addBarPlotAction.triggered.connect(self.addBarplot) + self.addBarPlotNonDiracAction.triggered.connect(self.addBarplotNonDirac) + def generateID(self): self._currID += 1 @@ -94,6 +96,7 @@ class TestDesktop_test(QMainWindow): self.memActionID = self.generateID() self.perfActionID = self.generateID() self.addBarPlotActionID = self.generateID() + self.addBarPlotNonDiracActionID = self.generateID() # Menus self.etudeMenuID = self.generateID() @@ -110,7 +113,8 @@ class TestDesktop_test(QMainWindow): self.addTabAction = ca(self.addTabActionID, "Add tab", "Add tab", "", "") self.memAction = ca(self.memActionID, "Display used mem", "Display used mem", "", "") self.perfTestAction = ca(self.perfActionID, "Perf test", "Perf test", "", "") - self.addBarPlotAction = ca(self.addBarPlotActionID, "Add BarPlot", "Add BarPlot", "", "") + self.addBarPlotAction = ca(self.addBarPlotActionID, "Add BarPlot - Dirac", "Add BarPlot - Dirac", "", "") + self.addBarPlotNonDiracAction = ca(self.addBarPlotActionID, "Add BarPlot - Non Dirac", "Add BarPlot- Non Dirac", "", "") def createToolbars(self): pass @@ -133,6 +137,7 @@ class TestDesktop_test(QMainWindow): self._sgPyQt.createMenu(self.memAction, curveMenu) self._sgPyQt.createMenu(self.perfTestAction, curveMenu) self._sgPyQt.createMenu(self.addBarPlotAction, curveMenu) + self._sgPyQt.createMenu(self.addBarPlotNonDiracAction, curveMenu) dummyMenu = self._sgPyQt.createMenu( "Dummy", -1, self.dummyMenuID, self._sgPyQt.defaultMenuGroup() ) self._sgPyQt.createMenu(self.addTabAction, dummyMenu) @@ -189,15 +194,53 @@ class TestDesktop_test(QMainWindow): @pyqtSlot() def addBarplot(self): - x, height = self.__generateRandomData() - width = np.full(x.size, 0.05) + energy_val = np.array([8.01, 8.33, 8.67, 8.99, 9.01, 9.33, 9.99]) + # energy_val = np.array([8.15, 8.42, 8.75, 8.88, 9.1, 9.55, 9.79]) + + intensity = np.random.rand(energy_val.size) * 5 isDirac = True - _, ps_id = curveplot.AddBarPlot(x, height, width, isDirac, - x_label="the x axis", y_label="the y axis", append=True) + width_dirac = np.full(energy_val.size, 0.01) + + _, ps_id = curveplot.AddBarPlot(energy_val, intensity, width_dirac, isDirac, + x_label="Energy", y_label="Intensity", append=True) + + curveplot.SetLegendVisible(ps_id, True) + if self.cnt >= 0: + QTimer.singleShot(self.timeLap, self, SLOT("itemDel()")) + + @pyqtSlot() + def addBarplotNonDirac(self): + # Testing with TableModel + from curveplot import TableModel + from random import random + + energy_inf = np.array([8.01, 8.33, 8.67, 8.99, 9.01, 9.33, 9.99]) + energy_sup = np.array([8.33, 8.67, 8.99, 9.01, 9.33, 9.99, 10.01]) + deltaE = energy_sup - energy_inf + intensity = np.random.rand(energy_inf.size) * 5 + isDirac = False + + t = TableModel(None) + t.setTitle("Non Dirac data") + t.addColumn(energy_inf) # x : energy inf + t.addColumn(intensity) # height : intensity + t.addColumn(deltaE) # width : delta energy + t.setColumnTitle(0, "Energy") + t.setColumnTitle(1, "Intensity") + t.setColumnTitle(2, "DeltaE") + + bar_label = "Some spectrum" + cont = curveplot.PlotController.GetInstance() + crv_id, ps_id = cont.plotBarPlotFromTable(t, isDirac, + x_col_index=0, height_col_index=1, width_col_index=2, + curve_label=bar_label, append=True) + curveplot.SetLegendVisible(ps_id, True) + if self.cnt >= 0: QTimer.singleShot(self.timeLap, self, SLOT("itemDel()")) + def clearPlotSet(self): curveplot.ClearPlotSet() diff --git a/tools/CurvePlot/src/python/views/CurveView.py b/tools/CurvePlot/src/python/views/CurveView.py index 708ce92e2..c585e0c18 100644 --- a/tools/CurvePlot/src/python/views/CurveView.py +++ b/tools/CurvePlot/src/python/views/CurveView.py @@ -33,10 +33,12 @@ class CurveView(View): self._isHighlighted = False self._initialLineWidth = None self._initialEdgeColorBar = None + self._initialAlpha = None self._parentXYView = parentXYView self._marker = None self._color = None + self._opacity = None self._lineStyle = None def setMPLAxes(self, axes): @@ -59,12 +61,18 @@ class CurveView(View): x_idx, height_idx, width_idx = m.getXAxisIndex(), m.getHeightIndex(), m.getWidthIndex() x_align = "center" if m.getIsDirac() else "edge" d = self._model.getTable().getData() + self._mplBars = self._mplAxes.bar(d[:, x_idx], d[:, height_idx], width=d[:, width_idx], - align=x_align, label=m._title, picker=self._PICKER_PRECISION_BAR) - self._initialLineWidth = self._mplBars[0].get_linewidth() - print("Bar initial lw : ", self._initialLineWidth) + align=x_align, label=m._title, alpha= 0.7, + picker=self._PICKER_PRECISION_BAR) + + self._initialLineWidth = 0. #self._mplBars[0].get_linewidth() self._initialEdgeColorBar = self._mplBars[0].get_edgecolor() - print("Bar initial ec : ", self._initialEdgeColorBar) + self._initialAlpha = self._mplBars[0].get_alpha() + + Logger.Debug(" > Bar initial lw : {}".format(self._initialLineWidth)) + Logger.Debug(" > Bar initial ec : {}".format(self._initialEdgeColorBar)) + Logger.Debug(" > Bar initial alpha : {}".format(self._initialAlpha)) else: x_idx, y_idx = m.getXAxisIndex(), m.getYAxisIndex() d = self._model.getTable().getData() @@ -93,9 +101,11 @@ class CurveView(View): if self._mplBars is None: return color = self.getColor() + opacity = self.getAlpha() self.erase() self.draw() self.setColor(color) + self.setAlpha(opacity) self.toggleHighlight(self._isHighlighted, force=True) else: if self._mplLines is None: @@ -110,17 +120,20 @@ class CurveView(View): self.toggleHighlight(self._isHighlighted, force=True) def setLineStyle(self, lin_style): - lin = self._mplLines[0] - lin.set_linestyle(lin_style) + if (self._model._name != "BarModel"): + lin = self._mplLines[0] + lin.set_linestyle(lin_style) def getLineStyle(self): - if self._mplLines is None: - return None - return self._mplLines[0].get_linestyle() + if (self._model._name != "BarModel"): + if self._mplLines is None: + return None + return self._mplLines[0].get_linestyle() def setMarker(self, marker): - lin = self._mplLines[0] - lin.set_marker(marker) + if (self._model._name != "BarModel"): + lin = self._mplLines[0] + lin.set_marker(marker) def getMarker(self): if self._mplLines is None: @@ -132,12 +145,14 @@ class CurveView(View): bar = self._mplBars.patches if highlight and (force or not self._isHighlighted): for rect in bar : - rect.set_linewidth(2*self._initialLineWidth) + rect.set_linewidth(1.) + rect.set_alpha(1) rect.set_edgecolor("red") self._isHighlighted = True elif not highlight and (force or self._isHighlighted): for rect in bar : rect.set_linewidth(self._initialLineWidth) + rect.set_alpha(self._initialAlpha) rect.set_edgecolor(self._initialEdgeColorBar) self._isHighlighted = False else: @@ -162,8 +177,8 @@ class CurveView(View): m = self._model # Check si BarModel ou CurveModel if (m._name == "BarModel"): - bar = self._mplBars - bar.set_color(rgb_color) + for rect in self._mplBars : + rect.set_facecolor(rgb_color) else: lin = self._mplLines[0] lin.set_color(rgb_color) @@ -172,10 +187,32 @@ class CurveView(View): m = self._model # Check si BarModel ou CurveModel if (m._name == "BarModel"): - if self._mplbars is None: + if self._mplBars is None: return None - return self._mplBars[0].get_color() + return self._mplBars[0].get_facecolor() else: if self._mplLines is None: return None return self._mplLines[0].get_color() + + def setAlpha(self, opacity): + m = self._model + # Check si BarModel ou CurveModel + if (m._name == "BarModel"): + bar = self._mplBars + bar.set_alpha(opacity) + else: + lin = self._mplLines[0] + lin.set_alpha(opacity) + + def getAlpha(self): + m = self._model + # Check si BarModel ou CurveModel + if (m._name == "BarModel"): + if self._mplBars is None: + return None + return self._mplBars[0].get_alpha() + else: + if self._mplLines is None: + return None + return self._mplLines[0].get_alpha() diff --git a/tools/CurvePlot/src/python/views/XYView.py b/tools/CurvePlot/src/python/views/XYView.py index 128736f23..a00310fc8 100644 --- a/tools/CurvePlot/src/python/views/XYView.py +++ b/tools/CurvePlot/src/python/views/XYView.py @@ -124,7 +124,7 @@ class XYView(View): newC.setMPLAxes(self._mplAxes) newC.draw() if (self._model._curves[curveID]._name == "BarModel"): - print("Appending un bar model !") + Logger.Debug(" > Appending a bar model") else: newC.setMarker(self.getMarker(go_next=True)) self._curveViews[curveID] = newC @@ -178,20 +178,29 @@ class XYView(View): def onPick(self, event): """ MPL callback when picking """ + Logger.Debug(" > Event onPick : {}".format(event)) + if event.mouseevent.button == 1: selected_id = -1 a = event.artist + Logger.Debug(" > Artist picked : {}".format(a)) + + for crv_id, legHandler in enumerate(self._legend.legendHandles): + if legHandler is a : + selected_id = crv_id + Logger.Debug("Legend {} selected".format(selected_id)) for crv_id, cv in list(self._curveViews.items()): if (cv._model._name == "BarModel") : # Checks if the picked artist is in the list of Rectangles if a in cv._mplBars: selected_id = crv_id - print(selected_id, " barplot selected") + Logger.Debug("Barplot {} selected".format(selected_id)) else: if cv._mplLines[0] is a: selected_id = crv_id - print(selected_id, " curve selected") + Logger.Debug("Curve {} selected".format(selected_id)) + # Use the plotmanager so that other plot sets get their current reset: self._controller._plotManager.setCurrentCurve(selected_id) @@ -462,6 +471,13 @@ class XYView(View): ncol=1, fancybox=True, shadow=True, prop={'size':fontSize, 'style': 'italic'}) else: raise Exception("Invalid legend placement! Must be 'bottom' or 'right'") + + + # Trying to set legend items pickable + Logger.Debug(" > Legend handles : {}".format(self._legend.legendHandles)) + for handler in self._legend.legendHandles: # legend_handles in newer versions of matplotlib ... + handler.set_picker(True) + # Canvas must be drawn so we can adjust the figure placement: self._mplCanvas.draw() self.__adjustFigureMargins(withLegend=True) -- 2.39.2