2626from matplotlib .tri import Triangulation
2727from PIL import Image
2828from ..bhom .analytics import bhom_analytics
29+ from ..bhom .logging import CONSOLE_LOGGER
2930
3031@bhom_analytics ()
3132def average_color (colors : Any , keep_alpha : bool = False ) -> str :
@@ -598,7 +599,16 @@ def format_polar_plot(ax: plt.Axes, yticklabels: bool = True) -> plt.Axes:
598599 if not yticklabels :
599600 ax .set_yticklabels ([])
600601
601- def process_polar_data (data :pd .DataFrame , values_column :str , directions_column :str , directions :int = 36 , value_bins :List [float ]= None ):
602+ def process_polar_data (data :pd .DataFrame , values_column :str , directions_column :str , directions :int = 36 , value_bins :List [float ]= None , density :bool = True ):
603+ """Process data for a polar plot by grouping by value and direction bins, either as value counts, or sums (determined by density)
604+
605+ """
606+ if values_column not in data .columns :
607+ raise ValueError (f"Values column `{ values_column } ` could not be found in the input dataframe." )
608+
609+ if directions_column not in data .columns :
610+ raise ValueError (f"Directions column `{ directions_column } ` could not be found in the input dataframe" )
611+
602612 if value_bins is None :
603613 value_bins = np .linspace (min (data [values_column ]), max (data [values_column ]), 11 )
604614
@@ -646,19 +656,30 @@ def process_polar_data(data:pd.DataFrame, values_column:str, directions_column:s
646656 index = dir_categories .index ,
647657 name = dir_categories .name ,
648658 )
649-
650- df = pd .concat ([dir_categories , categories ], axis = 1 )
651-
652- #remove calm?
659+
660+ df = pd .concat ([dir_categories , categories , values_ser ], axis = 1 )
661+ df .columns = [df .columns [0 ], df .columns [1 ], "Value" ]
653662
654663 # pivot dataframe
655- df = (
656- df .groupby ([df .columns [0 ], df .columns [1 ]], observed = True )
657- .value_counts ()
658- .unstack ()
659- .fillna (0 )
660- .astype (int )
661- )
664+ if density :
665+ df = (
666+ df .groupby ([df .columns [0 ], df .columns [1 ]], observed = True )
667+ .value_counts ()
668+ .unstack ()
669+ .fillna (0 )
670+ .astype (int )
671+ )
672+ else :
673+ #This allows plots like radiation roses, where the sum of the radiation from that direction is more useful than the counts
674+ df = (
675+ df .groupby ([df .columns [0 ], df .columns [1 ]], observed = True )
676+ .sum ()
677+ .unstack ()
678+ .fillna (0 )
679+ .astype (float )
680+ )
681+
682+ df = df .droplevel (level = 0 , axis = 1 )
662683
663684 for b in bin_tuples :
664685 if b not in df .columns :
@@ -671,6 +692,7 @@ def process_polar_data(data:pd.DataFrame, values_column:str, directions_column:s
671692 df .sort_index (axis = 1 , inplace = True )
672693 df = df .T
673694
674- df = df / df .values .sum () #as density plot
695+ if density :
696+ df = df / df .values .sum () #show density values as a percentage of the total number of values.
675697
676698 return df
0 commit comments