|
| 1 | +from matplotlib.patches import _Style, FancyArrowPatch |
| 2 | +from matplotlib.transforms import IdentityTransform |
| 3 | +from matplotlib.path import Path |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +class AxislineStyle(_Style): |
| 7 | + """ |
| 8 | + :class:`AxislineStyle` is a container class which defines style classes |
| 9 | + for AxisArtists. |
| 10 | +
|
| 11 | + An instance of any axisline style class is an callable object, |
| 12 | + whose call signature is :: |
| 13 | +
|
| 14 | + __call__(self, axis_artist, path, transform) |
| 15 | +
|
| 16 | + When called, this should return a mpl artist with following |
| 17 | + methods implemented. :: |
| 18 | +
|
| 19 | + def set_path(self, path): |
| 20 | + # set the path for axisline. |
| 21 | + |
| 22 | + def set_line_mutation_scale(self, scale): |
| 23 | + # set the scale |
| 24 | + |
| 25 | + def draw(self, renderer): |
| 26 | + # draw |
| 27 | +
|
| 28 | + |
| 29 | + """ |
| 30 | + |
| 31 | + _style_list = {} |
| 32 | + |
| 33 | + |
| 34 | + class _Base(object): |
| 35 | + # The derived classes are required to be able to be initialized |
| 36 | + # w/o arguments, i.e., all its argument (except self) must have |
| 37 | + # the default values. |
| 38 | + |
| 39 | + def __init__(self): |
| 40 | + """ |
| 41 | + initializtion. |
| 42 | + """ |
| 43 | + super(AxislineStyle._Base, self).__init__() |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + def __call__(self, axis_artist, transform): |
| 49 | + """ |
| 50 | + Given the AxisArtist instance, and transform for the path |
| 51 | + (set_path method), return the mpl artist for drawing the axis line. |
| 52 | + """ |
| 53 | + |
| 54 | + return self.new_line(axis_artist, transform) |
| 55 | + |
| 56 | + |
| 57 | + class SimpleArrow(_Base): |
| 58 | + """ |
| 59 | + A simple arrow. |
| 60 | + """ |
| 61 | + |
| 62 | + |
| 63 | + class ArrowAxisline(FancyArrowPatch): |
| 64 | + """ |
| 65 | + The artist class that will be returend for SimpleArrow style. |
| 66 | + """ |
| 67 | + def __init__(self, axis_artist, line_path, transform, |
| 68 | + line_mutation_scale): |
| 69 | + self._axis_artist = axis_artist |
| 70 | + self._line_transform = transform |
| 71 | + self._line_path = line_path |
| 72 | + self._line_mutation_scale = line_mutation_scale |
| 73 | + |
| 74 | + FancyArrowPatch.__init__(self, |
| 75 | + path=self._line_path, |
| 76 | + arrowstyle="->", |
| 77 | + arrow_transmuter=None, |
| 78 | + patchA=None, |
| 79 | + patchB=None, |
| 80 | + shrinkA=0., |
| 81 | + shrinkB=0., |
| 82 | + mutation_scale=line_mutation_scale, |
| 83 | + mutation_aspect=None, |
| 84 | + transform=IdentityTransform(), |
| 85 | + ) |
| 86 | + |
| 87 | + def set_line_mutation_scale(self, scale): |
| 88 | + self.set_mutation_scale(scale*self._line_mutation_scale) |
| 89 | + |
| 90 | + def _extend_path(self, path, mutation_size=10): |
| 91 | + """ |
| 92 | + Extend the path to make a room for drawing arrow. |
| 93 | + """ |
| 94 | + from matplotlib.bezier import get_cos_sin |
| 95 | + |
| 96 | + x0, y0 = path.vertices[-2] |
| 97 | + x1, y1 = path.vertices[-1] |
| 98 | + cost, sint = get_cos_sin(x0, y0, x1, y1) |
| 99 | + |
| 100 | + d = mutation_size * 1. |
| 101 | + x2, y2 = x1 + cost*d, y1+sint*d |
| 102 | + |
| 103 | + if path.codes is None: |
| 104 | + _path = Path(np.concatenate([path.vertices, [[x2, y2]]])) |
| 105 | + else: |
| 106 | + _path = Path(np.concatenate([path.vertices, [[x2, y2]]]), |
| 107 | + np.concatenate([path.codes, [Path.LINETO]])) |
| 108 | + |
| 109 | + return _path |
| 110 | + |
| 111 | + def set_path(self, path): |
| 112 | + self._line_path = path |
| 113 | + |
| 114 | + def draw(self, renderer): |
| 115 | + """ |
| 116 | + Draw the axis line. |
| 117 | + 1) transform the path to the display cooridnate. |
| 118 | + 2) extend the path to make a room for arrow |
| 119 | + 3) update the path of the FancyArrowPatch. |
| 120 | + 4) draw |
| 121 | + """ |
| 122 | + path_in_disp = self._line_transform.transform_path(self._line_path) |
| 123 | + mutation_size = self.get_mutation_scale() #line_mutation_scale() |
| 124 | + extented_path = self._extend_path(path_in_disp, |
| 125 | + mutation_size=mutation_size) |
| 126 | + |
| 127 | + self._path_original = extented_path |
| 128 | + FancyArrowPatch.draw(self, renderer) |
| 129 | + |
| 130 | + |
| 131 | + def __init__(self, size=1): |
| 132 | + """ |
| 133 | + *size* |
| 134 | + size of the arrow as a fraction of the ticklabel size. |
| 135 | + """ |
| 136 | + |
| 137 | + self.size = size |
| 138 | + super(AxislineStyle.SimpleArrow, self).__init__() |
| 139 | + |
| 140 | + def new_line(self, axis_artist, transform): |
| 141 | + |
| 142 | + linepath = Path([(0,0), (0, 1)]) |
| 143 | + axisline = self.ArrowAxisline(axis_artist, linepath, transform, |
| 144 | + line_mutation_scale=self.size) |
| 145 | + return axisline |
| 146 | + |
| 147 | + |
| 148 | + _style_list["->"] = SimpleArrow |
0 commit comments