美文网首页
Python plt.bar 添加数据标签

Python plt.bar 添加数据标签

作者: 一念万水千山 | 来源:发表于2020-12-10 14:19 被阅读0次

    def plot_result(count_df):

        fig = plt.figure()

        #use a figure size of (20, 8),bar width of 0.8, 设置图片大小,柱宽

        #use color #5cb85c for the Very interested bars, 设置柱子颜色

        #color #5bc0de for the Somewhat interested bars,

        #color #d9534f for the Not interested bars,

        c=count_df.plot(kind='bar',figsize=(20, 8),width=0.8,

                color=['#5cb85c','#d9534f','#5bc0de'],alpha = 0.75,fontsize=20)

        #use font size 14 for the bar labels, percentages, and legend, 图例颜色

        plt.legend(fontsize=18)

        #use font size 16 for the title, and, 标题字号

        plt.title("Result ",fontsize=28)

        plt.yticks([]) # y轴空轴

        #display the percentages above the bars as shown above 数据标签列表

        x=np.arange(len(count_df.index))

        yv=np.array(list(count_df['P']))

        ys=np.array(list(count_df['F']))

        yn=np.array(list(count_df['Rpass']))

        for a,b in zip(x,yv): ##控制标签位置

            plt.text(a-0.27,b+0.1,'%.d'%b,ha = 'center',va = 'bottom',fontsize=18)

        for a,b in zip(x,ys):

            plt.text(a,b+0.1,'%.d'%b,ha = 'center',va = 'bottom',fontsize=18)

        for a,b in zip(x,yn):

            plt.text(a+0.27,b+0.1,'%.d'%b,ha = 'center',va = 'bottom',fontsize=18)

        #remove the left, top, and right borders. 去掉图片边框

        c.spines['top'].set_visible(False)

        c.spines['right'].set_visible(False)

        #c.spines['bottom'].set_visible(False) 保留横坐标边框

        c.spines['left'].set_visible(False)

        plt.savefig("./data/result.png")

        plt.show

    plot_result(count_df)

    相关文章

      网友评论

          本文标题:Python plt.bar 添加数据标签

          本文链接:https://www.haomeiwen.com/subject/bqntwktx.html