matplotlib:半球/楔形的自定义投影

aim*_*aim 3 python projection axes matplotlib

我正在看matplotlib画廊中的自定义投影示例 - 我正在尝试修改它以仅绘制南半球.我已将必要的[-pi/2,pi/2]限制调整为[-pi/2,0].现在我一直在看:

def _gen_axes_patch(self):
    """
    Override this method to define the shape that is used for the
    background of the plot.  It should be a subclass of Patch.

    In this case, it is a Circle (that may be warped by the axes
    transform into an ellipse).  Any data and gridlines will be
    clipped to this shape.
    """
    #return Circle((0.5, 0.5), 0.5)
    return Wedge((0.5,0.5), 0.5, 180, 360)

def _gen_axes_spines(self):
    return {'custom_hammer':mspines.Spine.circular_spine(self,
                                                  (0.5, 0.5), 0.25)}
Run Code Online (Sandbox Code Playgroud)

如您所见,我用Wedge替换了Circle补丁.这就是投影图目前的样子: 投影图

脊柱仍然遵循圆/椭圆 - 我如何指定我希望脊椎遵循楔形的边界?

我不确定如何最好地修改脊柱,所以任何帮助将非常感谢!

谢谢,

亚历克斯

Joe*_*ton 5

只是为了记录,如果你还是python的新手,你肯定会直接进入游泳池的深处.(感谢你向前走!)

你正在做的事情需要对matplotlib的内部工作方式有一个相当详细的了解,这是一个相当复杂的库.

话虽如此,这是一个快速学习的好方法!

对于这样的事情,你需要了解事物结构的内部架构,而不仅仅是"公共"api.

对于大多数情况,你必须深入挖掘并"使用来源".对于任何项目,内部工作的文档都是代码本身.

有人说,对于一个简单的案例,它非常简单.

import numpy as np
from matplotlib.projections.geo import HammerAxes
import matplotlib.projections as mprojections
from matplotlib.axes import Axes
from matplotlib.patches import Wedge
import matplotlib.spines as mspines

class LowerHammerAxes(HammerAxes):
    name = 'lower_hammer'
    def cla(self):
        HammerAxes.cla(self)
        Axes.set_xlim(self, -np.pi, np.pi)
        Axes.set_ylim(self, -np.pi / 2.0, 0)

    def _gen_axes_patch(self):
        return Wedge((0.5, 0.5), 0.5, 180, 360)

    def _gen_axes_spines(self):
        path = Wedge((0, 0), 1.0, 180, 360).get_path()
        spine = mspines.Spine(self, 'circle', path)
        spine.set_patch_circle((0.5, 0.5), 0.5)
        return {'wedge':spine}

mprojections.register_projection(LowerHammerAxes)

if __name__ == '__main__':
    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='lower_hammer')
    ax.grid(True)
    plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

让我们深入研究一下这个_get_axes_spines方法:

def _gen_axes_spines(self):
    """Return the spines for the axes."""
    # Make the path for the spines
    # We need the path, rather than the patch, thus the "get_path()"
    # The path is expected to be centered at 0,0, with radius of 1
    # It will be transformed by `Spine` when we initialize it
    path = Wedge((0, 0), 1.0, 180, 360).get_path()

    # We can fake a "wedge" spine without subclassing `Spine` by initializing 
    # it as a circular spine with the wedge path. 
    spine = mspines.Spine(self, 'circle', path)

    # This sets some attributes of the patch object. In this particular 
    # case, what it sets happens to be approriate for our "wedge spine"
    spine.set_patch_circle((0.5, 0.5), 0.5)

    # Spines in matplotlib are handled in a dict (normally, you'd have top,
    # left, right, and bottom, instead of just wedge). The name is arbitrary
    return {'wedge':spine}
Run Code Online (Sandbox Code Playgroud)

现在有一些问题:

  1. 事物没有正确地在轴内居中
  2. 轴补丁可以缩放一点,以适当占据轴内的空间.
  3. 我们绘制整个地球的网格线然后剪切它们.仅在我们的"较低"楔形内部绘制它们会更有效.

但是,当我们看一下HammerAxes结构如何时,您会注意到很多这些东西(特别是轴补丁的居中)被有效地硬编码到变换中.(正如他们在评论中提到的那样,它意味着是一个"玩具"的例子,并且假设你总是在处理整个地球,这使得转换中的数学变得更加简单.)

如果你想修复它们,你需要调整其中的几种变换HammerAxes._set_lim_and_transforms.

但是,它的工作原理相当不错,所以我将把它作为练习留给读者.:)(请注意,这部分有点难,因为它需要详细了解matplotlib的转换.)