Skip to content
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

infinite range for hist(histtype="step") #5221

Closed
anntzer opened this issue Oct 11, 2015 · 11 comments
Closed

infinite range for hist(histtype="step") #5221

anntzer opened this issue Oct 11, 2015 · 11 comments
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
Milestone

Comments

@anntzer
Copy link
Contributor

anntzer commented Oct 11, 2015

In [1]: plt.hist(np.arange(10), range=(0, np.inf))
# empty plot
Out[1]: 
(array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
 array([ nan,  inf,  inf,  inf,  inf,  inf,  inf,  inf,  inf,  inf,  inf]),
 <a list of 10 Patch objects>)

In [2]: plt.hist(np.arange(10), range=(0, np.inf), histtype="step")
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-2-1921805d8f19> in <module>()
----> 1 plt.hist(np.arange(10), range=(0, np.inf), histtype="step")

/usr/lib/python3.5/site-packages/matplotlib/pyplot.py in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, **kwargs)
   2894                       histtype=histtype, align=align, orientation=orientation,
   2895                       rwidth=rwidth, log=log, color=color, label=label,
-> 2896                       stacked=stacked, **kwargs)
   2897         draw_if_interactive()
   2898     finally:

/usr/lib/python3.5/site-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
   5872                         ymin = np.amin(m[m != 0])
   5873                         # filter out the 0 height bins
-> 5874                 ymin = max(ymin*0.9, minimum)
   5875                 ymin = min(ymin0, ymin)
   5876                 self.dataLim.intervaly = (ymin, ymax)

UnboundLocalError: local variable 'ymin' referenced before assignment

Obviously there's isn't much to do here other than improving the error reporting in the second case.

@tacaswell tacaswell added this to the proposed next point release (2.1) milestone Oct 14, 2015
@tacaswell tacaswell added low hanging fruit Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Oct 14, 2015
@lzkelley
Copy link
Contributor

lzkelley commented Nov 2, 2015

Should the first case raise an error as-well? i.e. perhaps hist should assert that the range is finite.

(Aside: there's no error in the second case in python2.x)

@anntzer
Copy link
Contributor Author

anntzer commented Nov 2, 2015

I think it should.

@tacaswell
Copy link
Member

This should go as a bug against numpy. We delegate all of the histogram computation to them, we should not maintain significant validation on top of that.

@anntzer
Copy link
Contributor Author

anntzer commented Nov 2, 2015

Actually this behavior has changed recently, probably with numpy 1.10 & matplotlib 1.5: now neither command raises an error, and both give an empty plot.

@lzkelley
Copy link
Contributor

lzkelley commented Nov 3, 2015

I've brought this up in the numpy-discussion mailing list. Should we close this issue and punt it to them?

@tacaswell
Copy link
Member

Yes, closing this here. Please ping to have re-opened if discussions with numpy go very badly. If you end up creating an issue/PR over at numpy can you please also link to this issue?

@heroxbd
Copy link

heroxbd commented Apr 15, 2016

@lzkelley Could you please provide a reference to the topic at numpy-discussion mailing list?

@lzkelley
Copy link
Contributor

@heroxbd
Copy link

heroxbd commented Apr 15, 2016

I do think it is a bug of matplotlib.
My system is with python-2.7.10, numpy-1.10.4 and matplotlib-1.5.1.

from matplotlib import pylab as plt
plt.hist([1,3], range(1,2)) # success
plt.hist([1,3], range(1,3), histtype="step") # success
plt.hist([1,3], range(1,2), histtype="step") # exception
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-6-73c33d0106be> in <module>()
----> 1 plt.hist([1,3], range(1,2), histtype="step")

/disk/usr100/gentoo/usr/lib64/python2.7/site-packages/matplotlib/pyplot.pyc in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, data, **kwargs)
   2956                       histtype=histtype, align=align, orientation=orientation,
   2957                       rwidth=rwidth, log=log, color=color, label=label,
-> 2958                       stacked=stacked, data=data, **kwargs)
   2959     finally:
   2960         ax.hold(washold)

/disk/usr100/gentoo/usr/lib64/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer, func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/disk/usr100/gentoo/usr/lib64/python2.7/site-packages/matplotlib/axes/_axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
   6207                         ymin = np.amin(m[m != 0])
   6208                         # filter out the 0 height bins
-> 6209                 ymin = max(ymin*0.9, minimum) if not input_empty else minimum
   6210                 ymin = min(ymin0, ymin)
   6211                 self.dataLim.intervaly = (ymin, ymax)

UnboundLocalError: local variable 'ymin' referenced before assignment

@heroxbd
Copy link

heroxbd commented Apr 15, 2016

Thanks @lzkelley. This bug is not caused by infinite numbers, but histogram input failing outside of the specified range.

@lzkelley
Copy link
Contributor

@heroxbd I see - yeah, this looks like a slightly different issue - but hopefully a similar fix. That is really strange: I see that plt.hist([1,3], 1, histtype="step") works fine....

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
Projects
None yet
Development

No branches or pull requests

4 participants