def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if self.dx == 0:
            return

        # Get parameters
        from matplotlib import rcParams  # late import

        def _get_value(attr, default):
            value = getattr(self, attr)
            if value is None:
                value = rcParams.get('scalebar.' + attr, default)
            return value

        length_fraction = _get_value('length_fraction', 0.2)
        height_fraction = _get_value('height_fraction', 0.01)
        location = _get_value('location', 'upper right')
        if is_string_like(location):
            location = self._LOCATIONS[location]
        pad = _get_value('pad', 0.2)
        border_pad = _get_value('border_pad', 0.1)
        sep = _get_value('sep', 5)
        frameon = _get_value('frameon', True)
        color = _get_value('color', 'k')
        box_color = _get_value('box_color', 'w')
        box_alpha = _get_value('box_alpha', 1.0)
        scale_loc = _get_value('scale_loc', 'bottom')
        label_loc = _get_value('label_loc', 'top')
        font_properties = self.font_properties

        if font_properties is None:
            textprops = {'color': color}
        else:
            textprops = {'color': color, 'fontproperties': font_properties}

        ax = self.axes
        xlim, ylim = ax.get_xlim(), ax.get_ylim()
        label = self.label

        # Create label
        if label:
            txtlabel = TextArea(label,
                                minimumdescent=False,
                                textprops=textprops)
        else:
            txtlabel = None

        # Create sizebar
        length_px = abs(xlim[1] - xlim[0]) * length_fraction
        length_px, scale_label = self._calculate_length(length_px)

        size_vertical = abs(ylim[1] - ylim[0]) * height_fraction

        sizebar = AuxTransformBox(ax.transData)
        sizebar.add_artist(
            Rectangle((0, 0),
                      length_px,
                      size_vertical,
                      fill=True,
                      facecolor=color,
                      edgecolor=color))

        txtscale = TextArea(scale_label,
                            minimumdescent=False,
                            textprops=textprops)

        if scale_loc in ['bottom', 'right']:
            children = [sizebar, txtscale]
        else:
            children = [txtscale, sizebar]
        if scale_loc in ['bottom', 'top']:
            Packer = VPacker
        else:
            Packer = HPacker
        boxsizebar = Packer(children=children, align='center', pad=0, sep=sep)

        # Create final offset box
        if txtlabel:
            if label_loc in ['bottom', 'right']:
                children = [boxsizebar, txtlabel]
            else:
                children = [txtlabel, boxsizebar]
            if label_loc in ['bottom', 'top']:
                Packer = VPacker
            else:
                Packer = HPacker
            child = Packer(children=children, align='center', pad=0, sep=sep)
        else:
            child = boxsizebar

        box = AnchoredOffsetbox(loc=location,
                                pad=pad,
                                borderpad=border_pad,
                                child=child,
                                frameon=frameon)

        box.axes = ax
        box.set_figure(self.get_figure())
        box.patch.set_color(box_color)
        box.patch.set_alpha(box_alpha)
        box.draw(renderer)
