How to directly use Axes3D from matplotlib in standard plot to avoid flake8 error
Asked Answered
I

2

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?

Indecent answered 22/3, 2017 at 21:3 Comment(0)
C
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.

Clasping answered 23/3, 2017 at 10:8 Comment(2)
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?Rashad
@Jonathan Holvey The projection list contains all registered classes' names. Hence Axes3D.name is exactly what to put there if an Axes3D is to be created.Clasping
M
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.

Mclellan answered 21/10, 2020 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.