Skip to content

Commit ab2a630

Browse files
committed
Add CompatibilityAxes (as opposed to compatibilityArtist)
1 parent 3e7a0ec commit ab2a630

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

data_prototype/artist.py

+80
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,83 @@ def draw(self, renderer, edges=None):
9191
)
9292

9393
self._artist.draw(renderer, edges)
94+
95+
96+
class CompatibilityAxes:
97+
"""A compatibility shim to add to traditional matplotlib axes.
98+
99+
At this time features are implemented on an "as needed" basis, and many
100+
are only implemented insofar as they do not fail, not necessarily providing
101+
full functionality of a full MPL Artist.
102+
103+
The idea is to keep the new Artist class as minimal as possible.
104+
As features are added this may shrink.
105+
106+
The main thing we are trying to avoid is the reliance on the axes/figure
107+
108+
Ultimately for useability, whatever remains shimmed out here may be rolled in as
109+
some form of gaurded option to ``Artist`` itself, but a firm dividing line is
110+
useful for avoiding accidental dependency.
111+
"""
112+
113+
def __init__(self, axes):
114+
self.axes = axes
115+
self.figure = None
116+
self._clippath = None
117+
self.zorder = 2
118+
self._children = []
119+
120+
def set_figure(self, fig):
121+
self.figure = fig
122+
123+
def is_transform_set(self):
124+
return True
125+
126+
def get_mouseover(self):
127+
return False
128+
129+
def get_clip_path(self):
130+
self._clippath
131+
132+
def set_clip_path(self, path):
133+
self._clippath = path
134+
135+
def get_animated(self):
136+
return False
137+
138+
def draw(self, renderer, edges=None):
139+
if edges is None:
140+
edges = []
141+
142+
if self.axes is not None:
143+
desc: Desc = Desc(("N",), coordinates="data")
144+
xy: dict[str, Desc] = {"x": desc, "y": desc}
145+
edges.append(
146+
TransformEdge(
147+
"data",
148+
xy,
149+
desc_like(xy, coordinates="axes"),
150+
transform=self.axes.transData - self.axes.transAxes,
151+
)
152+
)
153+
edges.append(
154+
TransformEdge(
155+
"axes",
156+
desc_like(xy, coordinates="axes"),
157+
desc_like(xy, coordinates="display"),
158+
transform=self.axes.transAxes,
159+
)
160+
)
161+
162+
# TODO independent zorder
163+
for c in self._children:
164+
c.draw(renderer, edges)
165+
166+
def add_artist(self, artist):
167+
self._children.append(artist)
168+
169+
def set_xlim(self, min_=None, max_=None):
170+
self.axes.set_xlim(min_, max_)
171+
172+
def set_ylim(self, min_=None, max_=None):
173+
self.axes.set_ylim(min_, max_)

examples/first.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import numpy as np
1212
import pandas as pd
1313

14-
from data_prototype.artist import CompatibilityArtist
14+
from data_prototype.artist import CompatibilityAxes
1515
from data_prototype.line import Line
1616
from data_prototype.containers import FuncContainer, SeriesContainer
1717

@@ -30,9 +30,11 @@
3030
markersize=12,
3131
)
3232

33-
fig, ax = plt.subplots()
34-
ax.add_artist(CompatibilityArtist(lw))
35-
ax.add_artist(CompatibilityArtist(lw2))
33+
fig, nax = plt.subplots()
34+
ax = CompatibilityAxes(nax)
35+
nax.add_artist(ax)
36+
ax.add_artist(lw)
37+
ax.add_artist(lw2)
3638
ax.set_xlim(0, np.pi * 4)
3739
ax.set_ylim(-1.1, 1.1)
3840

0 commit comments

Comments
 (0)