3
3
use ndarray:: Array ;
4
4
use plotly:: {
5
5
color:: Rgb ,
6
- common:: { ColorScale , ColorScalePalette , Font , Marker , MarkerSymbol , Mode , Title } ,
6
+ common:: { ColorBar , ColorScale , ColorScalePalette , Font , Marker , MarkerSymbol , Mode , Title } ,
7
7
layout:: { Axis , Camera , Layout , LayoutScene , Legend , Margin , ProjectionType } ,
8
8
Mesh3D , Plot , Scatter3D , Surface ,
9
9
} ;
10
+ use rand:: Rng ;
10
11
11
12
// 3D Scatter Plots
12
13
fn simple_scatter3d_plot ( ) {
@@ -41,10 +42,11 @@ fn customized_scatter3d_plot() {
41
42
. map ( |i| ( i. abs ( ) * 25f64 ) as usize )
42
43
. collect ( ) ,
43
44
)
44
- . color_scale ( ColorScale :: Palette ( ColorScalePalette :: Viridis ) ) ,
45
+ . color_scale ( ColorScale :: Palette ( ColorScalePalette :: Viridis ) )
46
+ . color_array ( z. clone ( ) ) ,
45
47
) ;
46
48
47
- let trace2 = Scatter3D :: new ( t, z, y)
49
+ let trace2 = Scatter3D :: new ( t, z. clone ( ) , y)
48
50
. name ( "Helix 2" )
49
51
. mode ( Mode :: Markers )
50
52
. marker (
@@ -55,7 +57,8 @@ fn customized_scatter3d_plot() {
55
57
. map ( |i| ( i. abs ( ) * 25f64 ) as usize )
56
58
. collect ( ) ,
57
59
)
58
- . color_scale ( ColorScale :: Palette ( ColorScalePalette :: Viridis ) ) ,
60
+ . color_scale ( ColorScale :: Palette ( ColorScalePalette :: Viridis ) )
61
+ . color_array ( z) ,
59
62
) ;
60
63
61
64
let mut plot = Plot :: new ( ) ;
@@ -161,13 +164,79 @@ fn mesh_3d_plot() {
161
164
plot. show ( ) ;
162
165
}
163
166
167
+ fn colorscale_plot ( ) {
168
+ let mut plot = Plot :: new ( ) ;
169
+
170
+ let x = ( 0 ..100 )
171
+ . map ( |x| ( ( x - 50 ) as f64 ) / 100f64 )
172
+ . collect :: < Vec < f64 > > ( ) ;
173
+
174
+ let y = x. clone ( ) ;
175
+
176
+ let iproduct = |x : & [ f64 ] , y : & [ f64 ] | -> Vec < ( f64 , f64 ) > {
177
+ let mut result = Vec :: new ( ) ;
178
+ for x in x {
179
+ for y in y {
180
+ result. push ( ( * x, * y) ) ;
181
+ }
182
+ }
183
+ result
184
+ } ;
185
+
186
+ let ( ( x, y) , z) : ( ( Vec < f64 > , Vec < f64 > ) , Vec < f64 > ) = iproduct ( & x, & y)
187
+ . into_iter ( )
188
+ . map ( |( x, y) | ( ( x, y) , -( x. powi ( 2 ) + y. powi ( 2 ) ) + 0.5 ) )
189
+ . unzip ( ) ;
190
+
191
+ let color: Vec < f32 > = z. clone ( ) . into_iter ( ) . rev ( ) . map ( |x| x as f32 ) . collect ( ) ;
192
+ let _color: Vec < usize > = ( 0 ..z. len ( ) ) . collect ( ) ;
193
+ let _color: Vec < u8 > = ( 0 ..z. len ( ) ) . map ( |x| x as u8 ) . collect ( ) ;
194
+ let _color: Vec < i16 > = {
195
+ let mut rng = rand:: thread_rng ( ) ;
196
+ ( 0 ..z. len ( ) ) . map ( |_| rng. gen_range ( 0 ..100 ) ) . collect ( )
197
+ } ;
198
+
199
+ let color_max = color. iter ( ) . fold ( f64:: MIN , |acc, x| acc. max ( * x as f64 ) ) ;
200
+
201
+ let colorscale = ColorScalePalette :: YlGnBu ;
202
+
203
+ let marker = Marker :: new ( )
204
+ . color_array ( color)
205
+ . color_scale ( plotly:: common:: ColorScale :: Palette ( colorscale. clone ( ) ) )
206
+ . cauto ( false )
207
+ . cmax ( color_max * 1.5 )
208
+ . color_bar ( ColorBar :: new ( ) ) ;
209
+
210
+ let scatter = Scatter3D :: new ( x, y, z) . mode ( Mode :: Markers ) . marker ( marker) ;
211
+
212
+ plot. add_trace ( scatter) ;
213
+
214
+ let layout = Layout :: new ( )
215
+ . font ( Font :: new ( ) . size ( 18 ) . family ( "Palatino-Linotype" ) )
216
+ . title ( format ! ( "Colorscale: {colorscale:?}" ) . as_str ( ) . into ( ) )
217
+ . width ( 1200 )
218
+ . height ( 1000 )
219
+ . scene (
220
+ LayoutScene :: new ( )
221
+ . aspect_mode ( plotly:: layout:: AspectMode :: Data )
222
+ . x_axis ( Axis :: new ( ) . tick_format ( ".1f" ) )
223
+ . y_axis ( Axis :: new ( ) . tick_format ( ".1f" ) )
224
+ . z_axis ( Axis :: new ( ) . tick_format ( ".1f" ) ) ,
225
+ ) ;
226
+
227
+ plot. set_layout ( layout) ;
228
+
229
+ plot. show ( ) ;
230
+ }
231
+
164
232
fn main ( ) {
165
233
// Uncomment any of these lines to display the example.
166
234
167
235
// Scatter3D Plots
168
236
// simple_scatter3d_plot();
169
237
// simple_line3d_plot();
170
238
// customized_scatter3d_plot();
239
+ // colorscale_plot();
171
240
172
241
// Surface Plots
173
242
// surface_plot();
0 commit comments