@@ -3004,8 +3004,12 @@ def grouped_bar(self, x, heights, dataset_labels=None):
3004
3004
"""
3005
3005
Parameters
3006
3006
-----------
3007
- x : array-like of str
3008
- The labels.
3007
+ x : array-like or list of str
3008
+ The center positions of the bar groups. If these are numeric values,
3009
+ they have to be equidistant. As with `~.Axes.bar`, you can provide
3010
+ categorical labels, which will be used at integer numeric positions
3011
+ ``range(x)``.
3012
+
3009
3013
heights : list of array-like or dict of array-like or 2D array
3010
3014
The heights for all x and groups. One of:
3011
3015
@@ -3064,27 +3068,40 @@ def grouped_bar(self, x, heights, dataset_labels=None):
3064
3068
elif hasattr (heights , 'shape' ):
3065
3069
heights = heights .T
3066
3070
3067
- num_labels = len (x )
3071
+ num_groups = len (x )
3068
3072
num_datasets = len (heights )
3069
3073
3070
- for dataset in heights :
3071
- assert len (dataset ) == num_labels
3074
+ if isinstance (x [0 ], str ):
3075
+ tick_labels = x
3076
+ group_centers = np .arange (num_groups )
3077
+ else :
3078
+ if num_groups > 1 :
3079
+ d = np .diff (x )
3080
+ if not np .allclose (d , d .mean ()):
3081
+ raise ValueError ("'x' must be equidistant" )
3082
+ group_centers = np .asarray (x )
3083
+ tick_labels = None
3084
+
3085
+ for i , dataset in enumerate (heights ):
3086
+ if len (dataset ) != num_groups :
3087
+ raise ValueError (
3088
+ f"'x' indicates { num_groups } groups, but dataset { i } "
3089
+ f"has { len (dataset )} groups"
3090
+ )
3072
3091
3073
3092
margin = 0.1
3074
3093
bar_width = (1 - 2 * margin ) / num_datasets
3075
- block_centers = np .arange (num_labels )
3076
3094
3077
3095
if dataset_labels is None :
3078
3096
dataset_labels = [None ] * num_datasets
3079
3097
else :
3080
3098
assert len (dataset_labels ) == num_datasets
3081
3099
3082
3100
for i , (hs , dataset_label ) in enumerate (zip (heights , dataset_labels )):
3083
- lefts = block_centers - 0.5 + margin + i * bar_width
3084
- print (i , x , lefts , hs , dataset_label )
3101
+ lefts = group_centers - 0.5 + margin + i * bar_width
3085
3102
self .bar (lefts , hs , width = bar_width , align = "edge" , label = dataset_label )
3086
3103
3087
- self .xaxis .set_ticks (block_centers , labels = x )
3104
+ self .xaxis .set_ticks (group_centers , labels = tick_labels )
3088
3105
3089
3106
# TODO: does not return anything for now
3090
3107
0 commit comments