|
| 1 | +from matplotlib.patches import Rectangle, Ellipse |
| 2 | + |
| 3 | +from matplotlib.offsetbox import AnchoredOffsetbox, AuxTransformBox, VPacker,\ |
| 4 | + TextArea, DrawingArea |
| 5 | + |
| 6 | + |
| 7 | +class AnchoredText(AnchoredOffsetbox): |
| 8 | + def __init__(self, s, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True): |
| 9 | + |
| 10 | + self.txt = TextArea(s, |
| 11 | + minimumdescent=False) |
| 12 | + |
| 13 | + |
| 14 | + super(AnchoredText, self).__init__(loc, pad=pad, borderpad=borderpad, |
| 15 | + child=self.txt, |
| 16 | + prop=prop, |
| 17 | + frameon=frameon) |
| 18 | + |
| 19 | + |
| 20 | +class AnchoredSizeBar(AnchoredOffsetbox): |
| 21 | + def __init__(self, transform, size, label, loc, |
| 22 | + pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True): |
| 23 | + """ |
| 24 | + Draw a horizontal bar with the size in data coordinate of the give axes. |
| 25 | + A label will be drawn underneath (center-alinged). |
| 26 | +
|
| 27 | + pad, borderpad in fraction of the legend font size (or prop) |
| 28 | + sep in points. |
| 29 | + """ |
| 30 | + self.size_bar = AuxTransformBox(transform) |
| 31 | + self.size_bar.add_artist(Rectangle((0,0), size, 0, fc="none")) |
| 32 | + |
| 33 | + self.txt_label = TextArea(label, minimumdescent=False) |
| 34 | + |
| 35 | + self._box = VPacker(children=[self.size_bar, self.txt_label], |
| 36 | + align="center", |
| 37 | + pad=0, sep=sep) |
| 38 | + |
| 39 | + AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, |
| 40 | + child=self._box, |
| 41 | + prop=prop, |
| 42 | + frameon=frameon) |
| 43 | + |
| 44 | + |
| 45 | +class AnchoredEllipse(AnchoredOffsetbox): |
| 46 | + def __init__(self, transform, width, height, angle, loc, |
| 47 | + pad=0.1, borderpad=0.1, prop=None, frameon=True): |
| 48 | + """ |
| 49 | + Draw an ellipse the size in data coordinate of the give axes. |
| 50 | +
|
| 51 | + pad, borderpad in fraction of the legend font size (or prop) |
| 52 | + """ |
| 53 | + self._box = AuxTransformBox(transform) |
| 54 | + self.ellipse = Ellipse((0,0), width, height, angle) |
| 55 | + self._box.add_artist(self.ellipse) |
| 56 | + |
| 57 | + AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, |
| 58 | + child=self._box, |
| 59 | + prop=prop, |
| 60 | + frameon=frameon) |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +class AnchoredDrawingArea(AnchoredOffsetbox): |
| 65 | + def __init__(self, width, height, xdescent, ydescent, |
| 66 | + loc, pad=0.4, borderpad=0.5, prop=None, frameon=True): |
| 67 | + |
| 68 | + self.da = DrawingArea(width, height, xdescent, ydescent, clip=True) |
| 69 | + |
| 70 | + super(AnchoredDrawingArea, self).__init__(loc, pad=pad, borderpad=borderpad, |
| 71 | + child=self.da, |
| 72 | + prop=None, |
| 73 | + frameon=frameon) |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + |
| 79 | + import matplotlib.pyplot as plt |
| 80 | + |
| 81 | + ax = plt.gca() |
| 82 | + ax.set_aspect(1.) |
| 83 | + |
| 84 | + at = AnchoredText("Figure 1a", |
| 85 | + loc=2, frameon=True) |
| 86 | + at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") |
| 87 | + ax.add_artist(at) |
| 88 | + |
| 89 | + from matplotlib.patches import Circle |
| 90 | + ada = AnchoredDrawingArea(20, 20, 0, 0, |
| 91 | + loc=1, pad=0., frameon=False) |
| 92 | + p = Circle((10, 10), 10) |
| 93 | + ada.da.add_artist(p) |
| 94 | + ax.add_artist(ada) |
| 95 | + |
| 96 | + # draw an ellipse of width=0.1, height=0.15 in the data coordinate |
| 97 | + ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0., |
| 98 | + loc=3, pad=0.5, borderpad=0.4, frameon=True) |
| 99 | + |
| 100 | + ax.add_artist(ae) |
| 101 | + |
| 102 | + # draw a horizontal bar with length of 0.1 in Data coordinate |
| 103 | + # (ax.transData) with a label underneath. |
| 104 | + as = AnchoredSizeBar(ax.transData, |
| 105 | + 0.1, |
| 106 | + r"1$^{\prime}$", |
| 107 | + loc=8, |
| 108 | + pad=0.1, borderpad=0.5, sep=5, |
| 109 | + frameon=False) |
| 110 | + ax.add_artist(as) |
| 111 | + |
| 112 | + plt.draw() |
| 113 | + plt.show() |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments