Skip to content

Commit de25dd1

Browse files
authored
Merge pull request #5 from tacaswell/multivariate_cmap
Multivariate cmap
2 parents d2755aa + 980957b commit de25dd1

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-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)

examples/mulivariate_cmap.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 hsv_to_rgb
16+
17+
18+
def func(x, y):
19+
return (
20+
(np.sin(x).reshape(1, -1) * np.cos(y).reshape(-1, 1)) ** 2,
21+
np.arctan2(np.cos(y).reshape(-1, 1), np.sin(x).reshape(1, -1)),
22+
)
23+
24+
25+
def image_nu(image):
26+
saturation, angle = image
27+
hue = (angle + np.pi) / (2 * np.pi)
28+
value = np.ones_like(hue)
29+
return np.clip(hsv_to_rgb(np.stack([hue, saturation, value], axis=2)), 0, 1)
30+
31+
32+
fc = FuncContainer(
33+
{},
34+
xyfuncs={
35+
"xextent": ((2,), lambda x, y: [x[0], x[-1]]),
36+
"yextent": ((2,), lambda x, y: [y[0], y[-1]]),
37+
"image": (("N", "M", 2), func),
38+
},
39+
)
40+
41+
im = ImageWrapper(fc, {"image": image_nu})
42+
43+
fig, ax = plt.subplots()
44+
ax.add_artist(im)
45+
ax.set_xlim(-5, 5)
46+
ax.set_ylim(-5, 5)
47+
plt.show()

0 commit comments

Comments
 (0)