<commit_before>"""
Artist for matplotlib to display a colorbar as an artist,
instead of an axis as it is the default in matplotlib.

Example::

   >>> fig = plt.figure()
   >>> ax = fig.add_axes([0.0, 0.0, 1.0, 1.0])
   >>> mappable = ax.imshow(...)
   >>> colorbar = ColorBar(mappable)
   >>> ax.add_artist(scalebar)
   >>> plt.show()

The following parameters are available for customization in the matplotlibrc:
    - colorbar.orientation
    - colorbar.length_fraction
    - colorbar.width_fraction
    - colorbar.location
    - colorbar.pad
    - colorbar.border_pad
    - colorbar.sep
    - colorbar.frameon
    - colorbar.color
    - colorbar.box_color
    - colorbar.box_alpha
    - colorbar.ticklocation

See the class documentation (:class:`.ColorBar`) for a description of the
parameters.
"""

# Standard library modules.
import sys
import imp
import warnings

# Third party modules.
from matplotlib.rcsetup import \
    (defaultParams, ValidateInStrings, validate_float,
     validate_legend_loc, validate_bool, validate_color)
from matplotlib.artist import Artist
from matplotlib.cbook import is_string_like
from matplotlib.offsetbox import \
    AnchoredOffsetbox, AuxTransformBox, VPacker, HPacker
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection, LineCollection
from matplotlib.text import Text
from matplotlib.font_manager import FontProperties
from matplotlib.colorbar import ColorbarBase
import matplotlib.contour as contour
import matplotlib.ticker as ticker

import numpy as np

# Local modules.

# Globals and constants variables.

__all__ = ['ColorBar']

# Setup of extra parameters in the matplotlic rc
validate_orientation = ValidateInStrings('orientation',
                                         ['horizontal', 'vertical'])
validate_ticklocation = ValidateInStrings('orientation',
                                          ['auto', 'left', 'right', 'bottom', 'top'])

defaultParams.update(
    {'colorbar.orientation': ['vertical', validate_orientation],
     'colorbar.ticklocation': ['auto', validate_ticklocation],
     'colorbar.length_fraction': [0.2, validate_float],
     'colorbar.width_fraction': [0.02, validate_float],
     'colorbar.location': ['upper right', validate_legend_loc],
     'colorbar.pad': [0.2, validate_float],
     'colorbar.border_pad': [0.1, validate_float],
     'colorbar.sep': [5, validate_float],
     'colorbar.frameon': [True, validate_bool],
     'colorbar.color': ['k', validate_color],
     'colorbar.box_color': ['w', validate_color],
     'colorbar.box_alpha': [1.0, validate_float],
     })

# Reload matplotlib to reset the default parameters
imp.reload(sys.modules['matplotlib'])

class ColorbarBase2(ColorbarBase):
    """
    Disable some methods from the original :class:`ColorbarBase` class of
    matplotlib that required an :class:`Axes` object.
    """

    def _set_label(self):
        pass

    def _patch_ax(self):
        pass

    def _config_axes(self, X, Y):
        pass

    def config_axis(self):
        pass

    def draw_all(self):
        pass

class ColorbarCalculator(object):
    """
    Internally computes the values to draw a colorbar without actually drawing
    it as in the original :class:`ColorbarBase` class from matplotlib.
    """

    def __init__(self, mappable, **kw):
        # Ensure the given mappable's norm has appropriate vmin and vmax set
        # even if mappable.draw has not yet been called.
        mappable.autoscale_None()

        self.mappable = mappable
        kw['cmap'] = cmap = mappable.cmap
        kw['norm'] = mappable.norm

        if isinstance(mappable, contour.ContourSet):
            CS = mappable
            kw['alpha'] = mappable.get_alpha()
            kw['boundaries'] = CS._levels
            kw['values'] = CS.cvalues
            kw['extend'] = CS.extend
            #kw['ticks'] = CS._levels
            kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
            kw['filled'] = CS.filled

        else:
            if getattr(cmap, 'colorbar_extend', False) is not False:
                kw.setdefault('extend', cmap.colorbar_extend)

            if isinstance(mappable, Artist):
                kw['alpha'] = mappable.get_alpha()

        ticks = kw.pop('ticks', None)
        ticklabels = kw.pop('ticklabels', None)

        self._base = ColorbarBase2(None, **kw)
        if ticks:
            self._base.set_ticks(ticks, update_ticks=False)
        if ticks and ticklabels:
            self._base.set_ticklabels(ticklabels, update_ticks=False)

    def calculate_colorbar(self):
        """
        Returns the positions and colors of all intervals inside the colorbar.
        """
        self._base._process_values()
        self._base._find_range()
        X, Y = self._base._mesh()
        C = self._base._values[:, np.newaxis]
        return X, Y, C

    def calculate_ticks(self):
        """
        Returns the sequence of ticks (colorbar data locations),
        ticklabels (strings), and the corresponding offset string.
        """
        return self._base._ticker()

