【问题标题】:How to display the value of the bar on each bar with pyplot.barh()如何使用 pyplot.barh() 在每个条上显示条的值
【发布时间】:2022-01-13 04:43:17
【问题描述】:

我生成了一个条形图,如何在每个条形图上显示条形图的值?

当前剧情:

我想得到什么:

我的代码:

import os
import numpy as np
import matplotlib.pyplot as plt

x = [u'INFO', u'CUISINE', u'TYPE_OF_PLACE', u'DRINK', u'PLACE', u'MEAL_TIME', u'DISH', u'NEIGHBOURHOOD']
y = [160, 167, 137, 18, 120, 36, 155, 130]

fig, ax = plt.subplots()    
width = 0.75 # the width of the bars 
ind = np.arange(len(y))  # the x locations for the groups
ax.barh(ind, y, width, color="blue")
ax.set_yticks(ind+width/2)
ax.set_yticklabels(x, minor=False)
plt.title('title')
plt.xlabel('x')
plt.ylabel('y')      
#plt.show()
plt.savefig(os.path.join('test.png'), dpi=300, format='png', bbox_inches='tight') # use format='svg' or 'pdf' for vectorial pictures

【问题讨论】:

    标签: python matplotlib bar-chart


    【解决方案1】:

    添加:

    for i, v in enumerate(y):
        ax.text(v + 3, i + .25, str(v), color='blue', fontweight='bold')
    

    结果:

    y 值 v 既是 x 位置又是 ax.text 的字符串值,方便地,条形图的每个条的度量为 1,因此枚举 i 是 y 位置。

    【讨论】:

    • 可能替换使用 va='center',而不是 "i + .25" 进行水平对齐
    • plt.text(v, i, " "+str(v), color='blue', va='center', fontweight='bold')
    • 如何抑制 jupyter notebook 中的文本输出? “;”最后不起作用。
    • 如果我想在栏的顶部打印 NEIGHBORHOOD 并且所有 baove 栏都相同,而不是在左侧,需要做什么。尝试并搜索了很多地方,但直到现在都没有任何线索。
    • @RalfHundewadt 只需在末尾添加一个 plt.show() 子句。例如:df.plot(); plt.show()
    【解决方案2】:

    我注意到api example code 包含一个条形图示例,每个条形上显示条形的值:

    """
    ========
    Barchart
    ========
    
    A bar plot with errorbars and height labels on individual bars
    """
    import numpy as np
    import matplotlib.pyplot as plt
    
    N = 5
    men_means = (20, 35, 30, 35, 27)
    men_std = (2, 3, 4, 1, 2)
    
    ind = np.arange(N)  # the x locations for the groups
    width = 0.35       # the width of the bars
    
    fig, ax = plt.subplots()
    rects1 = ax.bar(ind, men_means, width, color='r', yerr=men_std)
    
    women_means = (25, 32, 34, 20, 25)
    women_std = (3, 5, 2, 3, 3)
    rects2 = ax.bar(ind + width, women_means, width, color='y', yerr=women_std)
    
    # add some text for labels, title and axes ticks
    ax.set_ylabel('Scores')
    ax.set_title('Scores by group and gender')
    ax.set_xticks(ind + width / 2)
    ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
    
    ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))
    
    
    def autolabel(rects):
        """
        Attach a text label above each bar displaying its height
        """
        for rect in rects:
            height = rect.get_height()
            ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                    '%d' % int(height),
                    ha='center', va='bottom')
    
    autolabel(rects1)
    autolabel(rects2)
    
    plt.show()
    

    输出:

    仅供参考What is the unit of height variable in "barh" of matplotlib?(截至目前,没有简单的方法为每个条设置固定高度)

    【讨论】:

    • 即使原始 x 有小数位,get_x() 似乎也会将数字向上取整。如何获得更多的小数位来显示?
    • 迟到了,但对于其他使用此功能的人来说,在自动标签功能中使用 height+0.1 而不是 1.05*height 会在条形和文本之间创建一致的间隙
    【解决方案3】:

    matplotlib 3.4.0 中的新功能

    现在有一个内置的Axes.bar_label 便捷方法:

    x = [u'INFO', u'CUISINE', u'TYPE_OF_PLACE', u'DRINK', u'PLACE', u'MEAL_TIME', u'DISH', u'NEIGHBOURHOOD']
    y = [160, 167, 137, 18, 120, 36, 155, 130]
    ind = np.arange(len(y))
    
    fig, ax = plt.subplots()
    ax.barh(ind, y)
    ax.set_yticks(ind)
    ax.set_yticklabels(x)
    
    # new helper method to auto-label bars
    ax.bar_label(ax.containers[0])
    

    对于分组条形图,请改为迭代 ax.containers

    for container in ax.containers:
        ax.bar_label(container)
    

    更全面的demo见官方文档bar labeling examples

    【讨论】:

    • 谢谢!这个新功能为我节省了数小时手动摆弄的时间。
    • 谢谢!太棒了!现在我不需要花太多力气来调整计数器的位置了!
    【解决方案4】:

    使用 plt.text() 将文本放入绘图中。

    例子:

    import matplotlib.pyplot as plt
    N = 5
    menMeans = (20, 35, 30, 35, 27)
    ind = np.arange(N)
    
    #Creating a figure with some fig size
    fig, ax = plt.subplots(figsize = (10,5))
    ax.bar(ind,menMeans,width=0.4)
    #Now the trick is here.
    #plt.text() , you need to give (x,y) location , where you want to put the numbers,
    #So here index will give you x pos and data+1 will provide a little gap in y axis.
    for index,data in enumerate(menMeans):
        plt.text(x=index , y =data+1 , s=f"{data}" , fontdict=dict(fontsize=20))
    plt.tight_layout()
    plt.show()
    

    这会将图形显示为:

    【讨论】:

    • 无论如何要将文本向左移动一点?
    • @S.Ramjit plt.text(x=index , y =data+1 , s=f"{data}" , fontdict=dict(fontsize=20), va='center')
    • @theGtknerd 他要的是ha='center'
    • @S.Ramjit 你的意思是左边的像素吗? ;P
    • @S.Ramjit 它必须是 ha='center' 因为这里我们需要水平对齐,而不是垂直对齐。
    【解决方案5】:

    对于任何想要在其条形图的底部放置标签的人,只需将 v 除以标签的值,如下所示:

    for i, v in enumerate(labels):
        axes.text(i-.25, 
                  v/labels[i]+100, 
                  labels[i], 
                  fontsize=18, 
                  color=label_color_list[i])
    

    (注意:我加了 100,所以它不是绝对在底部)

    要得到这样的结果:

    【讨论】:

    • 当然是v == labels[i],所以第三行和第四行可能只是101, v
    【解决方案6】:

    我知道这是一个旧线程,但我通过 Google 多次登陆这里,并认为没有给出真正令人满意的答案。尝试使用以下功能之一:

    编辑:当我在这个旧线程上获得一些喜欢时,我也想分享一个更新的解决方案(基本上将我以前的两个函数放在一起并自动决定它是条形图还是 hbar 图):

    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)
    

    现在您可以将它们用于常规条形图:

    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)
    

    或者对于水平条形图:

    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)
    

    【讨论】:

      【解决方案7】:

      对于熊猫人:

      ax = s.plot(kind='barh') # s is a Series (float) in [0,1]
      [ax.text(v, i, '{:.2f}%'.format(100*v)) for i, v in enumerate(s)];
      

      就是这样。 或者,对于那些更喜欢apply 而不是使用枚举循环的人:

      it = iter(range(len(s)))
      s.apply(lambda x: ax.text(x, next(it),'{:.2f}%'.format(100*x)));
      

      此外,ax.patches 将提供与ax.bar(...) 相同的条形图。如果您想应用@SaturnFromTitan 的功能或其他人的技术。

      【讨论】:

      • 请检查 i, v 是否倒置。也许应该是[ax.text(i, v, '{:.2f}%'.format(100*v)) for i, v in enumerate(s)];
      • @JairoAlves 这是一个水平条形图,v 表示 x 轴上的位置,所以应该是正确的。另请参阅已接受的答案。
      • 补丁示例for p in ax.patches: ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
      【解决方案8】:

      我也需要条形标签,请注意,我的 y 轴正在使用 y 轴上的限制进行缩放视图。将标签放在条形顶部的默认计算仍然使用高度(示例中的 use_global_coordinate=False)。但我想表明,标签也可以使用matplotlib 3.0.2 中的全局坐标在缩放视图中放置在图表的底部。希望对某人有所帮助。

      def autolabel(rects,data):
      """
      Attach a text label above each bar displaying its height
      """
      c = 0
      initial = 0.091
      offset = 0.205
      use_global_coordinate = True
      
      if use_global_coordinate:
          for i in data:        
              ax.text(initial+offset*c, 0.05, str(i), horizontalalignment='center',
                      verticalalignment='center', transform=ax.transAxes,fontsize=8)
              c=c+1
      else:
          for rect,i in zip(rects,data):
              height = rect.get_height()
              ax.text(rect.get_x() + rect.get_width()/2., height,str(i),ha='center', va='bottom')
      

      【讨论】:

        【解决方案9】:

        我试图用堆叠的绘图条来做到这一点。对我有用的代码是。

        # Code to plot. Notice the variable ax.
        ax = df.groupby('target').count().T.plot.bar(stacked=True, figsize=(10, 6))
        ax.legend(bbox_to_anchor=(1.1, 1.05))
        
        # Loop to add on each bar a tag in position
        for rect in ax.patches:
            height = rect.get_height()
            ypos = rect.get_y() + height/2
            ax.text(rect.get_x() + rect.get_width()/2., ypos,
                    '%d' % int(height), ha='center', va='bottom')
        

        【讨论】:

          【解决方案10】:

          检查此链接 Matplotlib Gallery 这就是我使用autolabel的代码sn-p的方式。

              def autolabel(rects):
              """Attach a text label above each bar in *rects*, displaying its height."""
              for rect in rects:
                  height = rect.get_height()
                  ax.annotate('{}'.format(height),
                              xy=(rect.get_x() + rect.get_width() / 2, height),
                              xytext=(0, 3),  # 3 points vertical offset
                              textcoords="offset points",
                              ha='center', va='bottom')
                  
          temp = df_launch.groupby(['yr_mt','year','month'])['subs_trend'].agg(subs_count='sum').sort_values(['year','month']).reset_index()
          _, ax = plt.subplots(1,1, figsize=(30,10))
          bar = ax.bar(height=temp['subs_count'],x=temp['yr_mt'] ,color ='g')
          autolabel(bar)
          
          ax.set_title('Monthly Change in Subscribers from Launch Date')
          ax.set_ylabel('Subscriber Count Change')
          ax.set_xlabel('Time')
          plt.show()
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-01-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多