From 152f261613e2cf68cd134d7c1276bb00fce68db7 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 25 Oct 2023 22:27:40 -0400 Subject: [PATCH] Backport PR #27178: Try/except import of Axes3D --- lib/matplotlib/projections/__init__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/projections/__init__.py b/lib/matplotlib/projections/__init__.py index 16a5651da1d1..8ce118986065 100644 --- a/lib/matplotlib/projections/__init__.py +++ b/lib/matplotlib/projections/__init__.py @@ -55,7 +55,15 @@ from .. import axes, _docstring from .geo import AitoffAxes, HammerAxes, LambertAxes, MollweideAxes from .polar import PolarAxes -from mpl_toolkits.mplot3d import Axes3D + +try: + from mpl_toolkits.mplot3d import Axes3D +except ImportError: + import warnings + warnings.warn("Unable to import Axes3D. This may be due to multiple versions of " + "Matplotlib being installed (e.g. as a system package and as a pip " + "package). As a result, the 3D projection is not available.") + Axes3D = None class ProjectionRegistry: @@ -87,8 +95,12 @@ def get_projection_names(self): HammerAxes, LambertAxes, MollweideAxes, - Axes3D, ) +if Axes3D is not None: + projection_registry.register(Axes3D) +else: + # remove from namespace if not importable + del Axes3D def register_projection(cls):