How to display the value of the bar on each bar with pyplot.barh()?

后端 未结 9 680
不知归路
不知归路 2020-11-22 08:10

I generated a bar plot, how can I display the value of the bar on each bar?

Current plot:

\"enter

9条回答
  •  逝去的感伤
    2020-11-22 08:23

    I know it's an old thread, but I landed here several times via Google and think no given answer is really satisfying yet. Try using one of the following functions:

    EDIT: As I'm getting some likes on this old thread, I wanna share an updated solution as well (basically putting my two previous functions together and automatically deciding whether it's a bar or hbar plot):

    def label_bars(ax, bars, text_format, **kwargs):
        """
        Attaches a label on every bar of a regular or horizontal bar chart
        """
        ys = [bar.get_y() for bar in bars]
        y_is_constant = all(y == ys[0] for y in ys)  # -> regular bar chart, since all all bars start on the same y level (0)
    
        if y_is_constant:
            _label_bar(ax, bars, text_format, **kwargs)
        else:
            _label_barh(ax, bars, text_format, **kwargs)
    
    
    def _label_bar(ax, bars, text_format, **kwargs):
        """
        Attach a text label to each bar displaying its y value
        """
        max_y_value = ax.get_ylim()[1]
        inside_distance = max_y_value * 0.05
        outside_distance = max_y_value * 0.01
    
        for bar in bars:
            text = text_format.format(bar.get_height())
            text_x = bar.get_x() + bar.get_width() / 2
    
            is_inside = bar.get_height() >= max_y_value * 0.15
            if is_inside:
                color = "white"
                text_y = bar.get_height() - inside_distance
            else:
                color = "black"
                text_y = bar.get_height() + outside_distance
    
            ax.text(text_x, text_y, text, ha='center', va='bottom', color=color, **kwargs)
    
    
    def _label_barh(ax, bars, text_format, **kwargs):
        """
        Attach a text label to each bar displaying its y value
        Note: label always outside. otherwise it's too hard to control as numbers can be very long
        """
        max_x_value = ax.get_xlim()[1]
        distance = max_x_value * 0.0025
    
        for bar in bars:
            text = text_format.format(bar.get_width())
    
            text_x = bar.get_width() + distance
            text_y = bar.get_y() + bar.get_height() / 2
    
            ax.text(text_x, text_y, text, va='center', **kwargs)
    

    Now you can use them for regular bar plots:

    fig, ax = plt.subplots((5, 5))
    bars = ax.bar(x_pos, values, width=0.5, align="center")
    value_format = "{:.1%}"  # displaying values as percentage with one fractional digit
    label_bars(ax, bars, value_format)
    

    or for horizontal bar plots:

    fig, ax = plt.subplots((5, 5))
    horizontal_bars = ax.barh(y_pos, values, width=0.5, align="center")
    value_format = "{:.1%}"  # displaying values as percentage with one fractional digit
    label_bars(ax, horizontal_bars, value_format)
    

提交回复
热议问题