python runtime_error ai_generated true

UserWarning: FixedFormatter 只应与 FixedLocator 一起使用。

UserWarning: FixedFormatter should only be used together with FixedLocator

ID: python/matplotlib-tick-label-overlap

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-05-27首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

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

  2. 85% 失败

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