自定义Boxstyle01

自定义Boxstyle01示例

  1. from matplotlib.path import Path
  2. def custom_box_style(x0, y0, width, height, mutation_size, mutation_aspect=1):
  3. """
  4. Given the location and size of the box, return the path of
  5. the box around it.
  6. - *x0*, *y0*, *width*, *height* : location and size of the box
  7. - *mutation_size* : a reference scale for the mutation.
  8. - *aspect_ratio* : aspect-ration for the mutation.
  9. """
  10. # note that we are ignoring mutation_aspect. This is okay in general.
  11. # padding
  12. mypad = 0.3
  13. pad = mutation_size * mypad
  14. # width and height with padding added.
  15. width = width + 2 * pad
  16. height = height + 2 * pad
  17. # boundary of the padded box
  18. x0, y0 = x0 - pad, y0 - pad
  19. x1, y1 = x0 + width, y0 + height
  20. cp = [(x0, y0),
  21. (x1, y0), (x1, y1), (x0, y1),
  22. (x0-pad, (y0+y1)/2.), (x0, y0),
  23. (x0, y0)]
  24. com = [Path.MOVETO,
  25. Path.LINETO, Path.LINETO, Path.LINETO,
  26. Path.LINETO, Path.LINETO,
  27. Path.CLOSEPOLY]
  28. path = Path(cp, com)
  29. return path
  30. import matplotlib.pyplot as plt
  31. fig, ax = plt.subplots(figsize=(3, 3))
  32. ax.text(0.5, 0.5, "Test", size=30, va="center", ha="center",
  33. bbox=dict(boxstyle=custom_box_style, alpha=0.2))
  34. plt.show()

下载这个示例