python module_error ai_generated true

ValueError: Unknown projection '3d'

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-02-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active
3.8 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. Installing mpl_toolkits via pip 95% fail

    mpl_toolkits is part of matplotlib, not a separate package.

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

    The projection is registered only after importing mpl_toolkits.mplot3d.