From 32aa5bece59f9bc8a059a2b3dc7ef511ef0fd120 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Fri, 20 Oct 2023 16:48:21 +0200
Subject: [PATCH] Results: CustomPlot: Add legend and change axes label
 fontsize for each plot.

---
 src/View/Geometry/PlotAC.py                   |  8 +--
 src/View/Geometry/PlotKPZ.py                  |  4 +-
 src/View/Geometry/PlotXY.py                   |  4 +-
 src/View/Results/CustomPlot/Plot.py           | 50 +++++++++++++++----
 src/View/Results/PlotH.py                     |  4 +-
 src/View/Results/PlotSedProfile.py            |  4 +-
 src/View/Results/PlotSedReach.py              |  4 +-
 src/View/Results/PlotXY.py                    |  4 +-
 src/View/SedimentLayers/Edit/Plot.py          |  2 +-
 src/View/SedimentLayers/Reach/Plot.py         |  4 +-
 src/View/SedimentLayers/Reach/Profile/Plot.py |  4 +-
 11 files changed, 60 insertions(+), 32 deletions(-)

diff --git a/src/View/Geometry/PlotAC.py b/src/View/Geometry/PlotAC.py
index 1d7bf8d8..7cc75184 100644
--- a/src/View/Geometry/PlotAC.py
+++ b/src/View/Geometry/PlotAC.py
@@ -72,11 +72,11 @@ class PlotAC(PamhyrPlot):
 
         self.canvas.axes.set_xlabel(
             _translate("MainWindow_reach", "Transverse abscissa (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("MainWindow_reach", "Height (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.figure.tight_layout()
 
@@ -176,11 +176,11 @@ class PlotAC(PamhyrPlot):
         self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)
         self.canvas.axes.set_xlabel(
             _translate("MainWindow_reach", "Abscisse en travers (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("MainWindow_reach", "Cote (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.figure.tight_layout()
 
diff --git a/src/View/Geometry/PlotKPZ.py b/src/View/Geometry/PlotKPZ.py
index 41b08a8f..79fbfe05 100644
--- a/src/View/Geometry/PlotKPZ.py
+++ b/src/View/Geometry/PlotKPZ.py
@@ -64,11 +64,11 @@ class PlotKPZ(PamhyrPlot):
 
         self.canvas.axes.set_xlabel(
             _translate("MainWindow_reach", "Kp (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("MainWindow_reach", "Height (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         kp = self.data.get_kp()
diff --git a/src/View/Geometry/PlotXY.py b/src/View/Geometry/PlotXY.py
index a851cc82..fcd55ecc 100644
--- a/src/View/Geometry/PlotXY.py
+++ b/src/View/Geometry/PlotXY.py
@@ -65,11 +65,11 @@ class PlotXY(PamhyrPlot):
         # Axes
         self.canvas.axes.set_xlabel(
             _translate("Geometry", "X (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("Geometry", "Y (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.axis("equal")
 
diff --git a/src/View/Results/CustomPlot/Plot.py b/src/View/Results/CustomPlot/Plot.py
index 4712c45b..9733448f 100644
--- a/src/View/Results/CustomPlot/Plot.py
+++ b/src/View/Results/CustomPlot/Plot.py
@@ -86,16 +86,19 @@ class CustomPlot(PamhyrPlot):
         if "0-meter" in self._y_axes and "1-m3s" in self._y_axes:
             m3s_axes = self._axes["1-m3s"]
 
+
+        lines = {}
         if "elevation" in self._y:
             meter_axes.set_ylim(
                 bottom=min(0, min(z_min)),
                 top=max(z_min) + 1
             )
 
-            meter_axes.plot(
+            line = meter_axes.plot(
                 kp, z_min,
-                color='grey', lw=1.
+                color='grey', lw=1.,
             )
+            lines["elevation"] = line
 
         if "water_elevation" in self._y:
             # Water elevation
@@ -111,10 +114,11 @@ class CustomPlot(PamhyrPlot):
                 top=max(water_z) + 1
             )
 
-            meter_axes.plot(
+            line = meter_axes.plot(
                 kp, water_z, lw=1.,
-                color='b',
+                color='blue',
             )
+            lines["water_elevation"] = line
 
             if "elevation" in self._y:
                 meter_axes.fill_between(
@@ -135,10 +139,20 @@ class CustomPlot(PamhyrPlot):
                 top=max(q) + 1
             )
 
-            m3s_axes.plot(
+            line = m3s_axes.plot(
                 kp, q, lw=1.,
                 color='r',
             )
+            lines["discharge"] = line
+
+        # Legend
+        lns = reduce(
+            lambda acc, l: acc + l,
+            map(lambda l: lines[l], lines),
+            []
+        )
+        labs = list(map(lambda l: self._trad[l], lines))
+        self.canvas.axes.legend(lns, labs, loc="lower left")
 
     def _customize_x_axes_time(self, ts, mode="time"):
         # Custom time display
@@ -198,6 +212,7 @@ class CustomPlot(PamhyrPlot):
         )
 
         x = ts
+        lines = {}
         if "elevation" in self._y:
             # Z min is constant in time
             z_min = profile.geometry.z_min()
@@ -208,10 +223,11 @@ class CustomPlot(PamhyrPlot):
                 )
             )
 
-            meter_axes.plot(
+            line = meter_axes.plot(
                 ts, ts_z_min,
                 color='grey', lw=1.
             )
+            lines["elevation"] = line
 
         if "water_elevation" in self._y:
             # Water elevation
@@ -222,10 +238,11 @@ class CustomPlot(PamhyrPlot):
                 top=max(z) + 1
             )
 
-            meter_axes.plot(
+            line = meter_axes.plot(
                 ts, z, lw=1.,
                 color='b',
             )
+            lines["water_elevation"] = line
 
             if "elevation" in self._y:
                 z_min = profile.geometry.z_min()
@@ -249,13 +266,24 @@ class CustomPlot(PamhyrPlot):
                 top=max(q) + 1
             )
 
-            m3s_axes.plot(
+            line = m3s_axes.plot(
                 ts, q, lw=1.,
                 color='r',
             )
+            lines["discharge"] = line
+
 
         self._customize_x_axes_time(ts)
 
+        # Legend
+        lns = reduce(
+            lambda acc, l: acc + l,
+            map(lambda l: lines[l], lines),
+            []
+        )
+        labs = list(map(lambda l: self._trad[l], lines))
+        self.canvas.axes.legend(lns, labs, loc="lower left")
+
     @timer
     def draw(self):
         self.canvas.axes.cla()
@@ -266,12 +294,12 @@ class CustomPlot(PamhyrPlot):
 
         self.canvas.axes.set_xlabel(
             self._trad[self._x],
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         self.canvas.axes.set_ylabel(
             self._trad[self._y_axes[0]],
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         for axes in self._y_axes[1:]:
@@ -281,7 +309,7 @@ class CustomPlot(PamhyrPlot):
             ax_new = self.canvas.axes.twinx()
             ax_new.set_ylabel(
                 self._trad[axes],
-                color='green', fontsize=12
+                color='green', fontsize=10
             )
             self._axes[axes] = ax_new
 
diff --git a/src/View/Results/PlotH.py b/src/View/Results/PlotH.py
index dd5a332b..2af2ea41 100644
--- a/src/View/Results/PlotH.py
+++ b/src/View/Results/PlotH.py
@@ -77,11 +77,11 @@ class PlotH(PamhyrPlot):
         # Axes
         self.canvas.axes.set_xlabel(
             _translate("Results", "Time (s)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("Results", "Discharge (m³/s)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         ts = list(self.results.get("timestamps"))
diff --git a/src/View/Results/PlotSedProfile.py b/src/View/Results/PlotSedProfile.py
index d16fb0b8..013480d9 100644
--- a/src/View/Results/PlotSedProfile.py
+++ b/src/View/Results/PlotSedProfile.py
@@ -122,11 +122,11 @@ class PlotSedProfile(PamhyrPlot):
 
         self.canvas.axes.set_xlabel(
             _translate("MainWindow_reach", "X (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("MainWindow_reach", "Height (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         x = profile.geometry.get_station()
diff --git a/src/View/Results/PlotSedReach.py b/src/View/Results/PlotSedReach.py
index 5cdc4ad7..9e49c076 100644
--- a/src/View/Results/PlotSedReach.py
+++ b/src/View/Results/PlotSedReach.py
@@ -203,11 +203,11 @@ class PlotSedReach(PamhyrPlot):
 
         self.canvas.axes.set_xlabel(
             _translate("MainWindow_reach", "Kp (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("MainWindow_reach", "Height (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         kp = reach.geometry.get_kp()
diff --git a/src/View/Results/PlotXY.py b/src/View/Results/PlotXY.py
index 5a513322..384e2813 100644
--- a/src/View/Results/PlotXY.py
+++ b/src/View/Results/PlotXY.py
@@ -78,11 +78,11 @@ class PlotXY(PamhyrPlot):
         # Axes
         self.canvas.axes.set_xlabel(
             _translate("Results", "X (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("Results", "Y (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.axis("equal")
 
diff --git a/src/View/SedimentLayers/Edit/Plot.py b/src/View/SedimentLayers/Edit/Plot.py
index 87cfe644..69e58f83 100644
--- a/src/View/SedimentLayers/Edit/Plot.py
+++ b/src/View/SedimentLayers/Edit/Plot.py
@@ -35,7 +35,7 @@ class Plot(PamhyrPlot):
         self.canvas.axes.axes.get_xaxis().set_visible(False)
         self.canvas.axes.set_ylabel(
             self._trad["height"],
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         if self.data is None:
diff --git a/src/View/SedimentLayers/Reach/Plot.py b/src/View/SedimentLayers/Reach/Plot.py
index 3664081c..1bea651f 100644
--- a/src/View/SedimentLayers/Reach/Plot.py
+++ b/src/View/SedimentLayers/Reach/Plot.py
@@ -46,11 +46,11 @@ class Plot(PamhyrPlot):
 
         self.canvas.axes.set_xlabel(
             self._trad["kp"],
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             self._trad["height"],
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         kp = self.data.get_kp()
diff --git a/src/View/SedimentLayers/Reach/Profile/Plot.py b/src/View/SedimentLayers/Reach/Profile/Plot.py
index 38adb990..5000ca9b 100644
--- a/src/View/SedimentLayers/Reach/Profile/Plot.py
+++ b/src/View/SedimentLayers/Reach/Profile/Plot.py
@@ -46,11 +46,11 @@ class Plot(PamhyrPlot):
 
         self.canvas.axes.set_xlabel(
             _translate("MainWindow_reach", "X (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
         self.canvas.axes.set_ylabel(
             _translate("MainWindow_reach", "Height (m)"),
-            color='green', fontsize=12
+            color='green', fontsize=10
         )
 
         x = self.data.get_station()
-- 
GitLab