Example #2
0
    def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if not self.get_mappable():
            return

        # Get parameters
        from matplotlib import rcParams  # late import

        def _get_value(attr, default):
            value = getattr(self, attr)
            if value is None:
                value = rcParams.get('colorbar.' + attr, default)
            return value

        orientation = _get_value('orientation', 'vertical')
        length_fraction = _get_value('length_fraction', 0.2)
        width_fraction = _get_value('width_fraction', 0.01)
        location = _get_value('location', 'upper right')
        if isinstance(location, six.string_types):
            location = self._LOCATIONS[location]
        pad = _get_value('pad', 0.2)
        border_pad = _get_value('border_pad', 0.1)
        sep = _get_value('sep', 5)
        frameon = _get_value('frameon', True)
        color = _get_value('color', 'k')
        box_color = _get_value('box_color', 'w')
        box_alpha = _get_value('box_alpha', 1.0)
        font_properties = self.font_properties
        ticklocation = _get_value('ticklocation', 'auto')
        if ticklocation == 'auto':
            ticklocation = 'bottom' if orientation == 'horizontal' else 'right'

        mappable = self.mappable
        cmap = self.mappable.get_cmap()
        norm = self.mappable.norm
        label = self.label
        ticks = self.ticks
        ticklabels = self.ticklabels

        ax = self.axes

        # Calculate
        calculator = ColorbarCalculator(mappable,
                                        ticks=ticks,
                                        ticklabels=ticklabels)

        X, Y, C = calculator.calculate_colorbar()
        X *= width_fraction
        Y *= length_fraction
        widths = np.diff(X, axis=1)[:, 0]
        heights = np.diff(Y[:, 0])
        if orientation == 'horizontal':
            X, Y = Y, X
            widths, heights = heights, widths

        ticks, ticklabels, offset_string = calculator.calculate_ticks()
        ticks *= length_fraction

        # Create colorbar
        colorbarbox = AuxTransformBox(ax.transAxes)

        patches = []
        for x0, y0, width, height in zip(X[:-1, 0], Y[:-1, 0], widths,
                                         heights):
            patch = Rectangle((x0, y0), width, height)
            patches.append(patch)

        edgecolors = 'none'  #if self.drawedges else 'none'
        #FIXME: drawedge property
        #FIXME: Filled property
        col = PatchCollection(patches,
                              cmap=cmap,
                              edgecolors=edgecolors,
                              norm=norm)
        col.set_array(C[:, 0])
        colorbarbox.add_artist(col)

        # Create outline
        if orientation == 'horizontal':
            outline = Rectangle((0, 0),
                                length_fraction,
                                width_fraction,
                                fill=False,
                                ec=color)
        else:
            outline = Rectangle((0, 0),
                                width_fraction,
                                length_fraction,
                                fill=False,
                                ec=color)
        colorbarbox.add_artist(outline)

        # Create ticks and tick labels
        w10th = width_fraction / 10.0
        ticklines = []
        ticktexts = []
        for tick, ticklabel in zip(ticks, ticklabels):
            if ticklocation == 'bottom':
                x0 = x1 = xtext = tick
                y0 = w10th
                y1 = -w10th
                ytext = -2 * w10th
                ha = 'center'
                va = 'top'
            elif ticklocation == 'top':
                x0 = x1 = xtext = tick
                y0 = width_fraction - w10th
                y1 = width_fraction + w10th
                ytext = width_fraction + 2 * w10th
                ha = 'center'
                va = 'bottom'
            elif ticklocation == 'left':
                x0 = w10th
                x1 = -w10th
                xtext = -2 * w10th
                y0 = y1 = ytext = tick
                ha = 'right'
                va = 'center'
            elif ticklocation == 'right':
                x0 = width_fraction - w10th
                x1 = width_fraction + w10th
                xtext = width_fraction + 2 * w10th
                y0 = y1 = ytext = tick
                ha = 'left'
                va = 'center'

            ticklines.append([(x0, y0), (x1, y1)])

            ticklabel = offset_string + ticklabel
            ticktext = Text(xtext,
                            ytext,
                            ticklabel,
                            color=color,
                            fontproperties=font_properties,
                            horizontalalignment=ha,
                            verticalalignment=va)
            ticktexts.append(ticktext)

        col = LineCollection(ticklines, color=color)
        colorbarbox.add_artist(col)

        for ticktext in ticktexts:
            colorbarbox.add_artist(ticktext)

        # Create label
        if label:
            labelbox = AuxTransformBox(ax.transAxes)

            va = 'baseline' if orientation == 'horizontal' else 'center'
            text = Text(0,
                        0,
                        label,
                        fontproperties=font_properties,
                        verticalalignment=va,
                        rotation=orientation,
                        color=color)
            labelbox.add_artist(text)
        else:
            labelbox = None

        # Create final offset box
        if ticklocation == 'bottom':
            children = [colorbarbox, labelbox] if labelbox else [colorbarbox]
            child = VPacker(children=children, align='center', pad=0, sep=sep)
        elif ticklocation == 'top':
            children = [labelbox, colorbarbox] if labelbox else [colorbarbox]
            child = VPacker(children=children, align='center', pad=0, sep=sep)
        elif ticklocation == 'left':
            children = [labelbox, colorbarbox] if labelbox else [colorbarbox]
            child = HPacker(children=children, align='center', pad=0, sep=sep)
        elif ticklocation == 'right':
            children = [colorbarbox, labelbox] if labelbox else [colorbarbox]
            child = HPacker(children=children, align='center', pad=0, sep=sep)


