22
33import numpy as np
44import pandas as pd
5+ from matplotlib .colors import ListedColormap
56
67from gempy_viewer .modules .plot_3d .vista import GemPyToVista
78
@@ -23,25 +24,56 @@ def select_surfaces_data(data_df: pd.DataFrame, surfaces: Union[str, list[str]]
2324 return geometric_data
2425
2526
26- def set_scalar_bar (gempy_vista : GemPyToVista , elements_names : list [str ], surfaces_ids : np .ndarray ):
27+ def set_scalar_bar (gempy_vista : GemPyToVista , elements_names : list [str ],
28+ surfaces_ids : np .ndarray , custom_colors : list = None ):
2729 import pyvista as pv
28-
30+
2931 # Get mapper actor
3032 if gempy_vista .surface_points_actor is not None :
3133 mapper_actor : pv .Actor = gempy_vista .surface_points_actor
3234 elif gempy_vista .regular_grid_actor is not None :
3335 mapper_actor = gempy_vista .regular_grid_actor
3436 else :
3537 return None # * Not a good mapper for the scalar bar
36-
38+
39+ # Get the lookup table from the mapper
40+ lut = mapper_actor .mapper .lookup_table
41+
42+ # Create annotations mapping integers to element names
3743 annotations = {}
38- for e , name in enumerate (elements_names ):
39- annotations [e ] = name
44+ for e , name in enumerate (elements_names [::- 1 ]):
45+ # Convert integer to string for the annotation key
46+ annotations [str (e )] = name
47+
48+ # Apply annotations to the lookup table
49+ lut .annotations = annotations
50+
51+ # Set number of colors to match the number of categories
52+ n_colors = len (elements_names )
53+ lut .n_values = n_colors
54+
55+ # Apply custom colors if provided
56+ if custom_colors is not None :
57+ # Check if we have enough colors
58+ if len (custom_colors ) < n_colors :
59+ raise ValueError (f"Not enough custom colors provided. Got { len (custom_colors )} , need { n_colors } " )
4060
41- mapper_actor .mapper .lookup_table .annotations = annotations
42-
61+ custom_cmap = ListedColormap (custom_colors )
62+
63+ # Apply the custom colormap to the lookup table
64+ lut .apply_cmap (cmap = custom_cmap , n_values = n_colors )
65+
66+ else :
67+ # Apply a default colormap if no custom colors are provided
68+ lut .apply_cmap (cmap = 'Set1' , n_values = n_colors )
69+
70+ # Configure scalar bar arguments
4371 sargs = gempy_vista .scalar_bar_arguments
4472 sargs ["mapper" ] = mapper_actor .mapper
45-
73+
74+ # Add scalar bar
4675 gempy_vista .p .add_scalar_bar (** sargs )
47- gempy_vista .p .update_scalar_bar_range ((surfaces_ids .min (), surfaces_ids .max ())) # * This has to be here to now screw the colors with the volumes
76+
77+ # Update scalar bar range to match surface IDs range
78+ min_id , max_id = surfaces_ids .min (), surfaces_ids .max ()
79+ gempy_vista .p .update_scalar_bar_range ((min_id , max_id ))
0 commit comments