使用插入定位器演示Colorbar

使用插入定位器演示Colorbar

  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.axes_grid1.inset_locator import inset_axes
  3. fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[6, 3])
  4. axins1 = inset_axes(ax1,
  5. width="50%", # width = 10% of parent_bbox width
  6. height="5%", # height : 50%
  7. loc='upper right')
  8. im1 = ax1.imshow([[1, 2], [2, 3]])
  9. plt.colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1, 2, 3])
  10. axins1.xaxis.set_ticks_position("bottom")
  11. axins = inset_axes(ax2,
  12. width="5%", # width = 10% of parent_bbox width
  13. height="50%", # height : 50%
  14. loc='lower left',
  15. bbox_to_anchor=(1.05, 0., 1, 1),
  16. bbox_transform=ax2.transAxes,
  17. borderpad=0,
  18. )
  19. # Controlling the placement of the inset axes is basically same as that
  20. # of the legend. you may want to play with the borderpad value and
  21. # the bbox_to_anchor coordinate.
  22. im = ax2.imshow([[1, 2], [2, 3]])
  23. plt.colorbar(im, cax=axins, ticks=[1, 2, 3])
  24. plt.show()

下载这个示例