#
        box = AnchoredOffsetbox(loc=location,
                                pad=pad,
                                borderpad=border_pad,
                                child=child,
                                frameon=frameon)

        box.axes = ax
        box.set_figure(self.get_figure())
        box.patch.set_color(box_color)
        box.patch.set_alpha(box_alpha)
        box.draw(renderer)
Example #3
0
    def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if self.dx == 0:
            return

        # Late import
        from matplotlib import rcParams

        # Deprecation
        if rcParams.get("scalebar.height_fraction") is not None:
            warnings.warn(
                "The scalebar.height_fraction parameter in matplotlibrc is deprecated. "
                "Use scalebar.width_fraction instead.",
                DeprecationWarning,
            )
            rcParams.setdefault("scalebar.width_fraction",
                                rcParams["scalebar.height_fraction"])

        # Get parameters
        def _get_value(attr, default):
            value = getattr(self, attr)
            if value is None:
                value = rcParams.get("scalebar." + attr, default)
            return value

        length_fraction = _get_value("length_fraction", 0.2)
        width_fraction = _get_value("width_fraction", 0.01)
        location = _get_value("location", "upper right")
        if isinstance(location, str):
            location = self._LOCATIONS[location.lower()]
        pad = _get_value("pad", 0.2)
        border_pad = _get_value("border_pad", 0.1)
        sep = _get_value("sep", 5)
        frameon = _get_value("frameon", True)
        color = _get_value("color", "k")
        box_color = _get_value("box_color", "w")
        box_alpha = _get_value("box_alpha", 1.0)
        scale_loc = _get_value("scale_loc", "bottom").lower()
        label_loc = _get_value("label_loc", "top").lower()
        font_properties = self.font_properties
        fixed_value = self.fixed_value
        fixed_units = self.fixed_units or self.units
        rotation = _get_value("rotation", "horizontal").lower()
        label = self.label

        # Create text properties
        textprops = {"color": color, "rotation": rotation}
        if font_properties is not None:
            textprops["fontproperties"] = font_properties

        # Calculate value, units and length
        ax = self.axes
        xlim = ax.get_xlim()
        ylim = ax.get_ylim()
        if rotation == "vertical":
            xlim, ylim = ylim, xlim

        # Mode 1: Auto
        if self.fixed_value is None:
            length_px = abs(xlim[1] - xlim[0]) * length_fraction
            length_px, value, units = self._calculate_best_length(length_px)

        # Mode 2: Fixed
        else:
            value = fixed_value
            units = fixed_units
            length_px = self._calculate_exact_length(value, units)

        scale_text = self.scale_formatter(value,
                                          self.dimension.to_latex(units))

        width_px = abs(ylim[1] - ylim[0]) * width_fraction

        # Create scale bar
        if rotation == "horizontal":
            scale_rect = Rectangle(
                (0, 0),
                length_px,
                width_px,
                fill=True,
                facecolor=color,
                edgecolor="none",
            )
        else:
            scale_rect = Rectangle(
                (0, 0),
                width_px,
                length_px,
                fill=True,
                facecolor=color,
                edgecolor="none",
            )

        scale_bar_box = AuxTransformBox(ax.transData)
        scale_bar_box.add_artist(scale_rect)

        scale_text_box = TextArea(scale_text, textprops=textprops)

        if scale_loc in ["bottom", "right"]:
            children = [scale_bar_box, scale_text_box]
        else:
            children = [scale_text_box, scale_bar_box]

        if scale_loc in ["bottom", "top"]:
            Packer = VPacker
        else:
            Packer = HPacker

        scale_box = Packer(children=children, align="center", pad=0, sep=sep)

        # Create label
        if label:
            label_box = TextArea(label, textprops=textprops)
        else:
            label_box = None

        # Create final offset box
        if label_box:
            if label_loc in ["bottom", "right"]:
                children = [scale_box, label_box]
            else:
                children = [label_box, scale_box]

            if label_loc in ["bottom", "top"]:
                Packer = VPacker
            else:
                Packer = HPacker

            child = Packer(children=children, align="center", pad=0, sep=sep)
        else:
            child = scale_box

        box = AnchoredOffsetbox(loc=location,
                                pad=pad,
                                borderpad=border_pad,
                                child=child,
                                frameon=frameon)

        box.axes = ax
        box.set_figure(self.get_figure())
        box.patch.set_color(box_color)
        box.patch.set_alpha(box_alpha)
        box.draw(renderer)
