예제 #1
0
    def __call__(self, x, pos=None):
        'Return the format for tick val *x* at position *pos*'
        b = self._base
        import math
        from matplotlib.ticker import is_close_to_int, nearest_long

        # only label the decades
        if x == 0:
            return "$0$"

        fx = math.log(abs(x)) / math.log(b)
        is_decade = is_close_to_int(fx)

        sign_string = '-' if x < 0 else ''

        # use string formatting of the base if it is not an integer
        if b % 1 == 0.0:
            base = '%d' % b
        else:
            base = '%s' % b

        if not is_decade and self.labelOnlyBase:
            return ''
        elif not is_decade:
            return ('$%s%s^{%.2f}$') % \
                                        (sign_string, base, fx)
        else:
            if nearest_long(fx) == 0.:
                return ('$%s1$') % sign_string
            elif nearest_long(fx) == 1.:
                return ('$%s%s$') % (sign_string, base)
            else:
                return ('$%s%s^{%d}$') % (sign_string, base, nearest_long(fx))
예제 #2
0
    def __call__(self, x, pos=None, fmt=None):
        if not x:
            return _math((fmt or "%s") % 0)

        # determine whether to label or not
        sign = '-' if x < 0 else ''
        x = abs(x)
        b = self._base
        fx = log(x) / log(b)

        is_x_decade = mticker.is_close_to_int(fx)
        if self.labelOnlyBase and not is_x_decade:
            return ''

        # work out whether to show this label
        # if there are enough major ticks or this formatter doesn't support
        # minor ticks, return a blank string
        exponent = numpy.round(fx) if is_x_decade else numpy.floor(fx)
        coeff = numpy.round(x / b ** exponent)
        nticks = self._num_ticks()
        if (
                nticks >= 1
                and self._sublabels is not None
                and coeff not in self._sublabels
        ):
            return ''

        # enable custom format
        if fmt:
            return _math("{}{}".format(sign, fmt % x))

        return super().__call__(x, pos=pos)
예제 #3
0
 def __call__(self, x, pos=None):
     """
     Return the format for tick value `x` at position `pos`.
     """
     if len(self.locs) == 0:
         return ''
     else:
         from matplotlib.ticker import is_close_to_int
         if abs(x) < 1e4 and is_close_to_int(x):
             return "%d" % int(np.round(x))
         else:
             s = self.pprint_val(x)
             return self.fix_minus(s)
예제 #4
0
    def __call__(self, x, pos=None):
        'Return the format for tick val *x* at position *pos*'
        b = self._base
        usetex = rcParams['text.usetex']

        # only label the decades
        if x == 0:
            if usetex:
                return '$0$'
            else:
                return '$\mathdefault{0}$'

        fx = math.log(abs(x)) / math.log(b)
        is_decade = is_close_to_int(fx)

        fx = math.floor(fx)
        mantissa = abs(x) / math.pow(b, fx)

        sign_string = '-' if x < 0 else ''

        # use string formatting of the base if it is not an integer
        if b % 1 == 0.0:
            base = '%d' % b
        else:
            base = '%s' % b

        if not is_decade and self.labelOnlyBase:
            return ''
        else:  #elif not is_decade:
            # Always show the mantissa
            if is_close_to_int(mantissa):
                mantissa = int(mantissa)

            if usetex:
                return (r'$%s%s\cdot%s^{%d}$') % \
                                            (sign_string, mantissa, base, fx)
            else:
                return ('$\mathregular{%s%s\cdot%s^{%d}}$') % \
                                            (sign_string, mantissa, base, fx)
예제 #5
0
파일: formatter.py 프로젝트: adamcpovey/cis
    def __call__(self, x, pos=None):
        """Return the format for tick val *x* at position *pos*"""
        b = self._base
        usetex = rcParams['text.usetex']

        # only label the decades
        if x == 0:
            if usetex:
                return '$0$'
            else:
                return '$\mathdefault{0}$'

        fx = math.log(abs(x)) / math.log(b)
        is_decade = is_close_to_int(fx)
        fx = math.floor(fx)

        decimal_part = x / (b**fx)

        sign_string = '-' if x < 0 else ''

        # use string formatting of the base if it is not an integer
        if b % 1 == 0.0:
            base = '%d' % b
        else:
            base = '%s' % b

        if not is_decade and self.labelOnlyBase:
            return ''
        elif not is_decade:
            if usetex:
                return '$%s\\times%s%s^{%d}$' % ('%1.1f' % decimal_part,
                                                 sign_string, base, fx)
            else:
                return '$\mathdefault{%s\\times%s%s^{%d}}$' % (
                    '%1.1f' % decimal_part, sign_string, base, fx)
        else:
            if usetex:
                return r'$%s%s^{%d}$' % (sign_string, base, round(fx))
            else:
                return r'$\mathdefault{%s%s^{%d}}$' % (sign_string, base,
                                                       round(fx))
예제 #6
0
 def _non_decade_format(self, sign_string, base, fx, usetex):
     'Return string for non-decade locations'
     b = float(base)
     exponent = math.floor(fx)
     coeff = b**fx / b**exponent
     if is_close_to_int(coeff):
         coeff = nearest_long(coeff)
     if usetex:
         return (r'{sign}{coeff:.{precision}}\times{base}^{{{exponent}}}'.
                 format(sign=sign_string,
                        coeff=coeff,
                        precision=self.precision,
                        base=base,
                        exponent=exponent))
     else:
         return ('$%s$' % mpl.ticker._mathdefault(
             r'{sign}{coeff:.{precision}}\times{base}^{{{exponent}}}'.
             format(sign=sign_string,
                    coeff=coeff,
                    precision=self.precision,
                    base=base,
                    exponent=exponent)))
예제 #7
0
파일: formatter.py 프로젝트: cedadev/cis
    def __call__(self, x, pos=None):
        """Return the format for tick val *x* at position *pos*"""
        b = self._base
        usetex = rcParams['text.usetex']

        # only label the decades
        if x == 0:
            if usetex:
                return '$0$'
            else:
                return '$\mathdefault{0}$'

        fx = math.log(abs(x)) / math.log(b)
        is_decade = is_close_to_int(fx)
        fx = math.floor(fx)

        decimal_part = x / (b**fx)

        sign_string = '-' if x < 0 else ''

        # use string formatting of the base if it is not an integer
        if b % 1 == 0.0:
            base = '%d' % b
        else:
            base = '%s' % b

        if not is_decade and self.labelOnlyBase:
            return ''
        elif not is_decade:
            if usetex:
                return '$%s\\times%s%s^{%d}$' % ('%1.1f' % decimal_part, sign_string, base, fx)
            else:
                return '$\mathdefault{%s\\times%s%s^{%d}}$' % ('%1.1f' % decimal_part, sign_string, base, fx)
        else:
            if usetex:
                return r'$%s%s^{%d}$' % (sign_string, base, nearest_long(fx))
            else:
                return r'$\mathdefault{%s%s^{%d}}$' % (sign_string, base, nearest_long(fx))