Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Likely incorrect code(?) in colorbar.py #13098

Closed
@anntzer

Description

@anntzer

colorbar.py contains the following snippet (in _process_values):

        if b is not None:
            self._boundaries = np.asarray(b, dtype=float)
            if self.values is None:
                self._values = 0.5 * (self._boundaries[:-1]
                                      + self._boundaries[1:])
                if isinstance(self.norm, colors.NoNorm):
                    self._values = (self._values + 0.00001).astype(np.int16)
                return
            self._values = np.array(self.values)
            return
        if self.values is not None:
            self._values = np.array(self.values)
            if self.boundaries is None:
                b = np.zeros(len(self.values) + 1, 'd')
                b[1:-1] = 0.5 * (self._values[:-1] - self._values[1:])
                b[0] = 2.0 * b[1] - b[2]
                b[-1] = 2.0 * b[-2] - b[-3]
                self._boundaries = b
                return
            self._boundaries = np.array(self.boundaries)
            return

From the first if-block, it appears that values are intended to occur between boundaries, which makes sense and also matches the docstring

    *boundaries*   None or a sequence
    *values*       None or a sequence which must be of length 1 less
                   than the sequence of *boundaries*. For each region
                   delimited by adjacent entries in *boundaries*, the
                   color mapped to the corresponding value in values
                   will be used.

When only boundaries are given, values are thus created at the midpoints between boundaries.

When only values are given (the second block), there are various ways in which the boundaries can be placed (because there's an additional degree of freedom), the point here is not to discuss the best method. But the implementation is clearly not correct: boundaries won't be between values; they will in fact be negative (if values are increasing).

Not sure what the intended use case is, but just putting this here in case someone wants to investigate.

mpl master, but this specific code block dates back to 2006 (from @efiring :)).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions