Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the projection (2D or 3D) of a matplotlib axes object?

In Python's matplotlib library it is easy to specify the projection of an axes object on creation:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
ax = plt.axes(projection='3d')

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

like image 796
binnev Avatar asked Dec 25 '22 01:12

binnev


1 Answers

It turns out that the answer given here is actually a better answer to this question: python check if figure is 2d or 3d And the answer i provided at that duplicate is a better answer to this question.

In any case, you can use the name of the axes. This is the string that determines the projection.

plt.gca().name   or   ax.name

if ax is the axes.

A 3D axes' name will be "3d". A 2D axes' name will be "rectilinear", "polar" or some other name depending on the type of plot. Custom projections will have their custom names.

So instead of ax.get_projection() just use ax.name.

like image 166
ImportanceOfBeingErnest Avatar answered Feb 15 '23 11:02

ImportanceOfBeingErnest