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 = 'black'
            text_y = bar.get_height() - inside_distance
        else:
            color = 'white'
            text_y = bar.get_height() + outside_distance
        ax.text(text_x, text_y, text, ha='center', va='bottom', color=color,
            **kwargs)
