Exemplo n.º 1
0
    def set_hover_data(self, hover_data):
        """Set the data that will be shown when the mouse hovers over
        the plot and used to create the tick labels.

        :param hover_data:
            must be a sequence of all k-vectors of the simulation, i.e.
            with the same amount of entries than the total number of
            indices (integers) on the x-axis.
        :return: None

        """
        # set the hover data using the parent method:
        CustomAxisFormatter.set_hover_data(self, hover_data)

        if hover_data is None:
            return

        # Now we can update the ticks and labels:
        self._ticks = []
        self._labels = []
        try:
            # here, hover_data can't be a callable:
            axis_length = len(hover_data) - 1
        except TypeError:
            log.error('KVectorAxisFormatter: Could not set ticks, '
                      'hover_data must be a sequence.')
            return
        if axis_length:
            step = max(1, np.floor(axis_length / (self._num_ticks - 1)))
            self._ticks = np.arange(0, axis_length + 1, step, dtype=np.int32)

        vecs = [
            self._get_hover_data_from_continuous_index(x) for x in self._ticks
        ]
        if self._fractions:
            vecs = self._make_fraction_str(vecs)
        for vec in vecs:
            try:
                lbl = self._format_str.format(*vec)
            except IndexError:
                log.warning('KVectorAxisFormatter: format_str "{0}" does not '
                            'match hover_data: {1}'.format(
                                self._format_str, vec))
                lbl = ''
            except ValueError:
                # vec could be a list of strings, rather than a list of
                # numbers, which is normal if self._fractions is True.
                # But if format_str is intended for numbers, we need to
                # strip the format-spec from it:
                lbl = strip_format_spec(self._format_str).format(*vec)
            self._labels.append(lbl)
Exemplo n.º 2
0
    def set_hover_data(self, hover_data):
        """Set the data that will be shown when the mouse hovers over
        the plot and used to create the tick labels.

        :param hover_data:
            must be a sequence of all k-vectors of the simulation, i.e.
            with the same amount of entries than the total number of
            indices (integers) on the x-axis.
        :return: None

        """
        # set the hover data using the parent method:
        CustomAxisFormatter.set_hover_data(self, hover_data)

        if hover_data is None:
            return

        # Now we can update the ticks and labels:
        self._ticks = []
        self._labels = []
        try:
            # here, hover_data can't be a callable:
            axis_length = len(hover_data) - 1
        except TypeError:
            log.error('KVectorAxisFormatter: Could not set ticks, '
                      'hover_data must be a sequence.')
            return
        if axis_length:
            step = max(1, np.floor(axis_length / (self._num_ticks - 1)))
            self._ticks = np.arange(0, axis_length + 1, step, dtype=np.int32)

        vecs = [
            self._get_hover_data_from_continuous_index(x)
            for x in self._ticks]
        if self._fractions:
            vecs = self._make_fraction_str(vecs)
        for vec in vecs:
            try:
                lbl = self._format_str.format(*vec)
            except IndexError:
                log.warning(
                    'KVectorAxisFormatter: format_str "{0}" does not '
                    'match hover_data: {1}'.format(self._format_str, vec))
                lbl = ''
            except ValueError:
                # vec could be a list of strings, rather than a list of
                # numbers, which is normal if self._fractions is True.
                # But if format_str is intended for numbers, we need to
                # strip the format-spec from it:
                lbl = strip_format_spec(self._format_str).format(*vec)
            self._labels.append(lbl)
Exemplo n.º 3
0
def infer_k_axis_label_from_format_string(format_str):
    """Given a format_str that is intended to format tick labels, infer
    an axis label for the k-axis.

    :param format_str:
        the format string that is intended to be used to create tick
        labels by format_str.format(*k_vector), as used in
        KVectorAxisFormatter.

    :return:
        String with an axis label for a k-vector axis.

        If it does not succeed, returns an empty string.

    """
    # Edit format_str, which usually accepts floats, so that it accepts
    # strings.
    fstr = strip_format_spec(format_str)

    try:
        kvec = fstr.format('k_x', 'k_y', 'k_z', '|k|')
    except IndexError:
        # format_str contains more indices than 0-3:
        log.warning('KVectorAxisFormatter: Could not infer axis label '
                    'from format_str. Please supply axis_label.')
        return ''

    # simplify:
    kvec = re.sub(r'\(k_x\W\s*k_y\W\s*k_z\)', r'\\vec{k}', kvec)

    if '$' in kvec:
        return defaults.default_x_axis_label.format(kvec)
    else:
        # add latex math mode if not contained in format_str yet:
        # (and preserve spaces)
        return defaults.default_x_axis_label.format('$' +
                                                    kvec.replace(' ', '\ ') +
                                                    '$')
Exemplo n.º 4
0
def infer_k_axis_label_from_format_string(format_str):
    """Given a format_str that is intended to format tick labels, infer
    an axis label for the k-axis.

    :param format_str:
        the format string that is intended to be used to create tick
        labels by format_str.format(*k_vector), as used in
        KVectorAxisFormatter.

    :return:
        String with an axis label for a k-vector axis.

        If it does not succeed, returns an empty string.

    """
    # Edit format_str, which usually accepts floats, so that it accepts
    # strings.
    fstr = strip_format_spec(format_str)

    try:
        kvec = fstr.format('k_x', 'k_y', 'k_z', '|k|')
    except IndexError:
        # format_str contains more indices than 0-3:
        log.warning('KVectorAxisFormatter: Could not infer axis label '
                    'from format_str. Please supply axis_label.')
        return ''

    # simplify:
    kvec = re.sub(r'\(k_x\W\s*k_y\W\s*k_z\)', r'\\vec{k}', kvec)

    if '$' in kvec:
        return defaults.default_x_axis_label.format(kvec)
    else:
        # add latex math mode if not contained in format_str yet:
        # (and preserve spaces)
        return defaults.default_x_axis_label.format(
            '$' + kvec.replace(' ', '\ ') + '$')