|
| 1 | +from typing import Sequence |
| 2 | + |
| 3 | + |
| 4 | +import matplotlib.path as mpath |
| 5 | +import matplotlib.colors as mcolors |
| 6 | +import matplotlib.lines as mlines |
| 7 | +import matplotlib.path as mpath |
| 8 | +import matplotlib.transforms as mtransforms |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from .containers import DataContainer, ArrayContainer, DataUnion |
| 12 | +from .description import Desc, desc_like |
| 13 | +from .conversion_edge import Edge, TransformEdge, FuncEdge, Graph |
| 14 | + |
| 15 | + |
| 16 | +class Artist: |
| 17 | + required_keys: dict[str, Desc] |
| 18 | + |
| 19 | + # defaults? |
| 20 | + def __init__( |
| 21 | + self, container: DataContainer, edges: Sequence[Edge] | None = None, **kwargs |
| 22 | + ): |
| 23 | + kwargs_cont = ArrayContainer(**kwargs) |
| 24 | + self._container = DataUnion(container, kwargs_cont) |
| 25 | + |
| 26 | + edges = edges or [] |
| 27 | + self._edges = list(edges) |
| 28 | + |
| 29 | + def draw(self, renderer, edges: Sequence[Edge]) -> None: ... |
| 30 | + |
| 31 | + |
| 32 | +class CompatibilityArtist: |
| 33 | + """A compatibility shim to ducktype as a classic Matplotlib Artist. |
| 34 | +
|
| 35 | + At this time features are implemented on an "as needed" basis, and many |
| 36 | + are only implemented insofar as they do not fail, not necessarily providing |
| 37 | + full functionality of a full MPL Artist. |
| 38 | +
|
| 39 | + The idea is to keep the new Artist class as minimal as possible. |
| 40 | + As features are added this may shrink. |
| 41 | +
|
| 42 | + The main thing we are trying to avoid is the reliance on the axes/figure |
| 43 | +
|
| 44 | + Ultimately for useability, whatever remains shimmed out here may be rolled in as |
| 45 | + some form of gaurded option to ``Artist`` itself, but a firm dividing line is |
| 46 | + useful for avoiding accidental dependency. |
| 47 | + """ |
| 48 | + |
| 49 | + def __init__(self, artist: Artist): |
| 50 | + self._artist = artist |
| 51 | + |
| 52 | + self.axes = None |
| 53 | + self.figure = None |
| 54 | + self._clippath = None |
| 55 | + self.zorder = 2 |
| 56 | + |
| 57 | + def set_figure(self, fig): |
| 58 | + self.figure = fig |
| 59 | + |
| 60 | + def is_transform_set(self): |
| 61 | + return True |
| 62 | + |
| 63 | + def get_mouseover(self): |
| 64 | + return False |
| 65 | + |
| 66 | + def get_clip_path(self): |
| 67 | + self._clippath |
| 68 | + |
| 69 | + def set_clip_path(self, path): |
| 70 | + self._clippath = path |
| 71 | + |
| 72 | + def get_animated(self): |
| 73 | + return False |
| 74 | + |
| 75 | + def draw(self, renderer, edges=None): |
| 76 | + |
| 77 | + if edges is None: |
| 78 | + edges = [] |
| 79 | + |
| 80 | + if self.axes is not None: |
| 81 | + desc: Desc = Desc(("N",), np.dtype("f8"), coordinates="data") |
| 82 | + xy: dict[str, Desc] = {"x": desc, "y": desc} |
| 83 | + edges.append( |
| 84 | + TransformEdge( |
| 85 | + "data", |
| 86 | + xy, |
| 87 | + desc_like(xy, coordinates="axes"), |
| 88 | + transform=self.axes.transData - self.axes.transAxes, |
| 89 | + ) |
| 90 | + ) |
| 91 | + edges.append( |
| 92 | + TransformEdge( |
| 93 | + "axes", |
| 94 | + desc_like(xy, coordinates="axes"), |
| 95 | + desc_like(xy, coordinates="display"), |
| 96 | + transform=self.axes.transAxes, |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + self._artist.draw(renderer, edges) |
| 101 | + |
| 102 | + |
| 103 | +class Line(Artist): |
| 104 | + def __init__(self, container, edges=None, **kwargs): |
| 105 | + super().__init__(container, edges, **kwargs) |
| 106 | + |
| 107 | + defaults = ArrayContainer( |
| 108 | + **{ |
| 109 | + "color": "C0", # TODO: interactions with cycler/rcparams? |
| 110 | + "linewidth": 1, |
| 111 | + "linestyle": "-", |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + self._container = DataUnion(defaults, self._container) |
| 116 | + # These are a stand-in for units etc... just kind of placed here as no-ops |
| 117 | + self._edges += [ |
| 118 | + FuncEdge.from_func( |
| 119 | + "xvals", lambda x: x, "naive", "data", inverse=lambda x: x |
| 120 | + ), |
| 121 | + FuncEdge.from_func( |
| 122 | + "yvals", lambda y: y, "naive", "data", inverse=lambda y: y |
| 123 | + ), |
| 124 | + ] |
| 125 | + |
| 126 | + def draw(self, renderer, edges: Sequence[Edge]) -> None: |
| 127 | + g = Graph(list(edges) + self._edges) |
| 128 | + desc = Desc(("N",), np.dtype("f8"), "display") |
| 129 | + xy = {"x": desc, "y": desc} |
| 130 | + conv = g.evaluator(self._container.describe(), xy) |
| 131 | + query, _ = self._container.query(g) |
| 132 | + x, y = conv.evaluate(query).values() |
| 133 | + |
| 134 | + # make the Path object |
| 135 | + path = mpath.Path(np.vstack([x, y]).T) |
| 136 | + # make an configure the graphic context |
| 137 | + gc = renderer.new_gc() |
| 138 | + gc.set_foreground(mcolors.to_rgba(query["color"]), isRGBA=True) |
| 139 | + gc.set_linewidth(query["linewidth"]) |
| 140 | + gc.set_dashes(*mlines._get_dash_pattern(query["linestyle"])) |
| 141 | + # add the line to the render buffer |
| 142 | + renderer.draw_path(gc, path, mtransforms.IdentityTransform()) |
| 143 | + |
| 144 | + |
| 145 | +class Image(Artist): |
| 146 | + def __init__(self, container, edges=None, **kwargs): |
| 147 | + super().__init__(container, edges, **kwargs) |
| 148 | + |
| 149 | + defaults = ArrayContainer( |
| 150 | + **{ |
| 151 | + "cmap": "viridis", |
| 152 | + "norm": "linear", |
| 153 | + } |
| 154 | + ) |
| 155 | + |
| 156 | + self._container = DataUnion(defaults, self._container) |
| 157 | + # These are a stand-in for units etc... just kind of placed here as no-ops |
| 158 | + self._edges += [ |
| 159 | + FuncEdge.from_func( |
| 160 | + "xvals", lambda x: x, "naive", "data", inverse=lambda x: x |
| 161 | + ), |
| 162 | + FuncEdge.from_func( |
| 163 | + "yvals", lambda y: y, "naive", "data", inverse=lambda y: y |
| 164 | + ), |
| 165 | + ] |
| 166 | + |
| 167 | + def draw(self, renderer, edges: Sequence[Edge]) -> None: |
| 168 | + g = Graph(list(edges) + self._edges) |
| 169 | + ... |
0 commit comments