This has always been a source of confusion for me: importing Axes3D
(just some sort of python object for our purposes here) appears to change an unrelated function, add_subplot
, from a different module. How can that happen? Doesn't that violate the whole idea of namespaces?
What I mean is:
>>> import matplotlib.pyplot as plt
>>> f = plt.figure()
>>> ax = f.add_subplot(111, projection='3d')
# Fails with ValueError: Unknown projection '3d'
>>> from mpl_toolkits.mplot3d import Axes3D
>>> ax = f.add_subplot(111, projection='3d')
# Works!
At first I thought it was similar to from __future__ import print_function
, which then somehow magically introduces a function called print
into the namespace. But as it turns out, that is in fact magic, and is specific to the __future__
module.
So, how does Axes3D work?