Repository: spyder-ide/spyder

Issue:
Resizing plot panes does not resize the content
I suggest that when panes are resized the content adapts to the new size. This is not currently the case for the Plots pane

![problem3](https://user-images.githubusercontent.com/3627835/57983466-d05b6d80-7a17-11e9-9a1e-745da82f8667.gif)



Discussion:
This could be possible if we switch from Png to Svg for the default Inline backend format.
I'll take care of this, thanks for filling an issue about this.
Also making svg defaults is not a good idea IMO, but I'm not sure this is related to the current issue.

Resolving patch:
diff --git a/spyder/plugins/plots/widgets/figurebrowser.py b/spyder/plugins/plots/widgets/figurebrowser.py
--- a/spyder/plugins/plots/widgets/figurebrowser.py
+++ b/spyder/plugins/plots/widgets/figurebrowser.py
@@ -397,12 +397,31 @@ def __init__(self, parent=None, background_color=None):
         self._sfmax = 10
         self._sfmin = -10
 
+        self.setup_figcanvas()
         self.auto_fit_plotting = False
 
         # An internal flag that tracks when the figure is being panned.
         self._ispanning = False
 
-        self.setup_figcanvas()
+    @property
+    def auto_fit_plotting(self):
+        """
+        Return whether to automatically fit the plot to the scroll area size.
+        """
+        return self._auto_fit_plotting
+
+    @auto_fit_plotting.setter
+    def auto_fit_plotting(self, value):
+        """
+        Set whether to automatically fit the plot to the scroll area size.
+        """
+        self._auto_fit_plotting = value
+        if value:
+            self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
+            self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
+        else:
+            self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
+            self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
 
     def setup_figcanvas(self):
         """Setup the FigureCanvas."""
@@ -431,6 +450,10 @@ def eventFilter(self, widget, event):
             else:
                 return False
 
+        # ---- Scaling
+        elif event.type() == QEvent.Paint and self.auto_fit_plotting:
+            self.scale_image()
+
         # ---- Panning
         # Set ClosedHandCursor:
         elif event.type() == QEvent.MouseButtonPress:
@@ -493,10 +516,13 @@ def scale_image(self):
         # Scale the image to fit figviewer size while respect the ratio
         else:
             size = self.size()
-            scrollbar_width = self.verticalScrollBar().sizeHint().width()
-            width = size.width() - scrollbar_width
-            scrollbar_height = self.horizontalScrollBar().sizeHint().height()
-            height = size.height() - scrollbar_height
+            style = self.style()
+            width = (size.width() -
+                     style.pixelMetric(QStyle.PM_LayoutLeftMargin) -
+                     style.pixelMetric(QStyle.PM_LayoutRightMargin))
+            height = (size.height() -
+                      style.pixelMetric(QStyle.PM_LayoutTopMargin) -
+                      style.pixelMetric(QStyle.PM_LayoutBottomMargin))
             if (fwidth / fheight) > (width / height):
                 new_width = int(width)
                 new_height = int(width / fwidth * fheight)
@@ -931,7 +957,7 @@ def clear_canvas(self):
         """Clear the figure that was painted on the widget."""
         self.fig = None
         self.fmt = None
-        self._qpix_buffer = []
+        self._qpix_scaled = None
         self.repaint()
 
     def load_figure(self, fig, fmt):
@@ -948,14 +974,14 @@ def load_figure(self, fig, fmt):
         elif fmt == 'image/svg+xml':
             self._qpix_orig = QPixmap(svg_to_image(fig))
 
-        self._qpix_buffer = [self._qpix_orig]
+        self._qpix_scaled = self._qpix_orig
         self.fwidth = self._qpix_orig.width()
         self.fheight = self._qpix_orig.height()
 
     def paintEvent(self, event):
         """Qt method override to paint a custom image on the Widget."""
         super(FigureCanvas, self).paintEvent(event)
-        # Prepare the rect on which the image is going to be painted :
+        # Prepare the rect on which the image is going to be painted.
         fw = self.frameWidth()
         rect = QRect(0 + fw, 0 + fw,
                      self.size().width() - 2 * fw,
@@ -964,23 +990,19 @@ def paintEvent(self, event):
         if self.fig is None or self._blink_flag:
             return
 
-        # Check/update the qpixmap buffer :
-        qpix2paint = None
-        for qpix in self._qpix_buffer:
-            if qpix.size().width() == rect.width():
-                qpix2paint = qpix
-                break
-        else:
+        # Prepare the scaled qpixmap to paint on the widget.
+        if (self._qpix_scaled is None or
+                self._qpix_scaled.size().width() != rect.width()):
             if self.fmt in ['image/png', 'image/jpeg']:
-                qpix2paint = self._qpix_orig.scaledToWidth(
+                self._qpix_scaled = self._qpix_orig.scaledToWidth(
                     rect.width(), mode=Qt.SmoothTransformation)
             elif self.fmt == 'image/svg+xml':
-                qpix2paint = QPixmap(svg_to_image(self.fig, rect.size()))
-            self._qpix_buffer.append(qpix2paint)
+                self._qpix_scaled = QPixmap(svg_to_image(
+                    self.fig, rect.size()))
 
-        if qpix2paint is not None:
-            # Paint the image on the widget :
+        if self._qpix_scaled is not None:
+            # Paint the image on the widget.
             qp = QPainter()
             qp.begin(self)
-            qp.drawPixmap(rect, qpix2paint)
+            qp.drawPixmap(rect, self._qpix_scaled)
             qp.end()


Test patch:
diff --git a/spyder/plugins/plots/widgets/tests/test_plots_widgets.py b/spyder/plugins/plots/widgets/tests/test_plots_widgets.py
--- a/spyder/plugins/plots/widgets/tests/test_plots_widgets.py
+++ b/spyder/plugins/plots/widgets/tests/test_plots_widgets.py
@@ -23,7 +23,7 @@
 from matplotlib.figure import Figure
 from matplotlib.backends.backend_agg import FigureCanvasAgg
 import numpy as np
-from qtpy.QtWidgets import QApplication
+from qtpy.QtWidgets import QApplication, QStyle
 from qtpy.QtGui import QPixmap
 from qtpy.QtCore import Qt
 
@@ -402,12 +402,15 @@ def test_autofit_figure_viewer(figbrowser, tmpdir, fmt):
     # Test when `Fit plots to window` is set to True.
     # Otherwise, test should fall into `test_zoom_figure_viewer`
     figbrowser.change_auto_fit_plotting(True)
-    size = figviewer.size()
 
-    scrollbar_width = figviewer.verticalScrollBar().sizeHint().width()
-    width = size.width() - scrollbar_width
-    scrollbar_height = figviewer.horizontalScrollBar().sizeHint().height()
-    height = size.height() - scrollbar_height
+    size = figviewer.size()
+    style = figviewer.style()
+    width = (size.width() -
+             style.pixelMetric(QStyle.PM_LayoutLeftMargin) -
+             style.pixelMetric(QStyle.PM_LayoutRightMargin))
+    height = (size.height() -
+              style.pixelMetric(QStyle.PM_LayoutTopMargin) -
+              style.pixelMetric(QStyle.PM_LayoutBottomMargin))
     if (fwidth / fheight) > (width / height):
         new_width = int(width)
         new_height = int(width / fwidth * fheight)