Skip to content

Commit ed48e77

Browse files
Change color_scale into a private attribute, listen to its change in in Velocity.js, change default colormap to OrRd_06 to fit other default values in ipyleaflet, update docstrings.
1 parent d65324d commit ed48e77

File tree

3 files changed

+23
-50
lines changed

3 files changed

+23
-50
lines changed

examples/Velocity_with_colormap.ipynb

+3-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"# Set up for JupyterLite\n",
2121
"try:\n",
2222
" import piplite\n",
23-
" await piplite.install('ipyleaflet')\n",
23+
" await piplite.install(['ipyleaflet')\n",
2424
"except ImportError:\n",
2525
" pass"
2626
]
@@ -80,15 +80,6 @@
8080
"ds = xr.open_dataset(\"wind-global.nc\")"
8181
]
8282
},
83-
{
84-
"cell_type": "code",
85-
"execution_count": null,
86-
"metadata": {},
87-
"outputs": [],
88-
"source": [
89-
"colormap = linear.viridis"
90-
]
91-
},
9283
{
9384
"cell_type": "code",
9485
"execution_count": null,
@@ -108,7 +99,7 @@
10899
" longitude_dimension=\"lon\",\n",
109100
" velocity_scale=0.01,\n",
110101
" max_velocity=20,\n",
111-
" colormap = colormap,\n",
102+
" colormap = linear.viridis,\n",
112103
" display_options=display_options,\n",
113104
")"
114105
]
@@ -121,7 +112,7 @@
121112
"source": [
122113
"colormap_control = ipyleaflet.ColormapControl(\n",
123114
" caption='Unemployment rate',\n",
124-
" colormap=colormap,\n",
115+
" colormap=wind.colormap,\n",
125116
" value_min=wind.min_velocity,\n",
126117
" value_max=wind.max_velocity,\n",
127118
" position='topright',\n",

ipyleaflet/velocity.py

+11-37
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ class Velocity(Layer):
3838
Used to align color scale.
3939
velocity_scale: float, 0.005
4040
To be modified for particle animations.
41-
color_scale: array, default []
42-
Array of hex/rgb colors for user-specified color scale.
41+
branca.colormap.LinearColorMap instance, default linear.OrRd_06
42+
The colormap used for displaying the Velocity data
43+
_color_scale: array, default []
44+
Array of hex/rgb colors for user-specified color scale, it is private and defined from the colormap.
45+
4346
"""
4447

4548
_view_name = Unicode('LeafletVelocityView').tag(sync=True)
@@ -67,45 +70,16 @@ class Velocity(Layer):
6770
min_velocity = Float(0).tag(sync=True, o=True)
6871
max_velocity = Float(10).tag(sync=True, o=True)
6972
velocity_scale = Float(0.005).tag(sync=True, o=True)
70-
colormap = Any(linear.YlOrRd_04)
71-
#color_scale = List([]).tag(sync=True, o=True)
72-
colors = [
73-
"rgb(36,104, 180)",
74-
"rgb(60,157, 194)",
75-
"rgb(128,205,193)",
76-
"rgb(151,218,168)",
77-
"rgb(198,231,181)",
78-
"rgb(238,247,217)",
79-
"rgb(255,238,159)",
80-
"rgb(252,217,125)",
81-
"rgb(255,182,100)",
82-
"rgb(252,150,75)",
83-
"rgb(250,112,52)",
84-
"rgb(245,64,32)",
85-
"rgb(237,45,28)",
86-
"rgb(220,24,32)",
87-
"rgb(180,0,35)"
88-
]
89-
color_scale = List(colors).tag(sync=True, o=True)
73+
colormap = Any(linear.OrRd_06)
74+
_color_scale = List([]).tag(sync=True, o=True)
9075

91-
@default('color_scale')
76+
@default('_color_scale')
9277
def _default_color_scale(self):
93-
self.color_scale = []
94-
95-
for color in self.colormap.colors:
96-
rgb_tuple = tuple(int(x * 256) for x in color[:3])
97-
rgb_str = f"rgb{rgb_tuple}"
98-
self.color_scale.append(rgb_str)
99-
100-
return self.color_scale
101-
102-
@default('colormap')
103-
def _default_colormap(self):
104-
78+
self._color_scale = []
10579

10680
for color in self.colormap.colors:
10781
rgb_tuple = tuple(int(x * 256) for x in color[:3])
10882
rgb_str = f"rgb{rgb_tuple}"
109-
self.color_scale.append(rgb_str)
83+
self._color_scale.append(rgb_str)
11084

111-
return self.color_scale
85+
return self._color_scale

js/src/layers/Velocity.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class LeafletVelocityModel extends layer.LeafletLayerModel {
2525
minVelocity: 0,
2626
maxVelocity: 10,
2727
velocityScale: 0.005,
28-
colorScale: []
28+
_colorScale: []
2929
};
3030
}
3131
}
@@ -60,5 +60,13 @@ export class LeafletVelocityView extends layer.LeafletLayerView {
6060
},
6161
this
6262
);
63+
this.listenTo(
64+
this.model,
65+
'change:_colorScale',
66+
function () {
67+
this.layer_views.update(this.model.get('_colorScale'));
68+
},
69+
this
70+
);
6371
}
6472
}

0 commit comments

Comments
 (0)