Skip to content

[Bug]: Colorbar get_ticks() return the incorrect array #31086

@williamlus

Description

@williamlus

Bug summary

When creating a colorbar using a ScalarMappable with the NoNorm normalization and a discrete colormap (e.g., viridis with a fixed number of colors), the ticks returned by the colorbar’s get_ticks() method do not align with the visually displayed tick positions. This will cause cbar.set_ticks(cbar.get_ticks()) to change the ticks.

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib import cm, colors
data = [1, 2, 3, 4, 5]
fig, ax = plt.subplots()
cbar = fig.colorbar(cm.ScalarMappable(norm=colors.NoNorm(), cmap=plt.get_cmap("viridis", len(data))), ax=ax)
print(cbar.get_ticks())
cbar.set_ticks(cbar.get_ticks()) # this unexpectedly changes the ticks

Actual outcome

[0. 1. 2. 3. 4. 5.]

Image

Expected outcome

[0. 1. 2. 3. 4.]

Image

Additional information

No response

Operating system

No response

Matplotlib Version

3.10.8

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions