【问题标题】:how to display the y-values on the top of each bar of the seaborn bargraph?如何在 seaborn 条形图的每个条形顶部显示 y 值?
【发布时间】:2020-09-15 20:12:43
【问题描述】:

我正在尝试生成如下条形图:


# generate the bargraphs based on the `stat_list_prop`
def plot_bargraph_min_error(stat_list_prop, bar_color):

    plt.xlabel("Layer")
    plt.ylabel('Probability of Producing the Minimal Error')    
    sns.barplot(x=[x+1 for x in range(7)], y = stat_list_prop, color = bar_color)

stat_list_prop=[0.5,0.1,0.3,0.4,0.6,0.9,0.2]

# generates a bargraph without the numbers
plot_bargraph_min_error(stat_list_prop,'green')

但我想在我制作的条形图的每个条形的顶部显示 y 值。例如,第一个条形图的高度为 0.5,所以我希望数字 0.5 显示在条形图的第一个条形图的顶部。

我该怎么做?因为我使用的是 seaborn 条形图,所以我不知道该怎么做。

我想要的类似于下图,除了我想要垂直条形图,而不是水平条形图

谢谢,

【问题讨论】:

    标签: python bar-chart seaborn


    【解决方案1】:
    # generate the bargraphs based on the `stat_list_prop`
    def plot_bargraph_min_error(stat_list_prop, bar_color):
    
        plt.xlabel("Layer")
        plt.ylabel('Probability of Producing the Minimal Error')    
        sns.barplot(x=[x+1 for x in range(7)], y = stat_list_prop, color = bar_color)
    
        # text legends on each bar
        for n,i in enumerate(stat_list_prop): 
            plt.text(n,i, str(i))   
    
    
    # Your defined list 
    stat_list_prop=[0.5,0.1,0.3,0.4,0.6,0.9,0.2]
     
    # generates a bargraph without the numbers
    plot_bargraph_min_error(stat_list_prop,'green')
    

    【讨论】:

    • 我希望每个数字直接显示在每个条形上方,而不是窗口顶部
    • raise ValueError(f"Grouper for '{t}' not 1-dimensional")
    • 您使用的数据与示例有何不同?你在使用多维数组吗?这可能是因为您无法将此类数据转换为文本。
    • x 轴全错了,x 刻度在窗口顶部,而我想要它沿着 x 轴
    • plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = True plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = False 或者重启内核
    猜你喜欢
    • 2019-01-25
    • 1970-01-01
    • 2019-08-01
    • 2014-09-21
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多