演示Curvelinear网格

自定义网格和记号行。

此示例演示如何通过在网格上应用转换,使用GridHelperCurve线性来定义自定义网格和注释行。这可以用作第二个打印上的演示,用于在矩形框中创建极轴投影。

Curvelinear网格示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.cbook as cbook
  4. from mpl_toolkits.axisartist import Subplot
  5. from mpl_toolkits.axisartist import SubplotHost, \
  6. ParasiteAxesAuxTrans
  7. from mpl_toolkits.axisartist.grid_helper_curvelinear import \
  8. GridHelperCurveLinear
  9. def curvelinear_test1(fig):
  10. """
  11. grid for custom transform.
  12. """
  13. def tr(x, y):
  14. x, y = np.asarray(x), np.asarray(y)
  15. return x, y - x
  16. def inv_tr(x, y):
  17. x, y = np.asarray(x), np.asarray(y)
  18. return x, y + x
  19. grid_helper = GridHelperCurveLinear((tr, inv_tr))
  20. ax1 = Subplot(fig, 1, 2, 1, grid_helper=grid_helper)
  21. # ax1 will have a ticks and gridlines defined by the given
  22. # transform (+ transData of the Axes). Note that the transform of
  23. # the Axes itself (i.e., transData) is not affected by the given
  24. # transform.
  25. fig.add_subplot(ax1)
  26. xx, yy = tr([3, 6], [5.0, 10.])
  27. ax1.plot(xx, yy, linewidth=2.0)
  28. ax1.set_aspect(1.)
  29. ax1.set_xlim(0, 10.)
  30. ax1.set_ylim(0, 10.)
  31. ax1.axis["t"] = ax1.new_floating_axis(0, 3.)
  32. ax1.axis["t2"] = ax1.new_floating_axis(1, 7.)
  33. ax1.grid(True, zorder=0)
  34. import mpl_toolkits.axisartist.angle_helper as angle_helper
  35. from matplotlib.projections import PolarAxes
  36. from matplotlib.transforms import Affine2D
  37. def curvelinear_test2(fig):
  38. """
  39. polar projection, but in a rectangular box.
  40. """
  41. # PolarAxes.PolarTransform takes radian. However, we want our coordinate
  42. # system in degree
  43. tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()
  44. # polar projection, which involves cycle, and also has limits in
  45. # its coordinates, needs a special method to find the extremes
  46. # (min, max of the coordinate within the view).
  47. # 20, 20 : number of sampling points along x, y direction
  48. extreme_finder = angle_helper.ExtremeFinderCycle(20, 20,
  49. lon_cycle=360,
  50. lat_cycle=None,
  51. lon_minmax=None,
  52. lat_minmax=(0, np.inf),
  53. )
  54. grid_locator1 = angle_helper.LocatorDMS(12)
  55. # Find a grid values appropriate for the coordinate (degree,
  56. # minute, second).
  57. tick_formatter1 = angle_helper.FormatterDMS()
  58. # And also uses an appropriate formatter. Note that,the
  59. # acceptable Locator and Formatter class is a bit different than
  60. # that of mpl's, and you cannot directly use mpl's Locator and
  61. # Formatter here (but may be possible in the future).
  62. grid_helper = GridHelperCurveLinear(tr,
  63. extreme_finder=extreme_finder,
  64. grid_locator1=grid_locator1,
  65. tick_formatter1=tick_formatter1
  66. )
  67. ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper)
  68. # make ticklabels of right and top axis visible.
  69. ax1.axis["right"].major_ticklabels.set_visible(True)
  70. ax1.axis["top"].major_ticklabels.set_visible(True)
  71. # let right axis shows ticklabels for 1st coordinate (angle)
  72. ax1.axis["right"].get_helper().nth_coord_ticks = 0
  73. # let bottom axis shows ticklabels for 2nd coordinate (radius)
  74. ax1.axis["bottom"].get_helper().nth_coord_ticks = 1
  75. fig.add_subplot(ax1)
  76. # A parasite axes with given transform
  77. ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
  78. # note that ax2.transData == tr + ax1.transData
  79. # Anything you draw in ax2 will match the ticks and grids of ax1.
  80. ax1.parasites.append(ax2)
  81. intp = cbook.simple_linear_interpolation
  82. ax2.plot(intp(np.array([0, 30]), 50),
  83. intp(np.array([10., 10.]), 50),
  84. linewidth=2.0)
  85. ax1.set_aspect(1.)
  86. ax1.set_xlim(-5, 12)
  87. ax1.set_ylim(-5, 10)
  88. ax1.grid(True, zorder=0)
  89. if 1:
  90. fig = plt.figure(1, figsize=(7, 4))
  91. fig.clf()
  92. curvelinear_test1(fig)
  93. curvelinear_test2(fig)
  94. plt.show()

下载这个示例