class Colorbar(Artist):

    zorder = 5

    _LOCATIONS = {'upper right':  1,
                  'upper left':   2,
                  'lower left':   3,
                  'lower right':  4,
                  'right':        5,
                  'center left':  6,
                  'center right': 7,
                  'lower center': 8,
                  'upper center': 9,
                  'center':       10}

    def __init__(self, mappable=None, label=None, orientation=None,
                 length_fraction=None, width_fraction=None,
                 location=None, pad=None, border_pad=None, sep=None,
                 frameon=None, color=None, box_color=None, box_alpha=None,
                 font_properties=None, ticks=None, ticklabels=None,
                 ticklocation=None):
        """
        Creates a new color bar.

        :arg mappable: scalar mappable object which implements the methods:
            :meth:`get_cmap` and :meth:`get_array`
            (default: ``None``, the mappable can be specified later)
        :arg label: label on top of the color bar
            (default: ``None``, no label is shown)
        :arg orientation: orientation, ``vertical`` or ``horizontal``
            (default: rcParams['colorbar.orientation'] or ``vertical``)
        :arg length_fraction: length of the color bar as a fraction of the
            axes's width (horizontal) or height (vertical) depending on the
            orientation (default: rcParams['colorbar.length_fraction'] or ``0.2``)
        :arg width_fraction: width of the color bar as a fraction of the
            axes's height (horizontal) or width (vertical) depending on the
            orientation (default: rcParams['colorbar.width_fraction'] or ``0.02``
        :arg location: a location code (same as legend)
            (default: rcParams['colorbar.location'] or ``upper right``)
        :arg pad: fraction of the font size
            (default: rcParams['colorbar.pad'] or ``0.2``)
        :arg border_pad: fraction of the font size
            (default: rcParams['colorbar.border_pad'] or ``0.1``)
        :arg sep: separation between color bar and label in points
            (default: rcParams['colorbar.sep'] or ``5``)
        :arg frameon: if True, will draw a box around the color bar
            (default: rcParams['colorbar.frameon'] or ``True``)
        :arg color: color for the tick text and label
            (default: rcParams['colorbar.color'] or ``k``)
        :arg box_color: color of the box (if *frameon*)
            (default: rcParams['colorbar.box_color'] or ``w``)
        :arg box_alpha: transparency of box
            (default: rcParams['colorbar.box_alpha'] or ``1.0``)
        :arg font_properties: a matplotlib.font_manager.FontProperties instance,
            optional sets the font properties for the label text
        :arg ticks: ticks location
            (default: minimal and maximal values)
        :arg ticklabels: a list of tick labels (same length as ``ticks`` argument)
        :arg ticklocation: location of the ticks: ``left`` or ``right`` for
            vertical oriented colorbar, ``bottom`` or ``top for horizontal
            oriented colorbar, or ``auto`` for automatic adjustment (``right``
            for vertical and ``bottom`` for horizontal oriented colorbar).
            (default: rcParams['colorbar.ticklocation'] or ``auto``)
        """
        Artist.__init__(self)

        self.mappable = mappable
        self.label = label
        self.orientation = orientation
        self.length_fraction = length_fraction
        self.width_fraction = width_fraction
        self.location = location
        self.pad = pad
        self.border_pad = border_pad
        self.sep = sep
        self.frameon = frameon
        self.color = color
        self.box_color = box_color
        self.box_alpha = box_alpha
        self.font_properties = FontProperties(font_properties)
        self.ticks = ticks
        self.ticklabels = ticklabels
        self.ticklocation = ticklocation

    def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if not self.get_mappable():<commit_msg>Fix font properties problem.<commit_after>"""
Artist for matplotlib to display a colorbar as an artist,
instead of an axis as it is the default in matplotlib.

Example::

   >>> fig = plt.figure()
   >>> ax = fig.add_axes([0.0, 0.0, 1.0, 1.0])
   >>> mappable = ax.imshow(...)
   >>> colorbar = ColorBar(mappable)
   >>> ax.add_artist(scalebar)
   >>> plt.show()

The following parameters are available for customization in the matplotlibrc:
    - colorbar.orientation
    - colorbar.length_fraction
    - colorbar.width_fraction
    - colorbar.location
    - colorbar.pad
    - colorbar.border_pad
    - colorbar.sep
    - colorbar.frameon
    - colorbar.color
    - colorbar.box_color
    - colorbar.box_alpha
    - colorbar.ticklocation

See the class documentation (:class:`.ColorBar`) for a description of the
parameters.
"""

