【问题标题】:How to add an encircling axes around a polar plot?如何在极坐标图周围添加环绕轴?
【发布时间】:2019-02-21 19:33:19
【问题描述】:

我正在尝试弄清楚如何将轴附加到我的极坐标投影中。新添加的轴应该像环一样环绕在原始极轴上。

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

但是,没有“外部”选项或任何类似于带有 append_axes 参数的极坐标投影的选项。 而不是围绕轴的环,我在原来的轴下方得到了一个新轴(见图)。

是否有任何替代方法可以允许围绕现有极轴创建环形轴?

注意,我不想将它们添加到同一个斧头上,因为比例可能不同。

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")

【问题讨论】:

  • 我改写了你的问题,这样它就不会要求一些根本不可能的事情,即,而不是“我该怎么做?”,你会问“怎么做我得到了?我尝试了,但失败了。”
  • 这个措辞听起来好多了!谢谢你。我不知道上面的方法是特定于矩形图的。

标签: python matplotlib plot projection polar-coordinates


【解决方案1】:

您可能可以通过像这样调整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 值的?
  • 只是通过反复试验,-25 因为下图似乎是从 -25 到 25,-75 表示圆的中心在点 -75,沿着 qith -25 为底部的地块留下 50 个单位。不过,这绝对取决于您想在情节中显示什么。
  • 我想知道有没有一种使用 ax.get_xlim() 样式方法自动化它的好方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多