How to determine the projection (2D or 3D) of a matplotlib axes object?
<p>In Python's matplotlib library it is easy to specify the projection of an axes object on creation: </p>

<pre><code>import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
ax = plt.axes(projection='3d')
</code></pre>

<p>But how do I determine the projection of an existing axes object? There's no <code>ax.get_projection</code>, <code>ax.properties</code> contains no "projection" key, and a quick google search hasn't turned up anything useful. </p>

<p>I don't think there is an automated way, but there are obviously some properties that only the 3D projection has (e.g. <code>zlim</code>).</p>

<p>So you could write a little helper function to test if its 3D or not:</p>

<pre><code>from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def axesDimensions(ax):
    if hasattr(ax, 'get_zlim'): 
        return 3
    else:
        return 2


fig = plt.figure()

ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212, projection='3d')

print "ax1: ", axesDimensions(ax1)
print "ax2: ", axesDimensions(ax2)
</code></pre>

<p>Which prints:</p>

<pre><code>ax1:  2
ax2:  3
</code></pre>

python matplotlib 3d 2d projection