Skip to content

BoundaryNorm yield a ZeroDivisionError: division by zero #17579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Jun 5, 2020 · 3 comments · Fixed by #17830
Closed

BoundaryNorm yield a ZeroDivisionError: division by zero #17579

ghost opened this issue Jun 5, 2020 · 3 comments · Fixed by #17830
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues status: confirmed bug
Milestone

Comments

@ghost
Copy link

ghost commented Jun 5, 2020

Bug report

Bug summary

import matplotlib as mpl
import matplotlib.pyplot as plt
cmap = plt.cm.jet
bounds = [0, 1]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
norm(0.5)

Gives:

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-160-8a8135eaeb3c> in <module>
      2 bounds = [0, 1]
      3 norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
----> 4 norm(0.5)

/usr/local/lib/python3.6/dist-packages/matplotlib/colors.py in __call__(self, value, clip)
   1442             iret[xx >= b] = i
   1443         if self._interp:
-> 1444             scalefac = (self.Ncmap - 1) / (self.N - 2)
   1445             iret = (iret * scalefac).astype(np.int16)
   1446         iret[xx < self.vmin] = -1

ZeroDivisionError: division by zero

Expected outcome

not an error: just an integer.

Matplotlib version

mpl.__version__: 3.2.1
os.name: posix
mpl.get_backend(): module://ipykernel.pylab.backend_inline
sys.version: 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0]
$ jupyter --version
jupyter core     : 4.6.3
jupyter-notebook : 6.0.3
qtconsole        : 4.7.1
ipython          : 7.13.0
ipykernel        : 5.1.1
jupyter client   : 6.0.0
jupyter lab      : 1.2.14
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 4.4.0
traitlets        : 4.3.3
@tacaswell tacaswell modified the milestones: v3.3.0, v3.4.0 Jun 5, 2020
@tacaswell tacaswell added the Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues label Jun 5, 2020
@tacaswell tacaswell modified the milestones: v3.4.0, v3.3.0 Jun 5, 2020
@tacaswell
Copy link
Member

This code path was changed a bin on master branchhttps://github.com//pull/17534 but does not fix this issue.

We need to add logic to

if self.Ncmap > self._N:
scalefac = (self.Ncmap - 1) / (self._N - 1)
iret = (iret * scalefac).astype(np.int16)
to handle the case were we only have 1 bin.

Sorry for the milestone thrashing.

@Raghibshams456
Copy link

is anybody is doing or done something on this issue .?...

@tacaswell
Copy link
Member

Yes, this should be fixed by #17830 (which is still under review) and if it is merged will be available in mpl 3.4 (which we are aiming for a Sept release).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues status: confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants