1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| from matplotlib.path import Path from matplotlib.patches import BoxStyle import matplotlib.pyplot as plt
class MyStyle(BoxStyle._Base): """ A simple box. """
def __init__(self, pad=0.3): """ The arguments need to be floating numbers and need to have default values.
*pad* amount of padding """
self.pad = pad super().__init__()
def transmute(self, x0, y0, width, height, mutation_size): """ Given the location and size of the box, return the path of the box around it.
- *x0*, *y0*, *width*, *height* : location and size of the box - *mutation_size* : a reference scale for the mutation.
Often, the *mutation_size* is the font size of the text. You don't need to worry about the rotation as it is automatically taken care of. """
pad = mutation_size * self.pad
width, height = width + 2.*pad, \ height + 2.*pad,
x0, y0 = x0-pad, y0-pad, x1, y1 = x0+width, y0 + height
cp = [(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0-pad, (y0+y1)/2.), (x0, y0), (x0, y0)]
com = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
path = Path(cp, com)
return path
BoxStyle._style_list["angled"] = MyStyle
fig, ax = plt.subplots(figsize=(3, 3)) ax.text(0.5, 0.5, "Test", size=30, va="center", ha="center", rotation=30, bbox=dict(boxstyle="angled,pad=0.5", alpha=0.2))
del BoxStyle._style_list["angled"]
plt.show()
|