python module_error ai_generated true

ValueError: 未知投影'3d'

ValueError: Unknown projection '3d'

ID: python/matplotlib-3d-projection-error

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

版本兼容性

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

根因分析

尝试创建3D子图,但未导入mpl_toolkits.mplot3d,或使用了不支持'3d'投影的旧版matplotlib。

English

Trying to create a 3D subplot without importing mpl_toolkits.mplot3d or using an older matplotlib version where '3d' is not recognized.

generic

解决方案

  1. 95% 成功率 Import mpl_toolkits.mplot3d explicitly
    from mpl_toolkits.mplot3d import Axes3D; fig = plt.figure(); ax = fig.add_subplot(111, projection='3d')
  2. 90% 成功率 Use the built-in subplot_kw argument
    fig, ax = plt.subplots(subplot_kw={'projection': '3d'})

无效尝试

常见但无效的做法:

  1. Installing mpl_toolkits via pip 95% 失败

    mpl_toolkits is part of matplotlib, not a separate package.

  2. Using projection='3d' without importing anything 90% 失败

    The projection is registered only after importing mpl_toolkits.mplot3d.