只是为了记录在案,你肯定直接跳进泳池的深水区,如果你还是新的蟒蛇。 (并且很荣幸地让你进入!)
你在做什么需要对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()
让我们深入到_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的转换的详细知识。)
哦,只是指出 - 我还是新的蟒蛇,所以任何方法的解释是真的有用!谢谢! – aim 2012-03-13 18:07:02