# import matplotlib.pyplot as _plt

import os as _os
on_rtd = _os.environ.get('READTHEDOCS', None) == 'True'
if not on_rtd:
    import matplotlib.widgets as _wdg
    import numpy as _np

from .setup_figure import setup_figure   # noqa


class Imshow_Slider(object):
    """
    Convenience class for viewing images.
    
    Plots *image* to a to an instance of :class:`matplotlib.axis.imshow(**kwargs)`, with sliders for controlling bounds.
    """
    def __init__(self, image, **kwargs):
        # ======================================
        # Save input info
        # ======================================
        self._image  = image
        self._kwargs = kwargs

        # ======================================
        # Create figure
        # ======================================
        self.fig, self.gs = setup_figure(20, 10)
        self.ax_img = self.fig.add_subplot(self.gs[0:-3, :])
        self.ax_min = self.fig.add_subplot(self.gs[-2, 1:-1])
        self.ax_max = self.fig.add_subplot(self.gs[-1, 1:-1])

        self._reset(**kwargs)

    def _reset(self, **kwargs):
        # ======================================
        # Imshow
        # ======================================
        self.p = self.ax_img.imshow(self._image, **kwargs)

        # ======================================
        # Add minimum slider
        # ======================================
        self.minslider = _wdg.Slider(self.ax_min, 'Min', self.imgmin, self.imgmax, self.imgmin)

        # ======================================
        # Add maximum slider
        # ======================================
        self.maxslider = _wdg.Slider(self.ax_max, 'Max', self.imgmin, self.imgmax, self.imgmax, slidermin=self.minslider)
        self.minslider.slidermax = self.maxslider

        self.minslider.on_changed(self._update_clim)
        self.maxslider.on_changed(self._update_clim)

        self.fig.tight_layout()

    def set_cmap(self, cmap):
        """
        Sets color map to *cmap*.
        """
        self.p.set_cmap(cmap)

    # ======================================
    # Get min of image
    # ======================================
    @property
    def imgmax(self):
        """
        Maximum of color bar
        """
        return _np.max(self._image)

    # ======================================
    # Get max of image
    # ======================================
    @property
    def imgmin(self):
        """
        Minimum of color bar
        """
        return _np.min(self._image)

    # ======================================
    # Update the clims
    # ======================================
    def _update_clim(self, val):
        cmin = self.minslider.val
        cmax = self.maxslider.val
        # print('Cmin: {}, Cmax: {}'.format(cmin, cmax))
        self.p.set_clim(cmin, cmax)

    # ======================================
    # Easily get and set slider
    # ======================================
    @property
    def clim_min(self):
        """
        Slider value for minimum
        """
        return self.minslider.val

    @clim_min.setter
    def clim_min(self, val):
        self.minslider.set_val(val)

    @property
    def clim_max(self):
        """
        Slider value for maximum
        """
        return self.maxslider.val

    @clim_max.setter
    def clim_max(self, val):
        self.maxslider.set_val(val)

@@ -848,24 +848,25 @@
        self.
+_
 ax_img = sel
@@ -1226,16 +1226,17 @@
  = self.
+_
 ax_img.i
@@ -2048,16 +2048,154 @@
 (cmap)%0A%0A
+    @property%0A    def ax(self):%0A        %22%22%22%0A        The axis for :func:%60matplotlib.axis.imshow%60.%0A        %22%22%22%0A        return self._ax_img%0A%0A
     # ==

Allow access to axis in Imshow_Slider