Skip to content

Commit 1fd2d18

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

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

examples/mulivariate_cmap.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
print(hue.min(), hue.max())
29+
value = np.ones_like(hue)
30+
return np.clip(hsv_to_rgb(np.stack([hue, saturation, value], axis=2)), 0, 1)
31+
32+
33+
fc = FuncContainer(
34+
{},
35+
xyfuncs={
36+
"xextent": ((2,), lambda x, y: [x[0], x[-1]]),
37+
"yextent": ((2,), lambda x, y: [y[0], y[-1]]),
38+
"image": (("N", "M", 2), func),
39+
},
40+
)
41+
42+
im = ImageWrapper(fc, {"image": image_nu})
43+
44+
fig, ax = plt.subplots()
45+
ax.add_artist(im)
46+
ax.set_xlim(-5, 5)
47+
ax.set_ylim(-5, 5)
48+
plt.show()

0 commit comments

Comments
 (0)