自定义投影

通过减轻Matplotlib的许多功能来展示Hammer投影。

自定义投影示例

  1. import matplotlib
  2. from matplotlib.axes import Axes
  3. from matplotlib.patches import Circle
  4. from matplotlib.path import Path
  5. from matplotlib.ticker import NullLocator, Formatter, FixedLocator
  6. from matplotlib.transforms import Affine2D, BboxTransformTo, Transform
  7. from matplotlib.projections import register_projection
  8. import matplotlib.spines as mspines
  9. import matplotlib.axis as maxis
  10. import numpy as np
  11. rcParams = matplotlib.rcParams
  12. # This example projection class is rather long, but it is designed to
  13. # illustrate many features, not all of which will be used every time.
  14. # It is also common to factor out a lot of these methods into common
  15. # code used by a number of projections with similar characteristics
  16. # (see geo.py).
  17. class GeoAxes(Axes):
  18. """
  19. An abstract base class for geographic projections
  20. """
  21. class ThetaFormatter(Formatter):
  22. """
  23. Used to format the theta tick labels. Converts the native
  24. unit of radians into degrees and adds a degree symbol.
  25. """
  26. def __init__(self, round_to=1.0):
  27. self._round_to = round_to
  28. def __call__(self, x, pos=None):
  29. degrees = np.round(np.rad2deg(x) / self._round_to) * self._round_to
  30. if rcParams['text.usetex'] and not rcParams['text.latex.unicode']:
  31. return r"$%0.0f^\circ$" % degrees
  32. else:
  33. return "%0.0f\N{DEGREE SIGN}" % degrees
  34. RESOLUTION = 75
  35. def _init_axis(self):
  36. self.xaxis = maxis.XAxis(self)
  37. self.yaxis = maxis.YAxis(self)
  38. # Do not register xaxis or yaxis with spines -- as done in
  39. # Axes._init_axis() -- until GeoAxes.xaxis.cla() works.
  40. # self.spines['geo'].register_axis(self.yaxis)
  41. self._update_transScale()
  42. def cla(self):
  43. Axes.cla(self)
  44. self.set_longitude_grid(30)
  45. self.set_latitude_grid(15)
  46. self.set_longitude_grid_ends(75)
  47. self.xaxis.set_minor_locator(NullLocator())
  48. self.yaxis.set_minor_locator(NullLocator())
  49. self.xaxis.set_ticks_position('none')
  50. self.yaxis.set_ticks_position('none')
  51. self.yaxis.set_tick_params(label1On=True)
  52. # Why do we need to turn on yaxis tick labels, but
  53. # xaxis tick labels are already on?
  54. self.grid(rcParams['axes.grid'])
  55. Axes.set_xlim(self, -np.pi, np.pi)
  56. Axes.set_ylim(self, -np.pi / 2.0, np.pi / 2.0)
  57. def _set_lim_and_transforms(self):
  58. # A (possibly non-linear) projection on the (already scaled) data
  59. # There are three important coordinate spaces going on here:
  60. #
  61. # 1. Data space: The space of the data itself
  62. #
  63. # 2. Axes space: The unit rectangle (0, 0) to (1, 1)
  64. # covering the entire plot area.
  65. #
  66. # 3. Display space: The coordinates of the resulting image,
  67. # often in pixels or dpi/inch.
  68. # This function makes heavy use of the Transform classes in
  69. # ``lib/matplotlib/transforms.py.`` For more information, see
  70. # the inline documentation there.
  71. # The goal of the first two transformations is to get from the
  72. # data space (in this case longitude and latitude) to axes
  73. # space. It is separated into a non-affine and affine part so
  74. # that the non-affine part does not have to be recomputed when
  75. # a simple affine change to the figure has been made (such as
  76. # resizing the window or changing the dpi).
  77. # 1) The core transformation from data space into
  78. # rectilinear space defined in the HammerTransform class.
  79. self.transProjection = self._get_core_transform(self.RESOLUTION)
  80. # 2) The above has an output range that is not in the unit
  81. # rectangle, so scale and translate it so it fits correctly
  82. # within the axes. The peculiar calculations of xscale and
  83. # yscale are specific to a Aitoff-Hammer projection, so don't
  84. # worry about them too much.
  85. self.transAffine = self._get_affine_transform()
  86. # 3) This is the transformation from axes space to display
  87. # space.
  88. self.transAxes = BboxTransformTo(self.bbox)
  89. # Now put these 3 transforms together -- from data all the way
  90. # to display coordinates. Using the '+' operator, these
  91. # transforms will be applied "in order". The transforms are
  92. # automatically simplified, if possible, by the underlying
  93. # transformation framework.
  94. self.transData = \
  95. self.transProjection + \
  96. self.transAffine + \
  97. self.transAxes
  98. # The main data transformation is set up. Now deal with
  99. # gridlines and tick labels.
  100. # Longitude gridlines and ticklabels. The input to these
  101. # transforms are in display space in x and axes space in y.
  102. # Therefore, the input values will be in range (-xmin, 0),
  103. # (xmax, 1). The goal of these transforms is to go from that
  104. # space to display space. The tick labels will be offset 4
  105. # pixels from the equator.
  106. self._xaxis_pretransform = \
  107. Affine2D() \
  108. .scale(1.0, self._longitude_cap * 2.0) \
  109. .translate(0.0, -self._longitude_cap)
  110. self._xaxis_transform = \
  111. self._xaxis_pretransform + \
  112. self.transData
  113. self._xaxis_text1_transform = \
  114. Affine2D().scale(1.0, 0.0) + \
  115. self.transData + \
  116. Affine2D().translate(0.0, 4.0)
  117. self._xaxis_text2_transform = \
  118. Affine2D().scale(1.0, 0.0) + \
  119. self.transData + \
  120. Affine2D().translate(0.0, -4.0)
  121. # Now set up the transforms for the latitude ticks. The input to
  122. # these transforms are in axes space in x and display space in
  123. # y. Therefore, the input values will be in range (0, -ymin),
  124. # (1, ymax). The goal of these transforms is to go from that
  125. # space to display space. The tick labels will be offset 4
  126. # pixels from the edge of the axes ellipse.
  127. yaxis_stretch = Affine2D().scale(np.pi*2, 1).translate(-np.pi, 0)
  128. yaxis_space = Affine2D().scale(1.0, 1.1)
  129. self._yaxis_transform = \
  130. yaxis_stretch + \
  131. self.transData
  132. yaxis_text_base = \
  133. yaxis_stretch + \
  134. self.transProjection + \
  135. (yaxis_space +
  136. self.transAffine +
  137. self.transAxes)
  138. self._yaxis_text1_transform = \
  139. yaxis_text_base + \
  140. Affine2D().translate(-8.0, 0.0)
  141. self._yaxis_text2_transform = \
  142. yaxis_text_base + \
  143. Affine2D().translate(8.0, 0.0)
  144. def _get_affine_transform(self):
  145. transform = self._get_core_transform(1)
  146. xscale, _ = transform.transform_point((np.pi, 0))
  147. _, yscale = transform.transform_point((0, np.pi / 2.0))
  148. return Affine2D() \
  149. .scale(0.5 / xscale, 0.5 / yscale) \
  150. .translate(0.5, 0.5)
  151. def get_xaxis_transform(self, which='grid'):
  152. """
  153. Override this method to provide a transformation for the
  154. x-axis tick labels.
  155. Returns a tuple of the form (transform, valign, halign)
  156. """
  157. if which not in ['tick1', 'tick2', 'grid']:
  158. raise ValueError(
  159. "'which' must be one of 'tick1', 'tick2', or 'grid'")
  160. return self._xaxis_transform
  161. def get_xaxis_text1_transform(self, pad):
  162. return self._xaxis_text1_transform, 'bottom', 'center'
  163. def get_xaxis_text2_transform(self, pad):
  164. """
  165. Override this method to provide a transformation for the
  166. secondary x-axis tick labels.
  167. Returns a tuple of the form (transform, valign, halign)
  168. """
  169. return self._xaxis_text2_transform, 'top', 'center'
  170. def get_yaxis_transform(self, which='grid'):
  171. """
  172. Override this method to provide a transformation for the
  173. y-axis grid and ticks.
  174. """
  175. if which not in ['tick1', 'tick2', 'grid']:
  176. raise ValueError(
  177. "'which' must be one of 'tick1', 'tick2', or 'grid'")
  178. return self._yaxis_transform
  179. def get_yaxis_text1_transform(self, pad):
  180. """
  181. Override this method to provide a transformation for the
  182. y-axis tick labels.
  183. Returns a tuple of the form (transform, valign, halign)
  184. """
  185. return self._yaxis_text1_transform, 'center', 'right'
  186. def get_yaxis_text2_transform(self, pad):
  187. """
  188. Override this method to provide a transformation for the
  189. secondary y-axis tick labels.
  190. Returns a tuple of the form (transform, valign, halign)
  191. """
  192. return self._yaxis_text2_transform, 'center', 'left'
  193. def _gen_axes_patch(self):
  194. """
  195. Override this method to define the shape that is used for the
  196. background of the plot. It should be a subclass of Patch.
  197. In this case, it is a Circle (that may be warped by the axes
  198. transform into an ellipse). Any data and gridlines will be
  199. clipped to this shape.
  200. """
  201. return Circle((0.5, 0.5), 0.5)
  202. def _gen_axes_spines(self):
  203. return {'geo': mspines.Spine.circular_spine(self, (0.5, 0.5), 0.5)}
  204. def set_yscale(self, *args, **kwargs):
  205. if args[0] != 'linear':
  206. raise NotImplementedError
  207. # Prevent the user from applying scales to one or both of the
  208. # axes. In this particular case, scaling the axes wouldn't make
  209. # sense, so we don't allow it.
  210. set_xscale = set_yscale
  211. # Prevent the user from changing the axes limits. In our case, we
  212. # want to display the whole sphere all the time, so we override
  213. # set_xlim and set_ylim to ignore any input. This also applies to
  214. # interactive panning and zooming in the GUI interfaces.
  215. def set_xlim(self, *args, **kwargs):
  216. raise TypeError("It is not possible to change axes limits "
  217. "for geographic projections. Please consider "
  218. "using Basemap or Cartopy.")
  219. set_ylim = set_xlim
  220. def format_coord(self, lon, lat):
  221. """
  222. Override this method to change how the values are displayed in
  223. the status bar.
  224. In this case, we want them to be displayed in degrees N/S/E/W.
  225. """
  226. lon, lat = np.rad2deg([lon, lat])
  227. if lat >= 0.0:
  228. ns = 'N'
  229. else:
  230. ns = 'S'
  231. if lon >= 0.0:
  232. ew = 'E'
  233. else:
  234. ew = 'W'
  235. return ('%f\N{DEGREE SIGN}%s, %f\N{DEGREE SIGN}%s'
  236. % (abs(lat), ns, abs(lon), ew))
  237. def set_longitude_grid(self, degrees):
  238. """
  239. Set the number of degrees between each longitude grid.
  240. This is an example method that is specific to this projection
  241. class -- it provides a more convenient interface to set the
  242. ticking than set_xticks would.
  243. """
  244. # Skip -180 and 180, which are the fixed limits.
  245. grid = np.arange(-180 + degrees, 180, degrees)
  246. self.xaxis.set_major_locator(FixedLocator(np.deg2rad(grid)))
  247. self.xaxis.set_major_formatter(self.ThetaFormatter(degrees))
  248. def set_latitude_grid(self, degrees):
  249. """
  250. Set the number of degrees between each longitude grid.
  251. This is an example method that is specific to this projection
  252. class -- it provides a more convenient interface than
  253. set_yticks would.
  254. """
  255. # Skip -90 and 90, which are the fixed limits.
  256. grid = np.arange(-90 + degrees, 90, degrees)
  257. self.yaxis.set_major_locator(FixedLocator(np.deg2rad(grid)))
  258. self.yaxis.set_major_formatter(self.ThetaFormatter(degrees))
  259. def set_longitude_grid_ends(self, degrees):
  260. """
  261. Set the latitude(s) at which to stop drawing the longitude grids.
  262. Often, in geographic projections, you wouldn't want to draw
  263. longitude gridlines near the poles. This allows the user to
  264. specify the degree at which to stop drawing longitude grids.
  265. This is an example method that is specific to this projection
  266. class -- it provides an interface to something that has no
  267. analogy in the base Axes class.
  268. """
  269. self._longitude_cap = np.deg2rad(degrees)
  270. self._xaxis_pretransform \
  271. .clear() \
  272. .scale(1.0, self._longitude_cap * 2.0) \
  273. .translate(0.0, -self._longitude_cap)
  274. def get_data_ratio(self):
  275. """
  276. Return the aspect ratio of the data itself.
  277. This method should be overridden by any Axes that have a
  278. fixed data ratio.
  279. """
  280. return 1.0
  281. # Interactive panning and zooming is not supported with this projection,
  282. # so we override all of the following methods to disable it.
  283. def can_zoom(self):
  284. """
  285. Return *True* if this axes supports the zoom box button functionality.
  286. This axes object does not support interactive zoom box.
  287. """
  288. return False
  289. def can_pan(self):
  290. """
  291. Return *True* if this axes supports the pan/zoom button functionality.
  292. This axes object does not support interactive pan/zoom.
  293. """
  294. return False
  295. def start_pan(self, x, y, button):
  296. pass
  297. def end_pan(self):
  298. pass
  299. def drag_pan(self, button, key, x, y):
  300. pass
  301. class HammerAxes(GeoAxes):
  302. """
  303. A custom class for the Aitoff-Hammer projection, an equal-area map
  304. projection.
  305. https://en.wikipedia.org/wiki/Hammer_projection
  306. """
  307. # The projection must specify a name. This will be used by the
  308. # user to select the projection,
  309. # i.e. ``subplot(111, projection='custom_hammer')``.
  310. name = 'custom_hammer'
  311. class HammerTransform(Transform):
  312. """
  313. The base Hammer transform.
  314. """
  315. input_dims = 2
  316. output_dims = 2
  317. is_separable = False
  318. def __init__(self, resolution):
  319. """
  320. Create a new Hammer transform. Resolution is the number of steps
  321. to interpolate between each input line segment to approximate its
  322. path in curved Hammer space.
  323. """
  324. Transform.__init__(self)
  325. self._resolution = resolution
  326. def transform_non_affine(self, ll):
  327. longitude, latitude = ll.T
  328. # Pre-compute some values
  329. half_long = longitude / 2
  330. cos_latitude = np.cos(latitude)
  331. sqrt2 = np.sqrt(2)
  332. alpha = np.sqrt(1 + cos_latitude * np.cos(half_long))
  333. x = (2 * sqrt2) * (cos_latitude * np.sin(half_long)) / alpha
  334. y = (sqrt2 * np.sin(latitude)) / alpha
  335. return np.column_stack([x, y])
  336. transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__
  337. def transform_path_non_affine(self, path):
  338. # vertices = path.vertices
  339. ipath = path.interpolated(self._resolution)
  340. return Path(self.transform(ipath.vertices), ipath.codes)
  341. transform_path_non_affine.__doc__ = \
  342. Transform.transform_path_non_affine.__doc__
  343. def inverted(self):
  344. return HammerAxes.InvertedHammerTransform(self._resolution)
  345. inverted.__doc__ = Transform.inverted.__doc__
  346. class InvertedHammerTransform(Transform):
  347. input_dims = 2
  348. output_dims = 2
  349. is_separable = False
  350. def __init__(self, resolution):
  351. Transform.__init__(self)
  352. self._resolution = resolution
  353. def transform_non_affine(self, xy):
  354. x, y = xy.T
  355. z = np.sqrt(1 - (x / 4) ** 2 - (y / 2) ** 2)
  356. longitude = 2 * np.arctan((z * x) / (2 * (2 * z ** 2 - 1)))
  357. latitude = np.arcsin(y*z)
  358. return np.column_stack([longitude, latitude])
  359. transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__
  360. def inverted(self):
  361. return HammerAxes.HammerTransform(self._resolution)
  362. inverted.__doc__ = Transform.inverted.__doc__
  363. def __init__(self, *args, **kwargs):
  364. self._longitude_cap = np.pi / 2.0
  365. GeoAxes.__init__(self, *args, **kwargs)
  366. self.set_aspect(0.5, adjustable='box', anchor='C')
  367. self.cla()
  368. def _get_core_transform(self, resolution):
  369. return self.HammerTransform(resolution)
  370. # Now register the projection with matplotlib so the user can select
  371. # it.
  372. register_projection(HammerAxes)
  373. if __name__ == '__main__':
  374. import matplotlib.pyplot as plt
  375. # Now make a simple example using the custom projection.
  376. plt.subplot(111, projection="custom_hammer")
  377. p = plt.plot([-1, 1, 1], [-1, -1, 1], "o-")
  378. plt.grid(True)
  379. plt.show()

下载这个示例