演示锚定方向箭头

演示锚定方向箭头示例

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDirectionArrows
  4. import matplotlib.font_manager as fm
  5. fig, ax = plt.subplots()
  6. ax.imshow(np.random.random((10, 10)))
  7. # Simple example
  8. simple_arrow = AnchoredDirectionArrows(ax.transAxes, 'X', 'Y')
  9. ax.add_artist(simple_arrow)
  10. # High contrast arrow
  11. high_contrast_part_1 = AnchoredDirectionArrows(
  12. ax.transAxes,
  13. '111', r'11$\overline{2}$',
  14. loc='upper right',
  15. arrow_props={'ec': 'w', 'fc': 'none', 'alpha': 1,
  16. 'lw': 2}
  17. )
  18. ax.add_artist(high_contrast_part_1)
  19. high_contrast_part_2 = AnchoredDirectionArrows(
  20. ax.transAxes,
  21. '111', r'11$\overline{2}$',
  22. loc='upper right',
  23. arrow_props={'ec': 'none', 'fc': 'k'},
  24. text_props={'ec': 'w', 'fc': 'k', 'lw': 0.4}
  25. )
  26. ax.add_artist(high_contrast_part_2)
  27. # Rotated arrow
  28. fontprops = fm.FontProperties(family='serif')
  29. roatated_arrow = AnchoredDirectionArrows(
  30. ax.transAxes,
  31. '30', '120',
  32. loc='center',
  33. color='w',
  34. angle=30,
  35. fontproperties=fontprops
  36. )
  37. ax.add_artist(roatated_arrow)
  38. # Altering arrow directions
  39. a1 = AnchoredDirectionArrows(
  40. ax.transAxes, 'A', 'B', loc='lower center',
  41. length=-0.15,
  42. sep_x=0.03, sep_y=0.03,
  43. color='r'
  44. )
  45. ax.add_artist(a1)
  46. a2 = AnchoredDirectionArrows(
  47. ax.transAxes, 'A', ' B', loc='lower left',
  48. aspect_ratio=-1,
  49. sep_x=0.01, sep_y=-0.02,
  50. color='orange'
  51. )
  52. ax.add_artist(a2)
  53. a3 = AnchoredDirectionArrows(
  54. ax.transAxes, ' A', 'B', loc='lower right',
  55. length=-0.15,
  56. aspect_ratio=-1,
  57. sep_y=-0.1, sep_x=0.04,
  58. color='cyan'
  59. )
  60. ax.add_artist(a3)
  61. plt.show()

下载这个示例