【问题标题】:How to display the values on the bar plot for each bar with barh() in this case? [duplicate]在这种情况下,如何使用 barh() 在条形图上显示值? [复制]
【发布时间】:2021-10-29 00:52:22
【问题描述】:

在这种情况下如何在它的顶部添加bar的值?

我想得到什么:

【问题讨论】:

    标签: python matplotlib plot data-visualization bar-chart


    【解决方案1】:

    您应该添加为matplotlib.axes.Axes.text
    如果你有这样的情节:

    import matplotlib.pyplot as plt
    
    
    labels = ['A', 'B', 'C']
    values = [150, 80, 10]
    
    
    fig, ax = plt.subplots()
    
    ax.barh(labels, values)
    
    plt.show()
    

    您可以使用此循环添加标签(您可能需要调整 x 轴范围以适合标签):

    for i, value in enumerate(values):
        ax.text(value + 3, i, str(value))
    xmin, xmax = ax.get_xlim()
    ax.set_xlim(xmin, 1.1*xmax)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      相关资源
      最近更新 更多