郑重声明一下,如果你还是 Python 新手,那么你肯定会直接跳进池子的最深处。 (感谢您直接进入!)
您正在做的事情需要对 matplotlib 的内部工作有相当详细的了解,这是一个相当复杂的库。
话虽如此,这是快速学习的好方法!
对于这样的事情,你需要了解internal事物的结构方式而不仅仅是“公共”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()
让我们深入研究一下_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}
现在有一些问题:
- 物体未正确居中于轴内
- 轴补丁可以放大一点,以正确占据轴内的空间。
- 我们正在绘制整个地球的网格线,然后裁剪它们。仅将它们绘制在我们的“较低”楔形内会更有效。
然而,当我们看看如何HammerAxes
是结构化的,您会注意到很多这些东西(特别是轴补丁的中心)都被有效地硬编码到变换中。 (正如他们在评论中提到的,这只是一个“玩具”示例,并且假设您始终处理整个地球,使得转换中的数学变得更加简单。)
如果你想修复这些问题,你需要调整其中的几个不同的转换HammerAxes._set_lim_and_transforms
.
然而,它按原样运行得相当好,所以我将把它作为练习留给读者。 :) (请注意,这部分有点难,因为它需要详细了解 matplotlib 的转换。)