Example #4
0
    def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if not self.get_mappable():
            return

        # Get parameters
        from matplotlib import rcParams  # late import

        def _get_value(attr, default):
            value = getattr(self, attr)
            if value is None:
                value = rcParams.get('colorbar.' + attr, default)
            return value
        orientation = _get_value('orientation', 'vertical')
        length_fraction = _get_value('length_fraction', 0.2)
        width_fraction = _get_value('width_fraction', 0.01)
        location = _get_value('location', 'upper right')
        if is_string_like(location):
            location = self._LOCATIONS[location]
        pad = _get_value('pad', 0.2)
        border_pad = _get_value('border_pad', 0.1)
        sep = _get_value('sep', 5)
        frameon = _get_value('frameon', True)
        color = _get_value('color', 'k')
        box_color = _get_value('box_color', 'w')
        box_alpha = _get_value('box_alpha', 1.0)
        font_properties = self.font_properties
        ticklocation = _get_value('ticklocation', 'auto')
        if ticklocation == 'auto':
            ticklocation = 'bottom' if orientation == 'horizontal' else 'right'

        mappable = self.mappable
        cmap = self.mappable.cmap
        label = self.label
        ticks = self.ticks
        ticklabels = self.ticklabels

        ax = self.axes

        # Calculate
        calculator = ColorbarCalculator(mappable, ticks=ticks, ticklabels=ticklabels)

        X, Y, C = calculator.calculate_colorbar()
        X *= width_fraction
        Y *= length_fraction
        widths = np.diff(X, axis=1)[:, 0]
        heights = np.diff(Y[:, 0])
        if orientation == 'horizontal':
            X, Y = Y, X
            widths, heights = heights, widths

        ticks, ticklabels, offset_string = calculator.calculate_ticks()
        ticks *= length_fraction

        # Create colorbar
        colorbarbox = AuxTransformBox(ax.transAxes)

        patches = []
        for x0, y0, width, height in zip(X[:-1, 0], Y[:-1, 0], widths, heights):
            patch = Rectangle((x0, y0), width, height)
            patches.append(patch)

        edgecolors = 'none' #if self.drawedges else 'none'
        #FIXME: drawedge property
        #FIXME: Filled property
        col = PatchCollection(patches, cmap=cmap, edgecolors=edgecolors)
        col.set_array(C[:, 0])
        colorbarbox.add_artist(col)

        # Create outline
        if orientation == 'horizontal':
            outline = Rectangle((0, 0), length_fraction, width_fraction,
                                fill=False, ec=color)
        else:
            outline = Rectangle((0, 0), width_fraction, length_fraction,
                                fill=False, ec=color)
        colorbarbox.add_artist(outline)

        # Create ticks and tick labels
        w10th = width_fraction / 10.0
        ticklines = []
        ticktexts = []
        for tick, ticklabel in zip(ticks, ticklabels):
            if ticklocation == 'bottom':
                x0 = x1 = xtext = tick
                y0 = w10th
                y1 = -w10th
                ytext = -2 * w10th
                ha = 'center'
                va = 'top'
            elif ticklocation == 'top':
                x0 = x1 = xtext = tick
                y0 = width_fraction - w10th
                y1 = width_fraction + w10th
                ytext = width_fraction + 2 * w10th
                ha = 'center'
                va = 'bottom'
            elif ticklocation == 'left':
                x0 = w10th
                x1 = -w10th
                xtext = -2 * w10th
                y0 = y1 = ytext = tick
                ha = 'right'
                va = 'center'
            elif ticklocation == 'right':
                x0 = width_fraction - w10th
                x1 = width_fraction + w10th
                xtext = width_fraction + 2 * w10th
                y0 = y1 = ytext = tick
                ha = 'left'
                va = 'center'

            ticklines.append([(x0, y0), (x1, y1)])

            ticklabel = offset_string + ticklabel
            ticktext = Text(xtext, ytext, ticklabel,
                            color=color,
                            fontproperties=font_properties,
                            horizontalalignment=ha,
                            verticalalignment=va)
            ticktexts.append(ticktext)

        col = LineCollection(ticklines, color=color)
        colorbarbox.add_artist(col)

        for ticktext in ticktexts:
            colorbarbox.add_artist(ticktext)

        # Create label
        if label:
            labelbox = AuxTransformBox(ax.transAxes)

            va = 'baseline' if orientation == 'horizontal' else 'center'
            text = Text(0, 0, label,
                        fontproperties=font_properties,
                        verticalalignment=va,
                        rotation=orientation,
                        color=color)
            labelbox.add_artist(text)
        else:
            labelbox = None

        # Create final offset box
        if ticklocation == 'bottom':
            children = [colorbarbox, labelbox] if labelbox else [colorbarbox]
            child = VPacker(children=children, align='center', pad=0, sep=sep)
        elif ticklocation == 'top':
            children = [labelbox, colorbarbox] if labelbox else [colorbarbox]
            child = VPacker(children=children, align='center', pad=0, sep=sep)
        elif ticklocation == 'left':
            children = [labelbox, colorbarbox] if labelbox else [colorbarbox]
            child = HPacker(children=children, align='center', pad=0, sep=sep)
        elif ticklocation == 'right':
            children = [colorbarbox, labelbox] if labelbox else [colorbarbox]
            child = HPacker(children=children, align='center', pad=0, sep=sep)
