【问题标题】:Displaying of values on barchart在条形图上显示值
【发布时间】:2018-02-07 08:59:15
【问题描述】:

我发现了一些与此主题类似的帖子。但它们对我没有帮助。

我对 Python 和 Seaborn 比较陌生。

这是我的代码:

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

x_axis = ["A", "B","C","D","E","F"]
y_axis = [78.5, 79.6, 81.6, 75.4, 78.3, 79.6]

plt.ylabel('Accuracy')
plt.title('Accuracy of Classifier')

g=sns.barplot(x_axis, y_axis, color="red")

我只是想在每个条形图的顶部显示 y_axis 的值。

【问题讨论】:

    标签: python bar-chart seaborn


    【解决方案1】:

    遍历补丁并注释条。

    import seaborn as sns
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    x_axis = ["A", "B","C","D","E","F"]
    y_axis = [78.5, 79.6, 81.6, 75.4, 78.3, 79.6]
    
    plt.ylabel('Accuracy')
    plt.title('Accuracy of Classifier')
    
    g=sns.barplot(x_axis, y_axis, color="red")
    ax=g
    #annotate axis = seaborn axis
    for p in ax.patches:
        ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
                    ha='center', va='center', fontsize=11, color='gray', xytext=(0, 20),
                    textcoords='offset points')
    _ = g.set_ylim(0,120) #To make space for the annotations
    

    输出:


    使用matplotlib v3.4.2条形图:

    import matplotlib.pyplot as plt
    %matplotlib inline
    
    x_axis = ["A", "B","C","D","E","F"]
    y_axis = [78.5, 79.6, 81.6, 75.4, 78.3, 79.6]
    
    fig,ax = plt.subplots()
    
    plt.ylabel('Accuracy')
    plt.title('Accuracy of Classifier')
    
    g=ax.bar(x_axis, y_axis, color="red")
    ax.bar_label(g, padding=10)
    
    _ = ax.set_ylim(0,120) #To make space for the annotations
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-12
      • 2022-01-15
      • 1970-01-01
      • 2018-09-28
      相关资源
      最近更新 更多