【问题标题】:How to replace a Matplotlib axis with a curvilinear axis如何用曲线轴替换 Matplotlib 轴
【发布时间】:2019-11-23 19:42:50
【问题描述】:

在文档here 中有一个很好的曲线轴网格示例。这假设您有一个空图形,然后使用以下方法创建曲线轴:

ax1 = Subplot(fig, 1, 2, 1, grid_helper=grid_helper)

但是,我正在编写一个给定现有轴的函数,我希望该函数将该轴 (ax) 替换为新的曲线轴。

到目前为止,这是我的功能的开始:

import matplotlib.pyplot as plt
from mpl_toolkits.axisartist import SubplotHost
from mpl_toolkits.axisartist.grid_helper_curvelinear import GridHelperCurveLinear
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D

def sgrid(ax=None):
    """
    Adds an s-plane grid of constant damping factors and natural
    frequencies to a plot.  If ax is not specified, the current
    figure axis is used.

    Parameters
    ----------
    ax : matplotlib axis object
        If not passed, uses gca() to get current axis.

    Returns
    -------
    ax : matplotlib axis
    """

    grid_helper = GridHelperCurveLinear(
        ... some stuff ...
    )

    if ax is None:
        # Get the current axis or create a new figure with one
        ax = plt.gca()
    fig = ax.figure

    # TODO: How can we change the axis if it is already created?
    ax = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)

    ... (code that adds more stuff to ax) ...

    return ax

另外,我不确定我是否理解SubplotHost 的论点。这些是新轴的初始化参数还是什么?

更新

这里的目标是模拟 Pandas.Series.plot 函数的工作方式。所需的用例是这样的:

H = tf([2, 5, 1],[1, 2, 3])
rlocus(H)
sgrid()
plt.show()

>>> fig, axes = plt.subplots(2, 1)
>>> rlocus(H1, ax=axes[1])
>>> rlocus(H2, ax=axes[2])
>>> for ax in axes:
>>>     sgrid(ax=ax)  # Later we might want to add ax.zgrid()
>>> plt.show()

sgridrlocus 的顺序理想情况下应该如上,因为这类似于我们正在模拟的 MATLAB 函数以及它们相似的 plt.grid() 方法。

【问题讨论】:

  • 您是否需要将原始轴留在原处(可能已经有一些艺术家在场),或者您是否可以销毁原始轴并在同一位置用新轴替换它?
  • 不能将现有的轴突然变成不同的轴;所以你需要预先知道参数然后创建这个轴。
  • 感谢您的 cmets。我很乐意破坏现有的轴并重新创建它。这里的目标是对只有一个轴参数的其他函数的行为进行建模(类似于 Pandas.Sereis.plot 等)。而不是必须传递一个数字。我将添加所需用例的示例。
  • 有谁知道实际使用grid_helper 的方法/类?我正在努力追踪 SubplotHost 最终在 Matplotlib 文档中发送此参数的位置。

标签: python matplotlib plot axis figure


【解决方案1】:

grid_helpermpl_toolkits.axisartist.axislines.Axes 的关键字。 https://matplotlib.org/api/_as_gen/mpl_toolkits.axisartist.axislines.html

您可能需要检查提供的轴是否是此 Axes 扩展的子类,否则重新创建轴:

from mpl_toolkits.axisartist import SubplotHost,axislines

...

def sgrid(ax1=None):
    ...
    if ax1 is None:
        ax1=plt.gca()
    fig=ax1.figure
    if not isinstance(ax1,axislines.Axes):
        subargs=ax1.get_geometry()
        fig.delaxes(ax1)
        ax2 = SubplotHost(fig, *subargs, grid_helper=grid_helper)
        fig.add_subplot(ax2)
    else:
       ax2=ax1
    ...

    return ax2

注意sgrid() 的调用者仍然引用ax1,它不再是图中的一部分。除了删除和重新创建之外,可能需要更复杂地替换轴引用。

【讨论】:

  • 这太棒了!谢谢。现在我已经实现了它似乎可以工作。我喜欢返回新轴(ax2)的想法,但如果该方法可以更新原始图形对它的引用(即在figure.axes 中),那就太好了。我想这是不可能的或有问题的。
  • 您是否考虑过使用Axes.add_child_axes()Axes.inset_axes() 或类似的东西?您可以使用sharex=sharey= 链接边界和限制,并将其放在父轴后面。
  • 这是一个有趣的想法。我会看看。我是否应该担心它说“此方法从 3.0 开始是实验性的,API 可能会更改”。在文档页面上?
猜你喜欢
  • 2020-03-27
  • 2022-12-18
  • 1970-01-01
  • 2021-02-05
  • 1970-01-01
  • 2011-01-22
  • 2022-01-18
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多