Skip to content

Commit 6479099

Browse files
committed
Update examples to use CompatibilityAxes
1 parent e5b5455 commit 6479099

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

examples/first.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@
3838
ax.add_artist(lw2, 2)
3939
ax.set_xlim(0, np.pi * 4)
4040
ax.set_ylim(-1.1, 1.1)
41+
4142
plt.show()

examples/mandelbrot.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import matplotlib.pyplot as plt
1414
import numpy as np
1515

16-
from data_prototype.wrappers import ImageWrapper
16+
from data_prototype.artist import CompatibilityAxes
17+
from data_prototype.image import Image
1718
from data_prototype.containers import FuncContainer
1819

1920
from matplotlib.colors import Normalize
@@ -36,19 +37,22 @@ def mandelbrot_set(X, Y, maxiter, *, horizon=3, power=2):
3637
fc = FuncContainer(
3738
{},
3839
xyfuncs={
39-
"xextent": ((2,), lambda x, y: [x[0], x[-1]]),
40-
"yextent": ((2,), lambda x, y: [y[0], y[-1]]),
40+
"x": ((2,), lambda x, y: [x[0], x[-1]]),
41+
"y": ((2,), lambda x, y: [y[0], y[-1]]),
4142
"image": (("N", "M"), lambda x, y: mandelbrot_set(x, y, maxiter)[1]),
4243
},
4344
)
4445
cmap = plt.get_cmap()
4546
cmap.set_under("w")
46-
im = ImageWrapper(fc, norm=Normalize(0, maxiter), cmap=cmap)
47+
im = Image(fc, norm=Normalize(0, maxiter), cmap=cmap)
4748

48-
fig, ax = plt.subplots()
49+
fig, nax = plt.subplots()
50+
ax = CompatibilityAxes(nax)
51+
nax.add_artist(ax)
4952
ax.add_artist(im)
5053
ax.set_xlim(-1, 1)
5154
ax.set_ylim(-1, 1)
52-
ax.set_aspect("equal")
53-
fig.colorbar(im)
55+
56+
nax.set_aspect("equal") # No equivalent yet
57+
5458
plt.show()

examples/units.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
"""
88

99
import numpy as np
10+
from collections import defaultdict
1011

1112
import matplotlib.pyplot as plt
1213
import matplotlib.markers as mmarkers
1314

15+
from data_prototype.artist import CompatibilityAxes
1416
from data_prototype.containers import ArrayContainer
15-
from data_prototype.conversion_node import DelayedConversionNode
17+
from data_prototype.conversion_edge import FuncEdge
1618

17-
from data_prototype.wrappers import PathCollectionWrapper
19+
from data_prototype.line import Line
1820

1921
import pint
2022

@@ -23,7 +25,11 @@
2325

2426
marker_obj = mmarkers.MarkerStyle("o")
2527

28+
29+
coords = defaultdict(lambda: "auto")
30+
coords["x"] = coords["y"] = "units"
2631
cont = ArrayContainer(
32+
coords,
2733
x=np.array([0, 1, 2]) * ureg.m,
2834
y=np.array([1, 4, 2]) * ureg.m,
2935
paths=np.array([marker_obj.get_path()]),
@@ -32,17 +38,17 @@
3238
facecolors=np.array(["C3"]),
3339
)
3440

35-
fig, ax = plt.subplots()
41+
fig, nax = plt.subplots()
42+
ax = CompatibilityAxes(nax)
43+
nax.add_artist(ax)
3644
ax.set_xlim(-0.5, 7)
3745
ax.set_ylim(0, 5)
3846

39-
# DelayedConversionNode is used to identify the keys which undergo unit transformations
40-
# The actual method which does conversions in this example is added by the
41-
# `Axis`/`Axes`, but `PathCollectionWrapper` does not natively interact with the units.
42-
xconv = DelayedConversionNode.from_keys(("x",), converter_key="xunits")
43-
yconv = DelayedConversionNode.from_keys(("y",), converter_key="yunits")
44-
lw = PathCollectionWrapper(cont, [xconv, yconv], offset_transform=ax.transData)
47+
xconv = FuncEdge.from_func("xconv", lambda x, xunits: x.to(xunits), "units", "data")
48+
yconv = FuncEdge.from_func("yconv", lambda y, yunits: y.to(yunits), "units", "data")
49+
lw = Line(cont, [xconv, yconv])
4550
ax.add_artist(lw)
46-
ax.xaxis.set_units(ureg.feet)
47-
ax.yaxis.set_units(ureg.m)
51+
nax.xaxis.set_units(ureg.feet)
52+
nax.yaxis.set_units(ureg.m)
53+
4854
plt.show()

0 commit comments

Comments
 (0)