Skip to content

Commit 9f2bb6f

Browse files
committed
ENH: use nu mechanism to do color mapping
1 parent eafb128 commit 9f2bb6f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

data_prototype/wrappers.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from cachetools import LFUCache
66
from functools import partial, wraps
77

8+
import matplotlib as mpl
89
from matplotlib.lines import Line2D as _Line2D
910
from matplotlib.image import AxesImage as _AxesImage
1011
from matplotlib.patches import StepPatch as _StepPatch
@@ -192,7 +193,17 @@ def _update_wrapped(self, data):
192193
class ImageWrapper(ProxyWrapper):
193194
_wrapped_class = _AxesImage
194195

195-
def __init__(self, data: DataContainer, nus=None, /, **kwargs):
196+
def __init__(self, data: DataContainer, nus=None, /, cmap=None, norm=None, **kwargs):
197+
print(kwargs, nus)
198+
nus = dict(nus or {})
199+
if cmap is not None or norm is not None:
200+
if nus is not None and 'image' in nus:
201+
raise ValueError("Conflicting input")
202+
if cmap is None:
203+
cmap = mpl.colormaps['viridis']
204+
if norm is None:
205+
raise ValueError("not sure how to do autoscaling yet")
206+
nus['image'] = lambda image: cmap(norm(image))
196207
super().__init__(data, nus)
197208
kwargs.setdefault("origin", "lower")
198209
self._wrapped_instance = self._wrapped_class(None, **kwargs)

examples/2Dfunc.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
from data_prototype.wrappers import ImageWrapper
1313
from data_prototype.containers import FuncContainer
1414

15+
import matplotlib as mpl
1516
from matplotlib.colors import Normalize
1617

18+
1719
fc = FuncContainer(
1820
{},
1921
xyfuncs={
@@ -22,7 +24,9 @@
2224
"image": (("N", "M"), lambda x, y: np.sin(x).reshape(1, -1) * np.cos(y).reshape(-1, 1)),
2325
},
2426
)
25-
im = ImageWrapper(fc, norm=Normalize(-1, 1))
27+
cmap = mpl.colormaps["viridis"]
28+
norm = Normalize(-1, 1)
29+
im = ImageWrapper(fc, {"image": lambda image: cmap(norm(image))})
2630

2731
fig, ax = plt.subplots()
2832
ax.add_artist(im)

0 commit comments

Comments
 (0)