#
        box = AnchoredOffsetbox(loc=location,
                                pad=pad,
                                borderpad=border_pad,
                                child=child,
                                frameon=frameon)

        box.axes = ax
        box.set_figure(self.get_figure())
        box.patch.set_color(box_color)
        box.patch.set_alpha(box_alpha)
        box.draw(renderer)
Example #5
0
    def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if self.dx == 0:
            return

        # Get parameters
        from matplotlib import rcParams # late import

        def _get_value(attr, default):
            value = getattr(self, attr)
            if value is None:
                value = rcParams.get('scalebar.' + attr, default)
            return value

        length_fraction = _get_value('length_fraction', 0.2)
        height_fraction = _get_value('height_fraction', 0.01)
        location = _get_value('location', 'upper right')
        if isinstance(location,six.string_types):
            location = self._LOCATIONS[location]
        pad = _get_value('pad', 0.2)
        border_pad = _get_value('border_pad', 0.1)
        sep = _get_value('sep', 5)
        frameon = _get_value('frameon', True)
        color = _get_value('color', 'k')
        box_color = _get_value('box_color', 'w')
        box_alpha = _get_value('box_alpha', 1.0)
        scale_loc = _get_value('scale_loc', 'bottom')
        label_loc = _get_value('label_loc', 'top')
        font_properties = self.font_properties

        if font_properties is None:
            textprops = {'color': color}
        else:
            textprops = {'color': color, 'fontproperties': font_properties}

        ax = self.axes
        xlim, ylim = ax.get_xlim(), ax.get_ylim()
        label = self.label

        # Create label
        if label:
            txtlabel = TextArea(label, minimumdescent=False, textprops=textprops)
        else:
            txtlabel = None

        # Create sizebar
        length_px = abs(xlim[1] - xlim[0]) * length_fraction
        length_px, scale_label = self._calculate_length(length_px)

        size_vertical = abs(ylim[1] - ylim[0]) * height_fraction

        sizebar = AuxTransformBox(ax.transData)
        sizebar.add_artist(Rectangle((0, 0), length_px, size_vertical,
                                     fill=True, facecolor=color,
                                     edgecolor=color))

        txtscale = TextArea(scale_label, minimumdescent=False, textprops=textprops)

        if scale_loc in ['bottom', 'right']:
            children = [sizebar, txtscale]
        else:
            children = [txtscale, sizebar]
        if scale_loc in ['bottom', 'top']:
            Packer = VPacker
        else:
            Packer = HPacker
        boxsizebar = Packer(children=children, align='center', pad=0, sep=sep)

        # Create final offset box
        if txtlabel:
            if label_loc in ['bottom', 'right']:
                children = [boxsizebar, txtlabel]
            else:
                children = [txtlabel, boxsizebar]
            if label_loc in ['bottom', 'top']:
                Packer = VPacker
            else:
                Packer = HPacker
            child = Packer(children=children, align='center', pad=0, sep=sep)
        else:
            child = boxsizebar

        box = AnchoredOffsetbox(loc=location,
                                pad=pad,
                                borderpad=border_pad,
                                child=child,
                                frameon=frameon)

        box.axes = ax
        box.set_figure(self.get_figure())
        box.patch.set_color(box_color)
        box.patch.set_alpha(box_alpha)
        box.draw(renderer)