# Standard library modules.
import sys
import imp
import warnings

# Third party modules.
from matplotlib.rcsetup import \
    (defaultParams, ValidateInStrings, validate_float,
     validate_legend_loc, validate_bool, validate_color)
from matplotlib.artist import Artist
from matplotlib.cbook import is_string_like
from matplotlib.offsetbox import \
    AnchoredOffsetbox, AuxTransformBox, VPacker, HPacker
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection, LineCollection
from matplotlib.text import Text
from matplotlib.font_manager import FontProperties
from matplotlib.colorbar import ColorbarBase
import matplotlib.contour as contour
import matplotlib.ticker as ticker

import numpy as np

# Local modules.

# Globals and constants variables.

__all__ = ['ColorBar']

# Setup of extra parameters in the matplotlic rc
validate_orientation = ValidateInStrings('orientation',
                                         ['horizontal', 'vertical'])
validate_ticklocation = ValidateInStrings('orientation',
                                          ['auto', 'left', 'right', 'bottom', 'top'])

defaultParams.update(
    {'colorbar.orientation': ['vertical', validate_orientation],
     'colorbar.ticklocation': ['auto', validate_ticklocation],
     'colorbar.length_fraction': [0.2, validate_float],
     'colorbar.width_fraction': [0.02, validate_float],
     'colorbar.location': ['upper right', validate_legend_loc],
     'colorbar.pad': [0.2, validate_float],
     'colorbar.border_pad': [0.1, validate_float],
     'colorbar.sep': [5, validate_float],
     'colorbar.frameon': [True, validate_bool],
     'colorbar.color': ['k', validate_color],
     'colorbar.box_color': ['w', validate_color],
     'colorbar.box_alpha': [1.0, validate_float],
     })

# Reload matplotlib to reset the default parameters
imp.reload(sys.modules['matplotlib'])

class ColorbarBase2(ColorbarBase):
    """
    Disable some methods from the original :class:`ColorbarBase` class of
    matplotlib that required an :class:`Axes` object.
    """

    def _set_label(self):
        pass

    def _patch_ax(self):
        pass

    def _config_axes(self, X, Y):
        pass

    def config_axis(self):
        pass

    def draw_all(self):
        pass

class ColorbarCalculator(object):
    """
    Internally computes the values to draw a colorbar without actually drawing
    it as in the original :class:`ColorbarBase` class from matplotlib.
    """

    def __init__(self, mappable, **kw):
        # Ensure the given mappable's norm has appropriate vmin and vmax set
        # even if mappable.draw has not yet been called.
        mappable.autoscale_None()

        self.mappable = mappable
        kw['cmap'] = cmap = mappable.cmap
        kw['norm'] = mappable.norm

        if isinstance(mappable, contour.ContourSet):
            CS = mappable
            kw['alpha'] = mappable.get_alpha()
            kw['boundaries'] = CS._levels
            kw['values'] = CS.cvalues
            kw['extend'] = CS.extend
            #kw['ticks'] = CS._levels
            kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
            kw['filled'] = CS.filled

        else:
            if getattr(cmap, 'colorbar_extend', False) is not False:
                kw.setdefault('extend', cmap.colorbar_extend)

            if isinstance(mappable, Artist):
                kw['alpha'] = mappable.get_alpha()

        ticks = kw.pop('ticks', None)
        ticklabels = kw.pop('ticklabels', None)

        self._base = ColorbarBase2(None, **kw)
        if ticks:
            self._base.set_ticks(ticks, update_ticks=False)
        if ticks and ticklabels:
            self._base.set_ticklabels(ticklabels, update_ticks=False)

    def calculate_colorbar(self):
        """
        Returns the positions and colors of all intervals inside the colorbar.
        """
        self._base._process_values()
        self._base._find_range()
        X, Y = self._base._mesh()
        C = self._base._values[:, np.newaxis]
        return X, Y, C

    def calculate_ticks(self):
        """
        Returns the sequence of ticks (colorbar data locations),
        ticklabels (strings), and the corresponding offset string.
        """
        return self._base._ticker()

