python runtime_error ai_generated true

ValueError: 等高线级别数必须至少为2,但得到了1。

ValueError: Number of contour levels must be at least 2, got 1.

ID: python/matplotlib-contour-levels-error

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

版本兼容性

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

根因分析

向contour/contourf的levels参数传递了单个值,但绘制等高线至少需要两个级别。

English

Passing a single value for levels parameter in contour/contourf, which requires at least two levels to draw contours.

generic

解决方案

  1. 95% 成功率 Provide at least two levels as an array
    plt.contour(X, Y, Z, levels=[0, 1, 2])
  2. 90% 成功率 Use an integer to specify the number of automatically chosen levels
    plt.contour(X, Y, Z, levels=10)  # at least 2

无效尝试

常见但无效的做法:

  1. Using a negative number of levels 90% 失败

    Levels must be positive integers or an array; negative numbers raise a different error.

  2. Setting levels as a string like 'auto' 70% 失败

    In some versions, 'auto' is not a valid input; it must be a number or array.