Skip to content

Commit 3a38616

Browse files
committed
ENH: add a working 2D example wrapping AxesImage
1 parent 354e590 commit 3a38616

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

data_prototype/wrappers.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from matplotlib.lines import Line2D as _Line2D
2+
from matplotlib.image import AxesImage as _AxesImage
23

34

45
class ProxyWrapper:
@@ -50,3 +51,18 @@ def draw(self, renderer):
5051
data = self._query_and_transform(renderer)
5152
self._wrapped_instance.set_data(data["x"], data["y"])
5253
return self._wrapped_instance.draw(renderer)
54+
55+
56+
class ImageWrapper(ProxyWrapper):
57+
_wrapped_class = _AxesImage
58+
59+
def __init__(self, data, nus=None, /, **kwargs):
60+
super().__init__(data, nus)
61+
kwargs.setdefault("origin", "lower")
62+
self._wrapped_instance = self._wrapped_class(None, **kwargs)
63+
64+
def draw(self, renderer):
65+
data = self._query_and_transform(renderer)
66+
self._wrapped_instance.set_array(data["image"])
67+
self._wrapped_instance.set_extent(data["extent"])
68+
return self._wrapped_instance.draw(renderer)

examples/2Dfunc.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
=====================
3+
A functional 2D image
4+
=====================
5+
6+
7+
"""
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
12+
from data_prototype.wrappers import ImageWrapper
13+
from data_prototype.containers import FuncContainer
14+
15+
from matplotlib.colors import Normalize
16+
17+
fc = FuncContainer(
18+
{},
19+
xyfuncs={
20+
"extent": lambda x, y: [x[0], x[-1], y[0], y[-1]],
21+
"image": lambda x, y: np.sin(x).reshape(1, -1) * np.cos(y).reshape(-1, 1),
22+
},
23+
)
24+
im = ImageWrapper(fc, norm=Normalize(-1, 1))
25+
26+
fig, ax = plt.subplots()
27+
ax.add_artist(im)
28+
ax.set_xlim(-5, 5)
29+
ax.set_ylim(-5, 5)
30+
fig.colorbar(im)

0 commit comments

Comments
 (0)