@@ -3017,8 +3017,12 @@ def grouped_bar(self, x, heights, dataset_labels=None):
3017
3017
"""
3018
3018
Parameters
3019
3019
-----------
3020
- x : array-like of str
3021
- The labels.
3020
+ x : array-like or list of str
3021
+ The center positions of the bar groups. If these are numeric values,
3022
+ they have to be equidistant. As with `~.Axes.bar`, you can provide
3023
+ categorical labels, which will be used at integer numeric positions
3024
+ ``range(x)``.
3025
+
3022
3026
heights : list of array-like or dict of array-like or 2D array
3023
3027
The heights for all x and groups. One of:
3024
3028
@@ -3077,27 +3081,40 @@ def grouped_bar(self, x, heights, dataset_labels=None):
3077
3081
elif hasattr (heights , 'shape' ):
3078
3082
heights = heights .T
3079
3083
3080
- num_labels = len (x )
3084
+ num_groups = len (x )
3081
3085
num_datasets = len (heights )
3082
3086
3083
- for dataset in heights :
3084
- assert len (dataset ) == num_labels
3087
+ if isinstance (x [0 ], str ):
3088
+ tick_labels = x
3089
+ group_centers = np .arange (num_groups )
3090
+ else :
3091
+ if num_groups > 1 :
3092
+ d = np .diff (x )
3093
+ if not np .allclose (d , d .mean ()):
3094
+ raise ValueError ("'x' must be equidistant" )
3095
+ group_centers = np .asarray (x )
3096
+ tick_labels = None
3097
+
3098
+ for i , dataset in enumerate (heights ):
3099
+ if len (dataset ) != num_groups :
3100
+ raise ValueError (
3101
+ f"'x' indicates { num_groups } groups, but dataset { i } "
3102
+ f"has { len (dataset )} groups"
3103
+ )
3085
3104
3086
3105
margin = 0.1
3087
3106
bar_width = (1 - 2 * margin ) / num_datasets
3088
- block_centers = np .arange (num_labels )
3089
3107
3090
3108
if dataset_labels is None :
3091
3109
dataset_labels = [None] * num_datasets
3092
3110
else :
3093
3111
assert len (dataset_labels ) == num_datasets
3094
3112
3095
3113
for i , (hs , dataset_label ) in enumerate (zip (heights , dataset_labels )):
3096
- lefts = block_centers - 0.5 + margin + i * bar_width
3097
- print (i , x , lefts , hs , dataset_label )
3114
+ lefts = group_centers - 0.5 + margin + i * bar_width
3098
3115
self .bar (lefts , hs , width = bar_width , align = "edge" , label = dataset_label )
3099
3116
3100
- self .xaxis .set_ticks (block_centers , labels = x )
3117
+ self .xaxis .set_ticks (group_centers , labels = tick_labels )
3101
3118
3102
3119
# TODO: does not return anything for now
3103
3120
0 commit comments