4

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

ascripter
  • 5,665
  • 12
  • 45
  • 68
O.rka
  • 29,847
  • 68
  • 194
  • 309

1 Answers1

4

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':
    # ....
William Miller
  • 9,839
  • 3
  • 25
  • 46
  • Is this safe? `ax.name` can be overridden to an arbitrary value. Alternatively, check if `type(ax).__name__` starts with "Polar" – normanius Nov 29 '22 at 18:09
  • @normanius You are correct, the `ax.name` property can be overridden, but that is not recommended unless you use a custom projection; the `name` property is intended to store the projection and it should not be mutated without also changing the projection. They should always match, and if you create your own projection it's on you to make sure they do. – William Miller Nov 29 '22 at 22:50