Repository: silx-kit/silx
Issue: [plot] KeepAspectRatio=True => Images/Plots not centered

Images and plot are not centered when keep aspect ration is set.

``` python
import numpy as np
from silx.gui import qt as Qt
from silx.gui import plot

data = np.ones((100, 200))
data[0::5, 0::5] = 0

app = Qt.QApplication([])
plotWin = plot.PlotWindow()
plotWin.addImage(data, origin=(10, 30))
# plotWin.setActiveCurveHandling(False)
plotWin.setKeepDataAspectRatio(True)
plotWin.show()
app.exec_()
```


Changed files:
silx/gui/plot/BackendMatplotlib.py, silx/gui/plot/test/testPlotWidget.py

diff --git a/silx/gui/plot/BackendMatplotlib.py b/silx/gui/plot/BackendMatplotlib.py
index 714da1c28..94b01a111 100644
--- a/silx/gui/plot/BackendMatplotlib.py
+++ b/silx/gui/plot/BackendMatplotlib.py
@@ -654,14 +654,14 @@ class BackendMatplotlib(BackendBase.BackendBase):
                 dataRatio = (ymax - ymin) / (xmax - xmin)
                 if dataRatio < figureRatio:
                     # Increase y range
-                    ycenter = 0.5 * (newLimits[3] - newLimits[2])
+                    ycenter = 0.5 * (newLimits[3] + newLimits[2])
                     yrange = (xmax - xmin) * figureRatio
                     newLimits[2] = ycenter - 0.5 * yrange
                     newLimits[3] = ycenter + 0.5 * yrange
 
                 elif dataRatio > figureRatio:
                     # Increase x range
-                    xcenter = 0.5 * (newLimits[1] - newLimits[0])
+                    xcenter = 0.5 * (newLimits[1] + newLimits[0])
                     xrange_ = (ymax - ymin) / figureRatio
                     newLimits[0] = xcenter - 0.5 * xrange_
                     newLimits[1] = xcenter + 0.5 * xrange_
diff --git a/silx/gui/plot/test/testPlotWidget.py b/silx/gui/plot/test/testPlotWidget.py
index 9cad028b5..fea51f87d 100644
--- a/silx/gui/plot/test/testPlotWidget.py
+++ b/silx/gui/plot/test/testPlotWidget.py
@@ -67,7 +67,7 @@ class _PlotWidgetTest(TestCaseQt):
         super(_PlotWidgetTest, self).tearDown()
 
 
-class TestPlotWidget(_PlotWidgetTest):
+class TestPlotWidget(_PlotWidgetTest, ParametricTestCase):
     """Basic tests for PlotWidget"""
 
     def testShow(self):
@@ -214,8 +214,6 @@ class TestPlotImage(_PlotWidgetTest, ParametricTestCase):
         for origin, scale in tests:
             with self.subTest(origin=origin, scale=scale):
                 self.plot.addImage(DATA_2D, origin=origin, scale=scale)
-                xmin, xmax = self.plot.getGraphXLimits()
-                ymin, ymax = self.plot.getGraphYLimits()
 
                 try:
                     ox, oy = origin
@@ -225,15 +223,27 @@ class TestPlotImage(_PlotWidgetTest, ParametricTestCase):
                     sx, sy = scale
                 except TypeError:
                     sx, sy = scale, scale
-
                 xbounds = ox, ox + DATA_2D.shape[1] * sx
+                ybounds = oy, oy + DATA_2D.shape[0] * sy
+
+                # Check limits without aspect ratio
+                xmin, xmax = self.plot.getGraphXLimits()
+                ymin, ymax = self.plot.getGraphYLimits()
                 self.assertEqual(xmin, min(xbounds))
                 self.assertEqual(xmax, max(xbounds))
-
-                ybounds = oy, oy + DATA_2D.shape[0] * sy
                 self.assertEqual(ymin, min(ybounds))
                 self.assertEqual(ymax, max(ybounds))
 
+                # Check limits with aspect ratio
+                self.plot.setKeepDataAspectRatio(True)
+                xmin, xmax = self.plot.getGraphXLimits()
+                ymin, ymax = self.plot.getGraphYLimits()
+                self.assertTrue(xmin <= min(xbounds))
+                self.assertTrue(xmax >= max(xbounds))
+                self.assertTrue(ymin <= min(ybounds))
+                self.assertTrue(ymax >= max(ybounds))
+
+                self.plot.setKeepDataAspectRatio(False)  # Reset aspect ratio
                 self.plot.clear()
                 self.plot.resetZoom()