class Colorbar(Artist):

    zorder = 5

    _LOCATIONS = {'upper right':  1,
                  'upper left':   2,
                  'lower left':   3,
                  'lower right':  4,
                  'right':        5,
                  'center left':  6,
                  'center right': 7,
                  'lower center': 8,
                  'upper center': 9,
                  'center':       10}

    def __init__(self, mappable=None, label=None, orientation=None,
                 length_fraction=None, width_fraction=None,
                 location=None, pad=None, border_pad=None, sep=None,
                 frameon=None, color=None, box_color=None, box_alpha=None,
                 font_properties=None, ticks=None, ticklabels=None,
                 ticklocation=None):
        """
        Creates a new color bar.

        :arg mappable: scalar mappable object which implements the methods:
            :meth:`get_cmap` and :meth:`get_array`
            (default: ``None``, the mappable can be specified later)
        :arg label: label on top of the color bar
            (default: ``None``, no label is shown)
        :arg orientation: orientation, ``vertical`` or ``horizontal``
            (default: rcParams['colorbar.orientation'] or ``vertical``)
        :arg length_fraction: length of the color bar as a fraction of the
            axes's width (horizontal) or height (vertical) depending on the
            orientation (default: rcParams['colorbar.length_fraction'] or ``0.2``)
        :arg width_fraction: width of the color bar as a fraction of the
            axes's height (horizontal) or width (vertical) depending on the
            orientation (default: rcParams['colorbar.width_fraction'] or ``0.02``
        :arg location: a location code (same as legend)
            (default: rcParams['colorbar.location'] or ``upper right``)
        :arg pad: fraction of the font size
            (default: rcParams['colorbar.pad'] or ``0.2``)
        :arg border_pad: fraction of the font size
            (default: rcParams['colorbar.border_pad'] or ``0.1``)
        :arg sep: separation between color bar and label in points
            (default: rcParams['colorbar.sep'] or ``5``)
        :arg frameon: if True, will draw a box around the color bar
            (default: rcParams['colorbar.frameon'] or ``True``)
        :arg color: color for the tick text and label
            (default: rcParams['colorbar.color'] or ``k``)
        :arg box_color: color of the box (if *frameon*)
            (default: rcParams['colorbar.box_color'] or ``w``)
        :arg box_alpha: transparency of box
            (default: rcParams['colorbar.box_alpha'] or ``1.0``)
        
        :arg font_properties: font properties of the label text, specified
            either as dict or `fontconfig <http://www.fontconfig.org/>`_
            pattern (XML).
        :type font_properties: :class:`matplotlib.font_manager.FontProperties`,
            :class:`str` or :class:`dict`
        
        :arg ticks: ticks location
            (default: minimal and maximal values)
        :arg ticklabels: a list of tick labels (same length as ``ticks`` argument)
        :arg ticklocation: location of the ticks: ``left`` or ``right`` for
            vertical oriented colorbar, ``bottom`` or ``top for horizontal
            oriented colorbar, or ``auto`` for automatic adjustment (``right``
            for vertical and ``bottom`` for horizontal oriented colorbar).
            (default: rcParams['colorbar.ticklocation'] or ``auto``)
        """
        Artist.__init__(self)

        self.mappable = mappable
        self.label = label
        self.orientation = orientation
        self.length_fraction = length_fraction
        self.width_fraction = width_fraction
        self.location = location
        self.pad = pad
        self.border_pad = border_pad
        self.sep = sep
        self.frameon = frameon
        self.color = color
        self.box_color = box_color
        self.box_alpha = box_alpha

        if font_properties is None:
            font_properties = FontProperties()
        elif isinstance(font_properties, dict):
            font_properties = FontProperties(**font_properties)
        elif is_string_like(font_properties):
            font_properties = FontProperties(font_properties)
        else:
            raise TypeError("Unsupported type for `font_properties`. Pass "
                            "either a dict or a font config pattern as string.")
        self.font_properties = font_properties

        self.ticks = ticks
        self.ticklabels = ticklabels
        self.ticklocation = ticklocation

    def draw(self, renderer, *args, **kwargs):
        if not self.get_visible():
            return
        if not self.get_mappable():
{'fraction_non_alphanumeric': 0.10419492851925284, 'fraction_numerical': 0.005008869873734738, 'mean_word_length': 2.4887149617764837, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5193368', 'n_tokens_mistral': 5496, 'n_tokens_neox': 5302, 'n_words': 1581}