@@ -54,15 +54,12 @@ def survey(results, category_names):
5454 for i , (colname , color ) in enumerate (zip (category_names , category_colors )):
5555 widths = data [:, i ]
5656 starts = data_cum [:, i ] - widths
57- ax .barh (labels , widths , left = starts , height = 0.5 ,
58- label = colname , color = color )
59- xcenters = starts + widths / 2
57+ rects = ax .barh (labels , widths , left = starts , height = 0.5 ,
58+ label = colname , color = color )
6059
6160 r , g , b , _ = color
6261 text_color = 'white' if r * g * b < 0.5 else 'darkgrey'
63- for y , (x , c ) in enumerate (zip (xcenters , widths )):
64- ax .text (x , y , str (int (c )), ha = 'center' , va = 'center' ,
65- color = text_color )
62+ ax .bar_label (rects , label_type = 'center' , color = text_color )
6663 ax .legend (ncol = len (category_names ), bbox_to_anchor = (0 , 1 ),
6764 loc = 'lower left' , fontsize = 'small' )
6865
@@ -85,7 +82,7 @@ def survey(results, category_names):
8582import matplotlib
8683matplotlib .axes .Axes .barh
8784matplotlib .pyplot .barh
88- matplotlib .axes .Axes .text
89- matplotlib .pyplot .text
85+ matplotlib .axes .Axes .bar_label
86+ matplotlib .pyplot .bar_label
9087matplotlib .axes .Axes .legend
9188matplotlib .pyplot .legend
0 commit comments