7

When using the typical 3D plot like so:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

flake8 reports the expected error:

./tools.py:62:9: F401 'mpl_toolkits.mplot3d.Axes3D' imported but unused

I know it can be avoided using the # NOQA comment. But is there a different way to formulate the projection in the figure so that the Axes3D object is used?

dsalaj
  • 2,857
  • 4
  • 34
  • 43

2 Answers2

20

If this is only about actually using the import at least once, you can do

ax = fig.gca(projection=Axes3D.name)

as "3d" is the name of the Axes3D class by which it is registered to the projection list.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Would this be how it is intended to be used, or is it a coincidence that the projection list and class name both use the string `3d`? – Jonathan Holvey Jun 18 '18 at 01:43
  • @Jonathan Holvey The projection list contains all registered classes' `name`s. Hence `Axes3D.name` is exactly what to put there if an `Axes3D` is to be created. – ImportanceOfBeingErnest Jun 18 '18 at 09:03
0
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)

However, if I understand the documentation well, it is not the preferred way anymore since version 1.0.0. I still mention it for completeness.

Henri
  • 348
  • 2
  • 8