python runtime_error ai_generated true

ValueError: xlim must be a 2-element sequence of numbers, got [0, 10, 20]

ID: python/matplotlib-axes-limits-error

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active

Root Cause

Passing a list with more than two elements to set_xlim() or xlim parameter.

generic

中文

向set_xlim()或xlim参数传递了包含两个以上元素的列表。

Workarounds

  1. 95% success Use a two-element sequence
    ax.set_xlim([0, 10])  # or (0, 10)
  2. 90% success Set min and max separately
    ax.set_xlim(left=0, right=10)

Dead Ends

Common approaches that don't work:

  1. Passing a tuple of three numbers 90% fail

    A tuple with three elements also fails; only two-element sequences are allowed.

  2. Using xlim=(0,10,20) as keyword argument 85% fail

    Same issue; xlim expects exactly two values.