|
| 1 | +from typing import Sequence |
| 2 | + |
| 3 | +import matplotlib.path as mpath |
| 4 | +import matplotlib.colors as mcolors |
| 5 | +import matplotlib.lines as mlines |
| 6 | +import matplotlib.markers as mmarkers |
| 7 | +import matplotlib.transforms as mtransforms |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from .artist import Artist |
| 11 | +from .description import Desc |
| 12 | +from .conversion_edge import Edge, Graph, CoordinateEdge, DefaultEdge |
| 13 | + |
| 14 | + |
| 15 | +class Line(Artist): |
| 16 | + def __init__(self, container, edges=None, **kwargs): |
| 17 | + super().__init__(container, edges, **kwargs) |
| 18 | + |
| 19 | + colordesc = Desc((), str, "display") # ... this needs thinking... |
| 20 | + floatdesc = Desc((), float, "display") |
| 21 | + strdesc = Desc((), str, "display") |
| 22 | + |
| 23 | + self._edges += [ |
| 24 | + CoordinateEdge.from_coords("xycoords", {"x": "auto", "y": "auto"}, "data"), |
| 25 | + CoordinateEdge.from_coords("color", {"color": Desc((), str)}, "display"), |
| 26 | + CoordinateEdge.from_coords( |
| 27 | + "linewidth", {"linewidth": Desc((), np.dtype("f8"))}, "display" |
| 28 | + ), |
| 29 | + CoordinateEdge.from_coords( |
| 30 | + "linestyle", {"linestyle": Desc((), str)}, "display" |
| 31 | + ), |
| 32 | + CoordinateEdge.from_coords( |
| 33 | + "markeredgecolor", {"markeredgecolor": Desc((), str)}, "display" |
| 34 | + ), |
| 35 | + CoordinateEdge.from_coords( |
| 36 | + "markerfacecolor", {"markerfacecolor": Desc((), str)}, "display" |
| 37 | + ), |
| 38 | + CoordinateEdge.from_coords( |
| 39 | + "markersize", {"markersize": Desc((), float)}, "display" |
| 40 | + ), |
| 41 | + CoordinateEdge.from_coords( |
| 42 | + "markeredgewidth", {"markeredgewidth": Desc((), float)}, "display" |
| 43 | + ), |
| 44 | + CoordinateEdge.from_coords("marker", {"marker": Desc((), str)}, "display"), |
| 45 | + DefaultEdge.from_default_value("color_def", "color", colordesc, "C0"), |
| 46 | + DefaultEdge.from_default_value("linewidth_def", "linewidth", floatdesc, 1), |
| 47 | + DefaultEdge.from_default_value("linestyle_def", "linestyle", strdesc, "-"), |
| 48 | + DefaultEdge.from_default_value( |
| 49 | + "mec_def", "markeredgecolor", colordesc, "C0" |
| 50 | + ), |
| 51 | + DefaultEdge.from_default_value( |
| 52 | + "mfc_def", "markerfacecolor", colordesc, "C0" |
| 53 | + ), |
| 54 | + DefaultEdge.from_default_value("ms_def", "markersize", floatdesc, 6), |
| 55 | + DefaultEdge.from_default_value("mew_def", "markeredgewidth", floatdesc, 1), |
| 56 | + DefaultEdge.from_default_value("marker_def", "marker", strdesc, "None"), |
| 57 | + ] |
| 58 | + # Currently ignoring: |
| 59 | + # - cap/join style |
| 60 | + # - url |
| 61 | + # - antialiased |
| 62 | + # - snapping |
| 63 | + # - sketch |
| 64 | + # - gap color |
| 65 | + # - draw style (steps) |
| 66 | + # - fill style/alt_marker_path |
| 67 | + # - markevery |
| 68 | + # - non-str markers |
| 69 | + # Each individually pretty easy, but relatively rare features, focusing on common cases |
| 70 | + |
| 71 | + def draw(self, renderer, edges: Sequence[Edge]) -> None: |
| 72 | + g = Graph(list(edges) + self._edges) |
| 73 | + desc = Desc(("N",), np.dtype("f8"), "display") |
| 74 | + colordesc = Desc((), str, "display") # ... this needs thinking... |
| 75 | + floatdesc = Desc((), float, "display") |
| 76 | + strdesc = Desc((), str, "display") |
| 77 | + |
| 78 | + require = { |
| 79 | + "x": desc, |
| 80 | + "y": desc, |
| 81 | + "color": colordesc, |
| 82 | + "linewidth": floatdesc, |
| 83 | + "linestyle": strdesc, |
| 84 | + "markeredgecolor": colordesc, |
| 85 | + "markerfacecolor": colordesc, |
| 86 | + "markersize": floatdesc, |
| 87 | + "markeredgewidth": floatdesc, |
| 88 | + "marker": strdesc, |
| 89 | + } |
| 90 | + |
| 91 | + conv = g.evaluator(self._container.describe(), require) |
| 92 | + query, _ = self._container.query(g) |
| 93 | + x, y, color, lw, ls, *marker = conv.evaluate(query).values() |
| 94 | + mec, mfc, ms, mew, mark = marker |
| 95 | + |
| 96 | + # make the Path object |
| 97 | + path = mpath.Path(np.vstack([x, y]).T) |
| 98 | + # make an configure the graphic context |
| 99 | + gc = renderer.new_gc() |
| 100 | + gc.set_foreground(color) |
| 101 | + gc.set_linewidth(lw) |
| 102 | + gc.set_dashes(*mlines._scale_dashes(*mlines._get_dash_pattern(ls), lw)) |
| 103 | + # add the line to the render buffer |
| 104 | + renderer.draw_path(gc, path, mtransforms.IdentityTransform()) |
| 105 | + |
| 106 | + if mark != "None" and ms > 0: |
| 107 | + gc = renderer.new_gc() |
| 108 | + gc.set_linewidth(mew) |
| 109 | + gc.set_foreground(mec) |
| 110 | + marker_ = mmarkers.MarkerStyle(mark) |
| 111 | + marker_path = marker_.get_path() |
| 112 | + marker_trans = marker_.get_transform() |
| 113 | + w = renderer.points_to_pixels(ms) |
| 114 | + marker_trans = marker_trans.scale(w) |
| 115 | + mfc = mcolors.to_rgba(mfc) |
| 116 | + renderer.draw_markers( |
| 117 | + gc, |
| 118 | + marker_path, |
| 119 | + marker_trans, |
| 120 | + path, |
| 121 | + mtransforms.IdentityTransform(), |
| 122 | + mfc, |
| 123 | + ) |
0 commit comments