8000 [Bug]: `get_ticklabels`/`set_ticklabels` gives incorrect values in log plot · Issue #29284 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: get_ticklabels/set_ticklabels gives incorrect values in log plot #29284
Closed
@elimbaum

Description

@elimbaum

Bug summary

In a certain scenario, adjusting the ylim of a log plot and updating the yticklabels gives numerically incorrect ticks while still plotting the correct values. We were calling get_/set_yticklabels because the cmss10 font didn't have a certain glyph which needed to be replaced, but the bug can be reproduced with the more minimal code below.

Code for reproduction

import matplotlib.pyplot as plt

plt.rcParams.update({
    "figure.figsize": (6, 2),
    "font.size": 12,
    "font.family": "cmss10",
})

x = ['A', 'B', 'C']
y = [10, 1000, 10000]

fig, ax = plt.subplots(layout='constrained')

# Plot and label data
b = ax.bar(x, y)
ax.bar_label(b, y)

# Log scale for vertical axis
ax.set_yscale('log')

# Give a bit extra space for the labels
plt.ylim(None, plt.ylim()[1] * 8)

# Originally was using this to replace special characters in the font
ax.set_yticklabels(ax.get_yticklabels())

plt.savefig('bug-output.png', dpi=300)

Actual outcome

bug-output

The y-axis ticks are incorrectly labeled.

Expected outcome

bug-output

Additional information

Changing plt.ylim(None, plt.ylim()[1] * 8) to plt.ylim(None, plt.ylim()[1] * 7) (that is, just a bit less extra room) fixes the issue. Granted, most other sets of changes also fix it: this appears to be an extremely idiosyncratic bug, so I'm not clear what the real issue is. Changing the plot size, removing 'constrained', changing the font, removing the ylim statement entirely... these all appear to fix it. It's just the specific combination of parameters that we happened to stumble into this.

Operating system

Mac OS

Matplotlib Version

3.9.3

Matplotlib Backend

macosx

Python version

3.10.2

Jupyter version

N/A

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0