pythonmatplotlibcartesian

How to check if ax is polar projection in matplotlib?


Is there an attribute that from either fig or ax that can indicate if the projection for the ax is polar or not?

I'm trying to create a basic nested function in one of my more complicated functions that essentially has the following functionality:

is_polar(ax):
    return ax.some_attribute

Though, I'm not sure if this is possible b/c I've looked through he obvious attributes for this. Thought I would reach out to the community before I do an exhaustive manual search.

# Source | https://matplotlib.org/gallery/pie_and_polar_charts/polar_scatter.html
# Fixing random state for reproducibility
np.random.seed(19680801)

# Compute areas and colors
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)

enter image description here


Solution

  • You can check the projection using ax.name, per the matplotlib axes docs. This contains the projection as a string so you can simply do

    if ax.name == 'polar':
        # ....