Fancybox演示

使用Matplotlib绘制精美的盒子。

以下示例显示如何绘制具有不同视觉属性的框。

  1. import matplotlib.pyplot as plt
  2. import matplotlib.transforms as mtransforms
  3. import matplotlib.patches as mpatch
  4. from matplotlib.patches import FancyBboxPatch

首先,我们将展示一些带有fancybox的样本盒。

  1. styles = mpatch.BoxStyle.get_styles()
  2. spacing = 1.2
  3. figheight = (spacing * len(styles) + .5)
  4. fig1 = plt.figure(1, (4 / 1.5, figheight / 1.5))
  5. fontsize = 0.3 * 72
  6. for i, stylename in enumerate(sorted(styles)):
  7. fig1.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename,
  8. ha="center",
  9. size=fontsize,
  10. transform=fig1.transFigure,
  11. bbox=dict(boxstyle=stylename, fc="w", ec="k"))
  12. plt.show()

Fancybox演示示例

接下来,我们将同时展示多个精美的盒子。

  1. # Bbox object around which the fancy box will be drawn.
  2. bb = mtransforms.Bbox([[0.3, 0.4], [0.7, 0.6]])
  3. def draw_bbox(ax, bb):
  4. # boxstyle=square with pad=0, i.e. bbox itself.
  5. p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
  6. abs(bb.width), abs(bb.height),
  7. boxstyle="square,pad=0.",
  8. ec="k", fc="none", zorder=10.,
  9. )
  10. ax.add_patch(p_bbox)
  11. def test1(ax):
  12. # a fancy box with round corners. pad=0.1
  13. p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
  14. abs(bb.width), abs(bb.height),
  15. boxstyle="round,pad=0.1",
  16. fc=(1., .8, 1.),
  17. ec=(1., 0.5, 1.))
  18. ax.add_patch(p_fancy)
  19. ax.text(0.1, 0.8,
  20. r' boxstyle="round,pad=0.1"',
  21. size=10, transform=ax.transAxes)
  22. # draws control points for the fancy box.
  23. # l = p_fancy.get_path().vertices
  24. # ax.plot(l[:,0], l[:,1], ".")
  25. # draw the original bbox in black
  26. draw_bbox(ax, bb)
  27. def test2(ax):
  28. # bbox=round has two optional argument. pad and rounding_size.
  29. # They can be set during the initialization.
  30. p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
  31. abs(bb.width), abs(bb.height),
  32. boxstyle="round,pad=0.1",
  33. fc=(1., .8, 1.),
  34. ec=(1., 0.5, 1.))
  35. ax.add_patch(p_fancy)
  36. # boxstyle and its argument can be later modified with
  37. # set_boxstyle method. Note that the old attributes are simply
  38. # forgotten even if the boxstyle name is same.
  39. p_fancy.set_boxstyle("round,pad=0.1, rounding_size=0.2")
  40. # or
  41. # p_fancy.set_boxstyle("round", pad=0.1, rounding_size=0.2)
  42. ax.text(0.1, 0.8,
  43. ' boxstyle="round,pad=0.1\n rounding_size=0.2"',
  44. size=10, transform=ax.transAxes)
  45. # draws control points for the fancy box.
  46. # l = p_fancy.get_path().vertices
  47. # ax.plot(l[:,0], l[:,1], ".")
  48. draw_bbox(ax, bb)
  49. def test3(ax):
  50. # mutation_scale determine overall scale of the mutation,
  51. # i.e. both pad and rounding_size is scaled according to this
  52. # value.
  53. p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
  54. abs(bb.width), abs(bb.height),
  55. boxstyle="round,pad=0.1",
  56. mutation_scale=2.,
  57. fc=(1., .8, 1.),
  58. ec=(1., 0.5, 1.))
  59. ax.add_patch(p_fancy)
  60. ax.text(0.1, 0.8,
  61. ' boxstyle="round,pad=0.1"\n mutation_scale=2',
  62. size=10, transform=ax.transAxes)
  63. # draws control points for the fancy box.
  64. # l = p_fancy.get_path().vertices
  65. # ax.plot(l[:,0], l[:,1], ".")
  66. draw_bbox(ax, bb)
  67. def test4(ax):
  68. # When the aspect ratio of the axes is not 1, the fancy box may
  69. # not be what you expected (green)
  70. p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
  71. abs(bb.width), abs(bb.height),
  72. boxstyle="round,pad=0.2",
  73. fc="none",
  74. ec=(0., .5, 0.), zorder=4)
  75. ax.add_patch(p_fancy)
  76. # You can compensate this by setting the mutation_aspect (pink).
  77. p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
  78. abs(bb.width), abs(bb.height),
  79. boxstyle="round,pad=0.3",
  80. mutation_aspect=.5,
  81. fc=(1., 0.8, 1.),
  82. ec=(1., 0.5, 1.))
  83. ax.add_patch(p_fancy)
  84. ax.text(0.1, 0.8,
  85. ' boxstyle="round,pad=0.3"\n mutation_aspect=.5',
  86. size=10, transform=ax.transAxes)
  87. draw_bbox(ax, bb)
  88. def test_all():
  89. plt.clf()
  90. ax = plt.subplot(2, 2, 1)
  91. test1(ax)
  92. ax.set_xlim(0., 1.)
  93. ax.set_ylim(0., 1.)
  94. ax.set_title("test1")
  95. ax.set_aspect(1.)
  96. ax = plt.subplot(2, 2, 2)
  97. ax.set_title("test2")
  98. test2(ax)
  99. ax.set_xlim(0., 1.)
  100. ax.set_ylim(0., 1.)
  101. ax.set_aspect(1.)
  102. ax = plt.subplot(2, 2, 3)
  103. ax.set_title("test3")
  104. test3(ax)
  105. ax.set_xlim(0., 1.)
  106. ax.set_ylim(0., 1.)
  107. ax.set_aspect(1)
  108. ax = plt.subplot(2, 2, 4)
  109. ax.set_title("test4")
  110. test4(ax)
  111. ax.set_xlim(-0.5, 1.5)
  112. ax.set_ylim(0., 1.)
  113. ax.set_aspect(2.)
  114. plt.show()
  115. test_all()

Fancybox演示2

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.patches
  3. matplotlib.patches.FancyBboxPatch
  4. matplotlib.patches.BoxStyle
  5. matplotlib.patches.BoxStyle.get_styles
  6. matplotlib.transforms.Bbox

下载这个示例