演示Edge Colorbar

演示Edge Colorbar

  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.axes_grid1 import AxesGrid
  3. def get_demo_image():
  4. import numpy as np
  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 demo_bottom_cbar(fig):
  11. """
  12. A grid of 2x2 images with a colorbar for each column.
  13. """
  14. grid = AxesGrid(fig, 121, # similar to subplot(132)
  15. nrows_ncols=(2, 2),
  16. axes_pad=0.10,
  17. share_all=True,
  18. label_mode="1",
  19. cbar_location="bottom",
  20. cbar_mode="edge",
  21. cbar_pad=0.25,
  22. cbar_size="15%",
  23. direction="column"
  24. )
  25. Z, extent = get_demo_image()
  26. cmaps = [plt.get_cmap("autumn"), plt.get_cmap("summer")]
  27. for i in range(4):
  28. im = grid[i].imshow(Z, extent=extent, interpolation="nearest",
  29. cmap=cmaps[i//2])
  30. if i % 2:
  31. cbar = grid.cbar_axes[i//2].colorbar(im)
  32. for cax in grid.cbar_axes:
  33. cax.toggle_label(True)
  34. cax.axis[cax.orientation].set_label("Bar")
  35. # This affects all axes as share_all = True.
  36. grid.axes_llc.set_xticks([-2, 0, 2])
  37. grid.axes_llc.set_yticks([-2, 0, 2])
  38. def demo_right_cbar(fig):
  39. """
  40. A grid of 2x2 images. Each row has its own colorbar.
  41. """
  42. grid = AxesGrid(F, 122, # similar to subplot(122)
  43. nrows_ncols=(2, 2),
  44. axes_pad=0.10,
  45. label_mode="1",
  46. share_all=True,
  47. cbar_location="right",
  48. cbar_mode="edge",
  49. cbar_size="7%",
  50. cbar_pad="2%",
  51. )
  52. Z, extent = get_demo_image()
  53. cmaps = [plt.get_cmap("spring"), plt.get_cmap("winter")]
  54. for i in range(4):
  55. im = grid[i].imshow(Z, extent=extent, interpolation="nearest",
  56. cmap=cmaps[i//2])
  57. if i % 2:
  58. grid.cbar_axes[i//2].colorbar(im)
  59. for cax in grid.cbar_axes:
  60. cax.toggle_label(True)
  61. cax.axis[cax.orientation].set_label('Foo')
  62. # This affects all axes because we set share_all = True.
  63. grid.axes_llc.set_xticks([-2, 0, 2])
  64. grid.axes_llc.set_yticks([-2, 0, 2])
  65. if 1:
  66. F = plt.figure(1, (5.5, 2.5))
  67. F.subplots_adjust(left=0.05, right=0.93)
  68. demo_bottom_cbar(F)
  69. demo_right_cbar(F)
  70. plt.show()

下载这个示例