Repository: rasterio/rasterio

Issue:
show_hist() fails with nans
I am getting an `UnboundLocalError: local variable 'ymin' referenced before assignment` when I am trying to use the show_hist() function in the plotting module.  

```
import rasterio
from rasterio.plot import show_hist
from rasterio.plot import show
import matplotlib.pyplot as plt

fname = r"C:\workspace\GLCM\output\glcm_rasters\2014_05\10\R02028_10_asm.tif"
src = rasterio.open(fname)

fig, (axrgb, axhist) = plt.subplots(1, 2, figsize=(14,7))
show(src, ax=axrgb)
show_hist(src, bins=50, histtype='bar', lw=0.0, stacked=False, alpha=0.3, ax=axhist)
 plt.show()
```

[here](https://drive.google.com/file/d/0B17WT3pXaqrYcUVQbXRVYkxqeXc/view?usp=sharing) is the offending file.

conda info:

```
             platform : win-64
        conda version : 4.1.11
    conda-env version : 2.5.2
  conda-build version : 1.20.0
       python version : 2.7.11.final.0
     requests version : 2.9.1
     root environment : c:\Anaconda2  (writable)
  default environment : c:\Anaconda2
     envs directories : c:\Anaconda2\envs
        package cache : c:\Anaconda2\pkgs
         channel URLs : https://repo.continuum.io/pkgs/free/win-64/
                        https://repo.continuum.io/pkgs/free/noarch/
                        https://repo.continuum.io/pkgs/pro/win-64/
                        https://repo.continuum.io/pkgs/pro/noarch/
                        https://repo.continuum.io/pkgs/msys2/win-64/
                        https://repo.continuum.io/pkgs/msys2/noarch/
          config file : None
         offline mode : False
    is foreign system : False
```

Package info:

```
rasterio                  0.36.0                    <pip>
matplotlib                1.5.1               np110py27_0
```



Discussion:
I get a different (though likely related) error on the master branch with python 3.5

```
$ rio insp ~/Downloads/R02028_10_asm.tif
Rasterio 1.0a2 Interactive Inspector (Python 3.5.2)
Type "src.meta", "src.read(1)", or "help(src)" for more information.
>>> import matplotlib.pyplot as plt
>>> fig, (axrgb, axhist) = plt.subplots(1, 2, figsize=(14,7))
>>> show(src, ax=axrgb)
<matplotlib.axes._subplots.AxesSubplot object at 0x111164860>
>>> show_hist(src, bins=50, histtype='bar', lw=0.0, stacked=False, alpha=0.3, ax=axhist)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/mperry/work/rasterio/rasterio/plot.py", line 264, in show_hist
    **kwargs)
  File "/Users/mperry/env/mapbox35/lib/python3.5/site-packages/matplotlib/__init__.py", line 1819, in inner
    return func(ax, *args, **kwargs)
  File "/Users/mperry/env/mapbox35/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 5985, in hist
    m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
  File "/Users/mperry/env/mapbox35/lib/python3.5/site-packages/numpy/lib/function_base.py", line 505, in histogram
    'range parameter must be finite.')
ValueError: range parameter must be finite.
```

This is most likely related to [how we calculate min/max](https://github.com/mapbox/rasterio/blob/master/rasterio/plot.py#L224-L226). The dataset has `NaN`s which blow up the numpy summary stat functions

```
$ rio info --indent 2 -v ~/Downloads/R02028_10_asm.tif
...
  "stats": [
    {
      "mean": NaN,
      "min": NaN,
      "max": NaN
    }
  ],
```

This is a bug in `show_hist` but also arguably in `rio-info`. In both cases we might consider using the `nan` variants - [`numpy.nanmin()`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.nanmin.html) instead of `numpy.min`, etc.

Changing the title to reflect the nan issue.

@danhamill can you send the full traceback error so we can track down where the `ymin` error is coming from?


Resolving patch:
diff --git a/rasterio/plot.py b/rasterio/plot.py
--- a/rasterio/plot.py
+++ b/rasterio/plot.py
@@ -223,7 +223,7 @@ def show_hist(source, bins=10, masked=True, title='Histogram', ax=None, **kwargs
 
     # The histogram is computed individually for each 'band' in the array
     # so we need the overall min/max to constrain the plot
-    rng = arr.min(), arr.max()
+    rng = np.nanmin(arr), np.nanmax(arr)
 
     if len(arr.shape) is 2:
         arr = np.expand_dims(arr.flatten(), 0).T


Test patch: