33use ndarray:: Array ;
44use plotly:: {
55 color:: Rgb ,
6- common:: { ColorScale , ColorScalePalette , Font , Marker , MarkerSymbol , Mode , Title } ,
6+ common:: { ColorBar , ColorScale , ColorScalePalette , Font , Marker , MarkerSymbol , Mode , Title } ,
77 layout:: { Axis , Camera , Layout , LayoutScene , Legend , Margin , ProjectionType } ,
88 Mesh3D , Plot , Scatter3D , Surface ,
99} ;
10+ use rand:: Rng ;
1011
1112// 3D Scatter Plots
1213fn simple_scatter3d_plot ( ) {
@@ -41,10 +42,11 @@ fn customized_scatter3d_plot() {
4142 . map ( |i| ( i. abs ( ) * 25f64 ) as usize )
4243 . collect ( ) ,
4344 )
44- . color_scale ( ColorScale :: Palette ( ColorScalePalette :: Viridis ) ) ,
45+ . color_scale ( ColorScale :: Palette ( ColorScalePalette :: Viridis ) )
46+ . color_array ( z. clone ( ) ) ,
4547 ) ;
4648
47- let trace2 = Scatter3D :: new ( t, z, y)
49+ let trace2 = Scatter3D :: new ( t, z. clone ( ) , y)
4850 . name ( "Helix 2" )
4951 . mode ( Mode :: Markers )
5052 . marker (
@@ -55,7 +57,8 @@ fn customized_scatter3d_plot() {
5557 . map ( |i| ( i. abs ( ) * 25f64 ) as usize )
5658 . collect ( ) ,
5759 )
58- . color_scale ( ColorScale :: Palette ( ColorScalePalette :: Viridis ) ) ,
60+ . color_scale ( ColorScale :: Palette ( ColorScalePalette :: Viridis ) )
61+ . color_array ( z) ,
5962 ) ;
6063
6164 let mut plot = Plot :: new ( ) ;
@@ -161,13 +164,79 @@ fn mesh_3d_plot() {
161164 plot. show ( ) ;
162165}
163166
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+
164232fn main ( ) {
165233 // Uncomment any of these lines to display the example.
166234
167235 // Scatter3D Plots
168236 // simple_scatter3d_plot();
169237 // simple_line3d_plot();
170238 // customized_scatter3d_plot();
239+ // colorscale_plot();
171240
172241 // Surface Plots
173242 // surface_plot();
0 commit comments