python runtime_error ai_generated true

ValueError: xlim必须是包含两个数字的序列,但得到了[0, 10, 20]

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

ID: python/matplotlib-axes-limits-error

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

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. Passing a tuple of three numbers 90% 失败

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

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

    Same issue; xlim expects exactly two values.