如何在极坐标图周围添加圆形轴?

8

我正在尝试弄清楚如何将轴附加到我的极坐标图中。新添加的轴应该像一个环一样包裹在原始极坐标轴周围。

为此,我尝试在通过make_axes_locatable创建的分隔符上使用append_axes,用于polar matplotlib投影ax

然而,在append_axes参数中没有“outer”或任何类似极坐标投影的选项。与其说是环绕轴的环,不如说我得到了一个新的轴,就在原来的轴下面(见图片)。

是否有其他替代方案,可以创建一个圆形的轴,绕在现有极坐标轴周围?

请注意,我不想将它们添加到相同的轴上,因为刻度可能不同。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

plt.style.use("seaborn-white")
def synthesize(polar=False):
    fig = plt.figure()
    ax = fig.add_subplot(111, polar=polar)
    t = np.linspace(0,2*np.pi)
    r_sin = np.sin(t)
    r_cos = np.cos(t)
    for r in [r_sin, r_cos]:
        ax.scatter(t, r, c=r, s=t*100, edgecolor="white", cmap=plt.cm.magma_r)
        ax.scatter(t, -r, c=r, s=t*100, edgecolor="white", cmap=plt.cm.magma_r)
    ax.set_title("polar={}".format(polar),fontsize=15)
    ax.set_xticklabels([])
    return fig, ax, t

# Rectilinear
fig, ax, t = synthesize(polar=False)
# Here are the plot dimensions in response to the answer below
xlim = ax.get_xlim()
ylim = ax.get_ylim()
rlim = (ax.get_rmin(), ax.get_rmax())
print(xlim, ylim, rlim)
(0.0, 6.283185307179586) (0.0, 2.2437621373846617) (0.0, 2.2437621373846617)
# Encircling axis
divider = make_axes_locatable(ax)
ax_below = divider.append_axes("bottom", size="32.8%", pad=0.1)
ax_below.scatter(t, np.tan(t), c="black", edgecolor="white")

# Polar
fig, ax, t = synthesize(polar=True)
divider = make_axes_locatable(ax)
ax_below = divider.append_axes("bottom", size="32.8%", pad=0.1)
ax_below.scatter(t, np.tan(t), c="black", edgecolor="white")

enter image description here


2
我重新表述了你的问题,这样它就不会询问一些根本不可能的事情,例如,你可以问“我该如何获得所需结果?”而不是“我该如何做到<不可能的事情>?”我尝试了<不可能的事情>,但失败了。 - ImportanceOfBeingErnest
这个措辞听起来好多了!谢谢。我不知道上面的方法是针对矩形图的。 - O.rka
1个回答

2

您可以通过调整set_rmaxset_rorigin来实现某些功能,例如:

最初的回答:

import numpy as np
import matplotlib.pyplot as plt

plt.style.use("seaborn-white")
def synthesize(polar=False):
    fig = plt.figure()
    ax = fig.add_subplot(111, polar=polar)
    ax.set_rmax(30)
    t = np.linspace(0,2*np.pi)
    r_sin = np.sin(t)
    r_cos = np.cos(t)
    for r in [r_sin, r_cos]:
        ax.scatter(t, r, c=r, s=t*100, edgecolor="white", cmap=plt.cm.magma_r)
        ax.scatter(t, -r, c=r, s=t*100, edgecolor="white", cmap=plt.cm.magma_r)
    ax.set_title("polar={}".format(polar),fontsize=15)
    ax.set_xticklabels([])
    return fig, ax, t

# Polar
fig, ax, t = synthesize(polar=True)
ax_below = fig.add_subplot(111, polar=True, frameon=True)

# ax_below = divider.append_axes("bottom", size="32.8%", pad=0.1)
ax_below.scatter(t, np.tan(t), c="black", edgecolor="white")
ax_below.set_rorigin(-75)
ax_below.set_rmin(-25)
plt.show()

你是如何决定使用-25、-75和30这些值的? - O.rka
1
仅通过试错,-25是因为下方的图似乎从-25到25,-75表示圆的中心在点-75处,加上-25在底部留下50个单位的绘图。但这肯定取决于您想在图中展示什么。 - vlizana
我在想是否有一种好的方法可以使用ax.get_xlim()这样的方法来自动化它? - O.rka

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