Example #6
0
    def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if self.dx == 0:
            return

        # Get parameters
        from matplotlib import rcParams  # late import

        def _get_value(attr, default):
            value = getattr(self, attr)
            if value is None:
                value = rcParams.get("scalebar." + attr, default)
            return value

        length_fraction = _get_value("length_fraction", 0.2)
        height_fraction = _get_value("height_fraction", 0.01)
        location = _get_value("location", "upper right")
        if isinstance(location, str):
            location = self._LOCATIONS[location]
        pad = _get_value("pad", 0.2)
        border_pad = _get_value("border_pad", 0.1)
        sep = _get_value("sep", 5)
        frameon = _get_value("frameon", True)
        color = _get_value("color", "k")
        box_color = _get_value("box_color", "w")
        box_alpha = _get_value("box_alpha", 1.0)
        scale_loc = _get_value("scale_loc", "bottom")
        label_loc = _get_value("label_loc", "top")
        font_properties = self.font_properties
        fixed_value = self.fixed_value
        fixed_units = self.fixed_units or self.units

        if font_properties is None:
            textprops = {"color": color}
        else:
            textprops = {"color": color, "fontproperties": font_properties}

        ax = self.axes
        xlim = ax.get_xlim()
        ylim = ax.get_ylim()
        label = self.label

        # Calculate value, units and length
        # Mode 1: Auto
        if self.fixed_value is None:
            length_px = abs(xlim[1] - xlim[0]) * length_fraction
            length_px, value, units = self._calculate_best_length(length_px)

        # Mode 2: Fixed
        else:
            value = fixed_value
            units = fixed_units
            length_px = self._calculate_exact_length(value, units)

        scale_label = self.label_formatter(value,
                                           self.dimension.to_latex(units))

        size_vertical = abs(ylim[1] - ylim[0]) * height_fraction

        # Create size bar
        sizebar = AuxTransformBox(ax.transData)
        sizebar.add_artist(
            Rectangle(
                (0, 0),
                length_px,
                size_vertical,
                fill=True,
                facecolor=color,
                edgecolor="none",
            ))

        txtscale = TextArea(scale_label,
                            minimumdescent=False,
                            textprops=textprops)

        if scale_loc in ["bottom", "right"]:
            children = [sizebar, txtscale]
        else:
            children = [txtscale, sizebar]
        if scale_loc in ["bottom", "top"]:
            Packer = VPacker
        else:
            Packer = HPacker
        boxsizebar = Packer(children=children, align="center", pad=0, sep=sep)

        # Create text area
        if label:
            txtlabel = TextArea(label,
                                minimumdescent=False,
                                textprops=textprops)
        else:
            txtlabel = None

        # Create final offset box
        if txtlabel:
            if label_loc in ["bottom", "right"]:
                children = [boxsizebar, txtlabel]
            else:
                children = [txtlabel, boxsizebar]
            if label_loc in ["bottom", "top"]:
                Packer = VPacker
            else:
                Packer = HPacker
            child = Packer(children=children, align="center", pad=0, sep=sep)
        else:
            child = boxsizebar

        box = AnchoredOffsetbox(loc=location,
                                pad=pad,
                                borderpad=border_pad,
                                child=child,
                                frameon=frameon)

        box.axes = ax
        box.set_figure(self.get_figure())
        box.patch.set_color(box_color)
        box.patch.set_alpha(box_alpha)
        box.draw(renderer)
