Repository: spyder-ide/spyder

Issue:
ZeroDivisionError when trying to show a plot
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

I really don't know what led to this error. I've launched my script 1 or 2 times already with the Debug button on the toolbar. I just re-launched it, again using Debug, after setting a new breakpoint.  This time, after running some minutes, this internal error popped up.  I don't know why it hadn't popped up on a previous run.

### Traceback
```python-traceback
  File "C:\Users\greg\AppData\Local\Continuum\anaconda3-64\envs\nwra3\lib\site-packages\spyder\plugins\plots\widgets\figurebrowser.py", line 540, in eventFilter
    self.scale_image()
  File "C:\Users\greg\AppData\Local\Continuum\anaconda3-64\envs\nwra3\lib\site-packages\spyder\plugins\plots\widgets\figurebrowser.py", line 609, in scale_image
    if (fwidth / fheight) > (width / height):
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\greg\AppData\Local\Continuum\anaconda3-64\envs\nwra3\lib\site-packages\spyder\plugins\plots\widgets\figurebrowser.py", line 540, in eventFilter
    self.scale_image()
  File "C:\Users\greg\AppData\Local\Continuum\anaconda3-64\envs\nwra3\lib\site-packages\spyder\plugins\plots\widgets\figurebrowser.py", line 609, in scale_image
    if (fwidth / fheight) > (width / height):
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\greg\AppData\Local\Continuum\anaconda3-64\envs\nwra3\lib\site-packages\spyder\plugins\plots\widgets\figurebrowser.py", line 540, in eventFilter
    self.scale_image()
  File "C:\Users\greg\AppData\Local\Continuum\anaconda3-64\envs\nwra3\lib\site-packages\spyder\plugins\plots\widgets\figurebrowser.py", line 609, in scale_image
    if (fwidth / fheight) > (width / height):
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "C:\Users\greg\AppData\Local\Continuum\anaconda3-64\envs\nwra3\lib\site-packages\spyder\plugins\plots\widgets\figurebrowser.py", line 540, in eventFilter
    self.scale_image()
  File "C:\Users\greg\AppData\Local\Continuum\anaconda3-64\envs\nwra3\lib\site-packages\spyder\plugins\plots\widgets\figurebrowser.py", line 609, in scale_image
    if (fwidth / fheight) > (width / height):
ZeroDivisionError: division by zero
```

## Versions

* Spyder version: 4.1.2 
* Python version: 3.7.6
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Windows 10

### Dependencies

```

# Mandatory:
atomicwrites >=1.2.0           :  1.3.0 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  1.3.0 (OK)
diff_match_patch >=20181111    :  20181111 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.13.0 (OK)
jedi =0.15.2                   :  0.15.2 (OK)
nbconvert >=4.0                :  5.6.1 (OK)
numpydoc >=0.6.0               :  0.9.2 (OK)
paramiko >=2.4.0               :  2.7.1 (OK)
parso =0.5.2                   :  0.5.2 (OK)
pexpect >=4.4.0                :  4.8.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.7.0 (OK)
pygments >=2.0                 :  2.6.1 (OK)
pylint >=0.25                  :  2.4.4 (OK)
pyls >=0.31.9;<0.32.0          :  0.31.9 (OK)
qdarkstyle >=2.8               :  2.8 (OK)
qtawesome >=0.5.7              :  0.7.0 (OK)
qtconsole >=4.6.0              :  4.7.2 (OK)
qtpy >=1.5.0                   :  1.9.0 (OK)
rtree >=0.8.3                  :  0.9.3 (OK)
sphinx >=0.6.6                 :  2.4.4 (OK)
spyder_kernels >=1.9.0;<1.10.0 :  1.9.0 (OK)
watchdog                       :  None (OK)
zmq >=17                       :  18.1.1 (OK)

# Optional:
cython >=0.21                  :  0.29.15 (OK)
matplotlib >=2.0.0             :  3.1.3 (OK)
numpy >=1.7                    :  1.18.1 (OK)
pandas >=0.13.1                :  1.0.3 (OK)
scipy >=0.17.0                 :  1.4.1 (OK)
sympy >=0.7.3                  :  1.5.1 (OK)
```



Discussion:
I have seen that, it happens when a corrupted image data is sent to the frontend.
What can we do about it @impact27?
We should at least be catching that exception in the meantime.
Sure, but what to do when that happens?
The image won't be updated/created 🤷‍♂️ 
Ok. If you want to do a PR to solve this, please do.
> The image won't be updated/created 🤷‍♂️

We could also display a "Broken" image logo
It could be, yeah. @goanpeca, what do you think?
@steff456, let's catch the error above and show in Plots this icon: `mdi-image-broken-variant`.

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
@@ -28,6 +28,8 @@
                             QStyle)
 
 # ---- Local library imports
+import spyder.utils.icon_manager as ima
+
 from spyder.config.base import _
 from spyder.config.manager import CONF
 from spyder.py3compat import is_unicode, to_text_string
@@ -606,12 +608,21 @@ def scale_image(self):
             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)
-            else:
-                new_height = int(height)
-                new_width = int(height / fheight * fwidth)
+            self.figcanvas.setToolTip('')
+            try:
+                if (fwidth / fheight) > (width / height):
+                    new_width = int(width)
+                    new_height = int(width / fwidth * fheight)
+                else:
+                    new_height = int(height)
+                    new_width = int(height / fheight * fwidth)
+            except ZeroDivisionError:
+                icon = ima.icon('broken_image')
+                self.figcanvas._qpix_orig = icon.pixmap(fwidth, fheight)
+                self.figcanvas.setToolTip(
+                    _('The image is broken, please try to generate it again'))
+                new_width = fwidth
+                new_height = fheight
 
         if self.figcanvas.size() != QSize(new_width, new_height):
             self.figcanvas.setFixedSize(new_width, new_height)
diff --git a/spyder/utils/icon_manager.py b/spyder/utils/icon_manager.py
--- a/spyder/utils/icon_manager.py
+++ b/spyder/utils/icon_manager.py
@@ -336,6 +336,8 @@
     'dependency_ok':           [('fa.check',), {'color': MAIN_FG_COLOR}],
     'dependency_warning':      [('fa.warning',), {'color': 'orange'}],
     'dependency_error':        [('fa.warning',), {'color': 'darkred'}],
+    'broken_image':          [('mdi.image-broken-variant',),
+                                {'color': MAIN_FG_COLOR}],
     # --- Status bar --------------------------------------------------------
     'code_fork':               [('fa.code-fork',), {'color': MAIN_FG_COLOR}],
 }


Test patch: