如何在matplotlib中检查ax是否为极坐标投影?

4

是否有来自figax的属性可以指示ax的投影是否为极坐标?

我正在尝试在我更复杂的函数中创建一个基本的嵌套函数,其基本功能如下:

is_polar(ax):
    return ax.some_attribute

虽然我不确定是否可能,因为我已经查看了明显的属性。在进行详尽的手动搜索之前,我想向社区寻求帮助。

# 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

1个回答

4
您可以按照matplotlib轴文档中的说明,使用ax.name来检查投影。该字符串包含了投影信息,您可以直接进行处理。
if ax.name == 'polar':
    # ....

这个安全吗?ax.name 可以被覆盖为任意值。或者,检查 type(ax).__name__ 是否以 "Polar" 开头。 - normanius
@normanius 您是正确的,ax.name 属性可以被覆盖,但除非您使用自定义投影,否则不建议这样做;name 属性旨在存储投影,不应在不更改投影的情况下进行突变。它们应始终匹配,如果创建自己的投影,则需要确保它们匹配。 - William Miller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接