Example #7
0
    def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if not self.get_mappable():
            return

        # Get parameters
        from matplotlib import rcParams  # late import

        cmap = self.mappable.get_cmap()
        array = self.mappable.get_array()
        label = self.label
        orientation = self.orientation or \
            rcParams.get('colorbar.orientation', 'vertical')
        nbins = self.nbins or rcParams.get('colorbar.nbins', 50)
        length_fraction = self.length_fraction or \
            rcParams.get('colorbar.length_fraction', 0.2)
        width_fraction = self.width_fraction or \
            rcParams.get('colorbar.width_fraction', 0.01)
        location = self.location or \
            self._LOCATIONS[rcParams.get('colorbar.location', 'upper right')]
        pad = self.pad or rcParams.get('colorbar.pad', 0.2)
        border_pad = self.border_pad or \
            rcParams.get('colorbar.border_pad', 0.1)
        sep = self.sep or rcParams.get('colorbar.sep', 5)
        frameon = self.frameon or rcParams.get('colorbar.frameon', True)
        color = self.color or rcParams.get('colorbar.color', 'k')
        box_color = self.box_color or rcParams.get('colorbar.box_color', 'w')
        box_alpha = self.box_alpha or rcParams.get('colorbar.box_alpha', 1.0)
        font_properties = self.font_properties
        ticks = self.ticks
        ticklabels = self.ticklabels

        ax = self.axes
        children = []

        # Create colorbar
        colorbarbox = AuxTransformBox(ax.transData)

        xlim, ylim = ax.get_xlim(), ax.get_ylim()
        if orientation == 'horizontal':
            length = abs(xlim[1] - xlim[0]) * length_fraction
            width = abs(ylim[1] - ylim[0]) * width_fraction
        else:
            length = abs(ylim[1] - ylim[0]) * length_fraction
            width = abs(xlim[1] - xlim[0]) * width_fraction
        step_length = length / nbins

        patches = []
        for x in np.arange(0, length, step_length):
            if orientation == 'horizontal':
                patch = Rectangle((x, 0), step_length, width)
            else:
                patch = Rectangle((0, x), width, step_length)
            patches.append(patch)

        values = np.linspace(np.min(array), np.max(array), nbins)
        minvalue, maxvalue = values[0], values[-1]

        col = PatchCollection(patches, cmap=cmap,
                              edgecolors='none')
        col.set_array(values)
        colorbarbox.add_artist(col)

        if orientation == 'horizontal':
            patch = Rectangle((0, 0), length, width, fill=False, ec=color)
        else:
            patch = Rectangle((0, 0), width, length, fill=False, ec=color)
        colorbarbox.add_artist(patch)

        children.append(colorbarbox)

        # Create ticks
        tickbox = AuxTransformBox(ax.transData)

        if ticks is None:
            ticks = [minvalue, maxvalue]  # default

        if not ticklabels:
            ticklabels = ticks[:]  # tick label by default

        if minvalue not in ticks:  # little hack to get right layout position
            ticks.append(minvalue)
            ticklabels.append('')  # no label for this extra tick

        if maxvalue not in ticks:  # little hack to get right layout position
            ticks.append(maxvalue)
            ticklabels.append('')  # no label for this extra tick

        for itick, tick in enumerate(ticks):

            if tick > maxvalue or tick < minvalue:
                continue  # ignore it

            # Fraction of colorbar depending of min and max values of colorbar
            a = 1 / (maxvalue - minvalue)
            b = -a * minvalue
            tickfrac = a * tick + b

            if orientation == 'horizontal':
                tickx = tickfrac * length
                ticky = 0
                ha = 'center'
                va = 'top'
            else:
                tickx = width
                ticky = tickfrac * length
                ha = 'left'
                va = 'center'

            ticktext = Text(tickx, ticky, ticklabels[itick],
                            color=color,
                            fontproperties=font_properties,
                            horizontalalignment=ha,
                            verticalalignment=va)
            tickbox.add_artist(ticktext)

        children.append(tickbox)

        # Create label
        if label:
            labelbox = AuxTransformBox(ax.transData)

            va = 'baseline' if orientation == 'horizontal' else 'center'
            text = Text(0, 0, label,
                        fontproperties=font_properties,
                        verticalalignment=va,
                        rotation=orientation)
            labelbox.add_artist(text)

            children.insert(0, labelbox)

        # Create final offset box
        Packer = VPacker if orientation == 'horizontal' else HPacker
        child = Packer(children=children, align="center", pad=0, sep=sep)

        box = AnchoredOffsetbox(loc=location,
                                pad=pad,
                                borderpad=border_pad,
                                child=child,
                                frameon=frameon)

        box.axes = ax
        box.set_figure(self.get_figure())
        box.patch.set_color(box_color)
        box.patch.set_alpha(box_alpha)
        box.draw(renderer)