Skip to content

Commit 0dc2c6c

Browse files
committed
ENH: add an example of a multi-variate color mapping
1 parent 9f2bb6f commit 0dc2c6c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

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)