|
| 1 | + |
| 2 | +from matplotlib.patches import Rectangle, Ellipse |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from matplotlib.offsetbox import AnchoredOffsetbox, AuxTransformBox, VPacker,\ |
| 7 | + TextArea, AnchoredText, DrawingArea, AnnotationBbox |
| 8 | + |
| 9 | + |
| 10 | +class AnchoredDrawingArea(AnchoredOffsetbox): |
| 11 | + """ |
| 12 | + AnchoredOffsetbox with DrawingArea |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, width, height, xdescent, ydescent, |
| 16 | + loc, pad=0.4, borderpad=0.5, prop=None, frameon=True, |
| 17 | + **kwargs): |
| 18 | + """ |
| 19 | + *width*, *height*, *xdescent*, *ydescent* : the dimensions of the DrawingArea. |
| 20 | + *prop* : font property. this is only used for scaling the paddings. |
| 21 | + """ |
| 22 | + |
| 23 | + self.da = DrawingArea(width, height, xdescent, ydescent, clip=True) |
| 24 | + self.drawing_area = self.da |
| 25 | + |
| 26 | + super(AnchoredDrawingArea, self).__init__(loc, pad=pad, borderpad=borderpad, |
| 27 | + child=self.da, |
| 28 | + prop=None, |
| 29 | + frameon=frameon, |
| 30 | + **kwargs) |
| 31 | + |
| 32 | + |
| 33 | +class AnchoredAuxTransformBox(AnchoredOffsetbox): |
| 34 | + def __init__(self, transform, loc, |
| 35 | + pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs): |
| 36 | + |
| 37 | + self.drawing_area = AuxTransformBox(transform) |
| 38 | + |
| 39 | + AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, |
| 40 | + child=self.drawing_area, |
| 41 | + prop=prop, |
| 42 | + frameon=frameon, |
| 43 | + **kwargs) |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +class AnchoredEllipse(AnchoredOffsetbox): |
| 48 | + def __init__(self, transform, width, height, angle, loc, |
| 49 | + pad=0.1, borderpad=0.1, prop=None, frameon=True, **kwargs): |
| 50 | + """ |
| 51 | + Draw an ellipse the size in data coordinate of the give axes. |
| 52 | +
|
| 53 | + pad, borderpad in fraction of the legend font size (or prop) |
| 54 | + """ |
| 55 | + self._box = AuxTransformBox(transform) |
| 56 | + self.ellipse = Ellipse((0,0), width, height, angle) |
| 57 | + self._box.add_artist(self.ellipse) |
| 58 | + |
| 59 | + AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, |
| 60 | + child=self._box, |
| 61 | + prop=prop, |
| 62 | + frameon=frameon, **kwargs) |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +class AnchoredSizeBar(AnchoredOffsetbox): |
| 67 | + def __init__(self, transform, size, label, loc, |
| 68 | + pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True, |
| 69 | + **kwargs): |
| 70 | + """ |
| 71 | + Draw a horizontal bar with the size in data coordinate of the give axes. |
| 72 | + A label will be drawn underneath (center-alinged). |
| 73 | +
|
| 74 | + pad, borderpad in fraction of the legend font size (or prop) |
| 75 | + sep in points. |
| 76 | + """ |
| 77 | + self.size_bar = AuxTransformBox(transform) |
| 78 | + self.size_bar.add_artist(Rectangle((0,0), size, 0, fc="none")) |
| 79 | + |
| 80 | + self.txt_label = TextArea(label, minimumdescent=False) |
| 81 | + |
| 82 | + self._box = VPacker(children=[self.size_bar, self.txt_label], |
| 83 | + align="center", |
| 84 | + pad=0, sep=sep) |
| 85 | + |
| 86 | + AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, |
| 87 | + child=self._box, |
| 88 | + prop=prop, |
| 89 | + frameon=frameon, **kwargs) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + |
| 94 | + import matplotlib.pyplot as plt |
| 95 | + |
| 96 | + fig = plt.gcf() |
| 97 | + fig.clf() |
| 98 | + ax = plt.subplot(111) |
| 99 | + |
| 100 | + offsetbox = AnchoredText("Test", loc=6, pad=0.3, |
| 101 | + borderpad=0.3, prop=None) |
| 102 | + xy = (0.5, 0.5) |
| 103 | + ax.plot([0.5], [0.5], "xk") |
| 104 | + ab = AnnotationBbox(offsetbox, xy, |
| 105 | + xybox=(1., .5), |
| 106 | + xycoords='data', |
| 107 | + boxcoords=("axes fraction", "data"), |
| 108 | + arrowprops=dict(arrowstyle="->")) |
| 109 | + #arrowprops=None) |
| 110 | + |
| 111 | + ax.add_artist(ab) |
| 112 | + |
| 113 | + |
| 114 | + from matplotlib.patches import Circle |
| 115 | + ada = AnchoredDrawingArea(20, 20, 0, 0, |
| 116 | + loc=6, pad=0.1, borderpad=0.3, frameon=True) |
| 117 | + p = Circle((10, 10), 10) |
| 118 | + ada.da.add_artist(p) |
| 119 | + |
| 120 | + ab = AnnotationBbox(ada, (0.3, 0.4), |
| 121 | + xybox=(1., 0.4), |
| 122 | + xycoords='data', |
| 123 | + boxcoords=("axes fraction", "data"), |
| 124 | + arrowprops=dict(arrowstyle="->")) |
| 125 | + #arrowprops=None) |
| 126 | + |
| 127 | + ax.add_artist(ab) |
| 128 | + |
| 129 | + |
| 130 | + arr = np.arange(100).reshape((10,10)) |
| 131 | + im = AnchoredImage(arr, |
| 132 | + loc=4, |
| 133 | + pad=0.5, borderpad=0.2, prop=None, frameon=True, |
| 134 | + zoom=1, |
| 135 | + cmap = None, |
| 136 | + norm = None, |
| 137 | + interpolation=None, |
| 138 | + origin=None, |
| 139 | + extent=None, |
| 140 | + filternorm=1, |
| 141 | + filterrad=4.0, |
| 142 | + resample = False, |
| 143 | + ) |
| 144 | + |
| 145 | + ab = AnnotationBbox(im, (0.5, 0.5), |
| 146 | + xybox=(-10., 10.), |
| 147 | + xycoords='data', |
| 148 | + boxcoords="offset points", |
| 149 | + arrowprops=dict(arrowstyle="->")) |
| 150 | + #arrowprops=None) |
| 151 | + |
| 152 | + ax.add_artist(ab) |
| 153 | + |
| 154 | + ax.set_xlim(0, 1) |
| 155 | + ax.set_ylim(0, 1) |
| 156 | + |
| 157 | + |
| 158 | + plt.draw() |
| 159 | + plt.show() |
| 160 | + |
0 commit comments