演示浮动轴

浮动轴的演示。

  1. from matplotlib.transforms import Affine2D
  2. import mpl_toolkits.axisartist.floating_axes as floating_axes
  3. import numpy as np
  4. import mpl_toolkits.axisartist.angle_helper as angle_helper
  5. from matplotlib.projections import PolarAxes
  6. from mpl_toolkits.axisartist.grid_finder import (FixedLocator, MaxNLocator,
  7. DictFormatter)
  8. import matplotlib.pyplot as plt
  9. # Fixing random state for reproducibility
  10. np.random.seed(19680801)
  11. def setup_axes1(fig, rect):
  12. """
  13. A simple one.
  14. """
  15. tr = Affine2D().scale(2, 1).rotate_deg(30)
  16. grid_helper = floating_axes.GridHelperCurveLinear(
  17. tr, extremes=(-0.5, 3.5, 0, 4))
  18. ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
  19. fig.add_subplot(ax1)
  20. aux_ax = ax1.get_aux_axes(tr)
  21. grid_helper.grid_finder.grid_locator1._nbins = 4
  22. grid_helper.grid_finder.grid_locator2._nbins = 4
  23. return ax1, aux_ax
  24. def setup_axes2(fig, rect):
  25. """
  26. With custom locator and formatter.
  27. Note that the extreme values are swapped.
  28. """
  29. tr = PolarAxes.PolarTransform()
  30. pi = np.pi
  31. angle_ticks = [(0, r"$0$"),
  32. (.25*pi, r"$\frac{1}{4}\pi$"),
  33. (.5*pi, r"$\frac{1}{2}\pi$")]
  34. grid_locator1 = FixedLocator([v for v, s in angle_ticks])
  35. tick_formatter1 = DictFormatter(dict(angle_ticks))
  36. grid_locator2 = MaxNLocator(2)
  37. grid_helper = floating_axes.GridHelperCurveLinear(
  38. tr, extremes=(.5*pi, 0, 2, 1),
  39. grid_locator1=grid_locator1,
  40. grid_locator2=grid_locator2,
  41. tick_formatter1=tick_formatter1,
  42. tick_formatter2=None)
  43. ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
  44. fig.add_subplot(ax1)
  45. # create a parasite axes whose transData in RA, cz
  46. aux_ax = ax1.get_aux_axes(tr)
  47. aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax
  48. ax1.patch.zorder = 0.9 # but this has a side effect that the patch is
  49. # drawn twice, and possibly over some other
  50. # artists. So, we decrease the zorder a bit to
  51. # prevent this.
  52. return ax1, aux_ax
  53. def setup_axes3(fig, rect):
  54. """
  55. Sometimes, things like axis_direction need to be adjusted.
  56. """
  57. # rotate a bit for better orientation
  58. tr_rotate = Affine2D().translate(-95, 0)
  59. # scale degree to radians
  60. tr_scale = Affine2D().scale(np.pi/180., 1.)
  61. tr = tr_rotate + tr_scale + PolarAxes.PolarTransform()
  62. grid_locator1 = angle_helper.LocatorHMS(4)
  63. tick_formatter1 = angle_helper.FormatterHMS()
  64. grid_locator2 = MaxNLocator(3)
  65. # Specify theta limits in degrees
  66. ra0, ra1 = 8.*15, 14.*15
  67. # Specify radial limits
  68. cz0, cz1 = 0, 14000
  69. grid_helper = floating_axes.GridHelperCurveLinear(
  70. tr, extremes=(ra0, ra1, cz0, cz1),
  71. grid_locator1=grid_locator1,
  72. grid_locator2=grid_locator2,
  73. tick_formatter1=tick_formatter1,
  74. tick_formatter2=None)
  75. ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
  76. fig.add_subplot(ax1)
  77. # adjust axis
  78. ax1.axis["left"].set_axis_direction("bottom")
  79. ax1.axis["right"].set_axis_direction("top")
  80. ax1.axis["bottom"].set_visible(False)
  81. ax1.axis["top"].set_axis_direction("bottom")
  82. ax1.axis["top"].toggle(ticklabels=True, label=True)
  83. ax1.axis["top"].major_ticklabels.set_axis_direction("top")
  84. ax1.axis["top"].label.set_axis_direction("top")
  85. ax1.axis["left"].label.set_text(r"cz [km$^{-1}$]")
  86. ax1.axis["top"].label.set_text(r"$\alpha_{1950}$")
  87. # create a parasite axes whose transData in RA, cz
  88. aux_ax = ax1.get_aux_axes(tr)
  89. aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax
  90. ax1.patch.zorder = 0.9 # but this has a side effect that the patch is
  91. # drawn twice, and possibly over some other
  92. # artists. So, we decrease the zorder a bit to
  93. # prevent this.
  94. return ax1, aux_ax
  1. fig = plt.figure(1, figsize=(8, 4))
  2. fig.subplots_adjust(wspace=0.3, left=0.05, right=0.95)
  3. ax1, aux_ax1 = setup_axes1(fig, 131)
  4. aux_ax1.bar([0, 1, 2, 3], [3, 2, 1, 3])
  5. ax2, aux_ax2 = setup_axes2(fig, 132)
  6. theta = np.random.rand(10)*.5*np.pi
  7. radius = np.random.rand(10) + 1.
  8. aux_ax2.scatter(theta, radius)
  9. ax3, aux_ax3 = setup_axes3(fig, 133)
  10. theta = (8 + np.random.rand(10)*(14 - 8))*15. # in degrees
  11. radius = np.random.rand(10)*14000.
  12. aux_ax3.scatter(theta, radius)
  13. plt.show()

演示浮动轴

下载这个示例