Skip to content

Commit f0fabc1

Browse files
committed
Add helpers for common artist edges
1 parent 0ebbbfd commit f0fabc1

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

data_prototype/axes.py

+26
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33

44
import matplotlib as mpl
5+
from data_prototype.artist import CompatibilityAxes
56
from matplotlib.axes._axes import Axes as MPLAxes, _preprocess_data
7+
from matplotlib.axes._base import _process_plot_var_args
68
import matplotlib.collections as mcoll
79
import matplotlib.cbook as cbook
10+
import matplotlib.lines as mlines
811
import matplotlib.markers as mmarkers
912
import matplotlib.projections as mprojections
1013

@@ -14,13 +17,20 @@
1417
FunctionConversionNode,
1518
RenameConversionNode,
1619
)
20+
from .line import Line
1721
from .wrappers import PathCollectionWrapper
1822

1923

2024
class Axes(MPLAxes):
2125
# Name for registering as a projection so we can experiment with it
2226
name = "data-prototype"
2327

28+
def __init__(self, *args, **kwargs):
29+
super().__init__(*args, **kwargs)
30+
self._ca = CompatibilityAxes(self)
31+
self._get_lines_mirror = _process_plot_var_args("mirror")
32+
self.add_artist(self._ca)
33+
2434
@_preprocess_data(
2535
replace_names=[
2636
"x",
@@ -142,6 +152,22 @@ def scatter(
142152
self._request_autoscale_view()
143153
return pcw
144154

155+
def plot(self, *args, scalex=True, scaley=True, data=None, **kwargs):
156+
kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
157+
line_args = [*self._get_lines_mirror(self, *args, data=data, **kwargs)]
158+
print(line_args)
159+
lines = []
160+
for coord, kws in line_args:
161+
cont = ArrayContainer(**{"x": coord[0], "y": coord[1]})
162+
line = Line(cont, **kws)
163+
lines.append(line)
164+
self._ca.add_artist(line)
165+
if scalex:
166+
self._request_autoscale_view("x")
167+
if scaley:
168+
self._request_autoscale_view("y")
169+
return lines
170+
145171

146172
# This is a handy trick to allow e.g. plt.subplots(subplot_kw={'projection': 'data-prototype'})
147173
mprojections.register_projection(Axes)

data_prototype/conversion_edge.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any
88
import numpy as np
99

10-
from data_prototype.description import Desc, desc_like
10+
from data_prototype.description import Desc, desc_like, ShapeSpec
1111

1212
from matplotlib.transforms import Transform
1313

@@ -112,6 +112,17 @@ def from_default_value(
112112
) -> "DefaultEdge":
113113
return cls(name, {}, {key: output}, weight, invertable=False, value=value)
114114

115+
@classmethod
116+
def from_rc(
117+
cls, rc_name: str, key: str | None = None, coordinates: str = "display"
118+
):
119+
from matplotlib import rcParams
120+
121+
if key is None:
122+
key = rc_name.split(".")[-1]
123+
scalar = Desc((), coordinates)
124+
return cls.from_default_value(f"{rc_name}_rc", key, scalar, rcParams[rc_name])
125+
115126
def evaluate(self, input: dict[str, Any]) -> dict[str, Any]:
116127
return {k: self.value for k in self.output}
117128

@@ -419,3 +430,25 @@ def __add__(self, other: Graph) -> Graph:
419430
aother = {k: v for k, v in other._aliases}
420431
aliases = tuple((aself | aother).items())
421432
return Graph(self._edges + other._edges, aliases)
433+
434+
435+
def coord_and_default(
436+
key: str,
437+
shape: ShapeSpec = (),
438+
coordinates: str = "display",
439+
default_value: Any = None,
440+
default_rc: str | None = None,
441+
):
442+
if default_rc is not None:
443+
if default_value is not None:
444+
raise ValueError(
445+
"Only one of 'default_value' and 'default_rc' may be specified"
446+
)
447+
def_edge = DefaultEdge.from_rc(default_rc, key, coordinates)
448+
else:
449+
scalar = Desc((), coordinates)
450+
def_edge = DefaultEdge.from_default_value(
451+
f"{key}_def", key, scalar, default_value
452+
)
453+
coord_edge = CoordinateEdge.from_coords(key, {key: Desc(shape)}, coordinates)
454+
return coord_edge, def_edge

data_prototype/text.py

+8-25
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,24 @@
44

55
from .artist import Artist
66
from .description import Desc
7-
from .conversion_edge import Graph, CoordinateEdge, DefaultEdge
7+
from .conversion_edge import Graph, CoordinateEdge, coord_and_default
88

99

1010
class Text(Artist):
1111
def __init__(self, container, edges=None, **kwargs):
1212
super().__init__(container, edges, **kwargs)
1313

14-
scalar = Desc((), "display")
15-
1614
edges = [
1715
CoordinateEdge.from_coords(
1816
"xycoords", {"x": Desc((), "auto"), "y": Desc((), "auto")}, "data"
1917
),
20-
CoordinateEdge.from_coords("text", {"text": Desc(())}, "display"),
21-
CoordinateEdge.from_coords("color", {"color": Desc(())}, "display"),
22-
CoordinateEdge.from_coords("alpha", {"alpha": Desc(())}, "display"),
23-
CoordinateEdge.from_coords(
24-
"fontproperties", {"fontproperties": Desc(())}, "display"
25-
),
26-
CoordinateEdge.from_coords("usetex", {"usetex": Desc(())}, "display"),
27-
CoordinateEdge.from_coords("rotation", {"rotation": Desc(())}, "display"),
28-
CoordinateEdge.from_coords(
29-
"antialiased", {"antialiased": Desc(())}, "display"
30-
),
31-
DefaultEdge.from_default_value("text_def", "text", scalar, ""),
32-
DefaultEdge.from_default_value("color_def", "color", scalar, "k"),
33-
DefaultEdge.from_default_value("alpha_def", "alpha", scalar, 1),
34-
DefaultEdge.from_default_value(
35-
"fontproperties_def", "fontproperties", scalar, FontProperties()
36-
),
37-
DefaultEdge.from_default_value("usetex_def", "usetex", scalar, False),
38-
DefaultEdge.from_default_value("rotation_def", "rotation", scalar, 0),
39-
DefaultEdge.from_default_value(
40-
"antialiased_def", "antialiased", scalar, False
41-
),
18+
*coord_and_default("text", default_value=""),
19+
*coord_and_default("color", default_rc="text.color"),
20+
*coord_and_default("alpha", default_value=1),
21+
*coord_and_default("fontproperties", default_value=FontProperties()),
22+
*coord_and_default("usetex", default_rc="text.usetex"),
23+
*coord_and_default("rotation", default_value=0),
24+
*coord_and_default("antialiased", default_rc="text.antialiased"),
4225
]
4326

4427
self._graph = self._graph + Graph(edges)

0 commit comments

Comments
 (0)