python runtime_error ai_generated true

UserWarning: FixedFormatter should only be used together with FixedLocator

ID: python/matplotlib-tick-label-overlap

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-05-27First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

Calling set_xticklabels() with a FixedFormatter while the locator is still an auto locator, causing labels to be mismatched with tick positions.

generic

中文

在定位器仍为自动定位器时调用 set_xticklabels() 并使用 FixedFormatter,导致标签与刻度位置不匹配。

Workarounds

  1. 95% success
    import numpy as np
    ax.set_xticks(np.arange(len(labels)))
    ax.set_xticklabels(labels, rotation=45, ha='right')
  2. 90% success
    from matplotlib.ticker import FuncFormatter
    ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: labels[int(x)] if 0 <= int(x) < len(labels) else ''))

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Labels may still be placed at wrong positions, producing a misleading plot.

  2. 85% fail

    The mismatch remains; the warning is informational about the locator/formatter pair.