import matplotlib.ticker

class EmbeddedSciFormatter(matplotlib.ticker.Formatter):
    """Provides a format for embedded-scientific notation."""

    def __init__(self, order_of_magnitude, digits):
        """ORDER_OF_MAGNITUDE is the power of ten to drag out.  DIGITS is the
        number of digits after to point to maintain."""

        self.order_of_magnitude = order_of_magnitude

        # Calculate the format, e.g. '$%f.2\ 10^{5}' .
        self.format = '$%.' + '%d' % digits + r'f\ 10^{%d}$' % \
                order_of_magnitude

    def __call__(self, value, pos_dummy):
        """Returns the formatted value, in math text."""

        # Drag out the order of magnitude.
        reduced_value = value * 10 ** (-self.order_of_magnitude)
        return self.format % reduced_value
