python type_error ai_generated true

TypeError: rotation must be a number or string, not a list

ID: python/matplotlib-tick-label-rotation-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-04-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active

Root Cause

Passing a list of rotation values to set_xticklabels() or similar, which expects a single value for all labels or a list of labels with a scalar rotation.

generic

中文

向set_xticklabels()等函数传递了旋转值列表,但该函数期望为所有标签设置一个标量值,或为标签列表配合标量旋转。

Workarounds

  1. 95% success Use a scalar rotation value
    ax.set_xticklabels(labels, rotation=45)
  2. 90% success Apply rotation individually using a loop
    for label in ax.get_xticklabels(): label.set_rotation(45)

Dead Ends

Common approaches that don't work:

  1. Using a tuple instead of a list 80% fail

    Tuples are also not accepted; rotation must be a scalar.

  2. Setting rotation via rcParams for all ticks 70% fail

    This changes default but doesn't fix the TypeError from passing a list.