演示Axes Grid2

共享xaxis和yaxis的图像网格。

演示Axes Grid2

  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.axes_grid1 import ImageGrid
  3. import numpy as np
  4. def get_demo_image():
  5. from matplotlib.cbook import get_sample_data
  6. f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
  7. z = np.load(f)
  8. # z is a numpy array of 15x15
  9. return z, (-3, 4, -4, 3)
  10. def add_inner_title(ax, title, loc, size=None, **kwargs):
  11. from matplotlib.offsetbox import AnchoredText
  12. from matplotlib.patheffects import withStroke
  13. if size is None:
  14. size = dict(size=plt.rcParams['legend.fontsize'])
  15. at = AnchoredText(title, loc=loc, prop=size,
  16. pad=0., borderpad=0.5,
  17. frameon=False, **kwargs)
  18. ax.add_artist(at)
  19. at.txt._text.set_path_effects([withStroke(foreground="w", linewidth=3)])
  20. return at
  21. if 1:
  22. F = plt.figure(1, (6, 6))
  23. F.clf()
  24. # prepare images
  25. Z, extent = get_demo_image()
  26. ZS = [Z[i::3, :] for i in range(3)]
  27. extent = extent[0], extent[1]/3., extent[2], extent[3]
  28. # demo 1 : colorbar at each axes
  29. grid = ImageGrid(F, 211, # similar to subplot(111)
  30. nrows_ncols=(1, 3),
  31. direction="row",
  32. axes_pad=0.05,
  33. add_all=True,
  34. label_mode="1",
  35. share_all=True,
  36. cbar_location="top",
  37. cbar_mode="each",
  38. cbar_size="7%",
  39. cbar_pad="1%",
  40. )
  41. for ax, z in zip(grid, ZS):
  42. im = ax.imshow(
  43. z, origin="lower", extent=extent, interpolation="nearest")
  44. ax.cax.colorbar(im)
  45. for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]):
  46. t = add_inner_title(ax, im_title, loc='lower left')
  47. t.patch.set_alpha(0.5)
  48. for ax, z in zip(grid, ZS):
  49. ax.cax.toggle_label(True)
  50. #axis = ax.cax.axis[ax.cax.orientation]
  51. #axis.label.set_text("counts s$^{-1}$")
  52. #axis.label.set_size(10)
  53. #axis.major_ticklabels.set_size(6)
  54. # changing the colorbar ticks
  55. grid[1].cax.set_xticks([-1, 0, 1])
  56. grid[2].cax.set_xticks([-1, 0, 1])
  57. grid[0].set_xticks([-2, 0])
  58. grid[0].set_yticks([-2, 0, 2])
  59. # demo 2 : shared colorbar
  60. grid2 = ImageGrid(F, 212,
  61. nrows_ncols=(1, 3),
  62. direction="row",
  63. axes_pad=0.05,
  64. add_all=True,
  65. label_mode="1",
  66. share_all=True,
  67. cbar_location="right",
  68. cbar_mode="single",
  69. cbar_size="10%",
  70. cbar_pad=0.05,
  71. )
  72. grid2[0].set_xlabel("X")
  73. grid2[0].set_ylabel("Y")
  74. vmax, vmin = np.max(ZS), np.min(ZS)
  75. import matplotlib.colors
  76. norm = matplotlib.colors.Normalize(vmax=vmax, vmin=vmin)
  77. for ax, z in zip(grid2, ZS):
  78. im = ax.imshow(z, norm=norm,
  79. origin="lower", extent=extent,
  80. interpolation="nearest")
  81. # With cbar_mode="single", cax attribute of all axes are identical.
  82. ax.cax.colorbar(im)
  83. ax.cax.toggle_label(True)
  84. for ax, im_title in zip(grid2, ["(a)", "(b)", "(c)"]):
  85. t = add_inner_title(ax, im_title, loc='upper left')
  86. t.patch.set_ec("none")
  87. t.patch.set_alpha(0.5)
  88. grid2[0].set_xticks([-2, 0])
  89. grid2[0].set_yticks([-2, 0, 2])
  90. plt.show()

下载这个示例