【问题标题】:MatPlotLib: How to show sum labels on each bar for a stacked bar chart?MatPlotLib:如何在堆积条形图的每个条形上显示总和标签?
【发布时间】:2021-02-06 16:17:55
【问题描述】:

我正在使用 matplotlib 绘制以下堆积条形图,并且我想在每个堆积条的顶部显示(整个条的)总视图。这是我用来创建图表的代码:

okrViews = okrs_results['Page Views'].tolist()
blogViews = blog_results['Page Views'].tolist()
landingPageViews = landing_page_results['Page Views'].tolist()
teamsViews = teams_results['Page Views'].tolist()
eventsViews = events_results['Page Views'].tolist()
initiativesViews = initiatives_results['Page Views'].tolist()
toolsViews = tools_results['Page Views'].tolist()
indx = np.arange(len(months))

# Calculate starting position for each bar
okrsAndBlogs = [i+j for i,j in zip(okrViews, blogViews)]
okrsBlogsLanding = [i+j for i,j in zip(landingPageViews, okrsAndBlogs)]
four = [i+j for i,j in zip(teamsViews, okrsBlogsLanding)]
five = [i+j for i,j in zip(eventsViews, four)]
six = [i+j for i,j in zip(initiativesViews, five)]

# Set figure size
plt.figure(figsize=(19,10))

# Add each bar
graphBlogs = plt.bar(x = indx, height = blogViews, width = .45, color='tab:blue', label = 'Blogs')
graphOkrs = plt.bar(x = indx, height = okrViews, width = .45, color='tab:pink', bottom = blogViews, label = 'OKRs')
graphLanding = plt.bar(x = indx, height = landingPageViews, width = .45, color='green', bottom = okrsAndBlogs, label = 'Landing Page')
graphTeams = plt.bar(x = indx, height = teamsViews, width = .45, color='orange', bottom = okrsBlogsLanding, label = 'Teams')
graphEvents = plt.bar(x = indx, height = eventsViews, width = .45, color='tab:olive', bottom = four, label = 'Events')
graphInitiatives = plt.bar(x = indx, height = initiativesViews, width = .45, color='tab:purple', bottom = five, label = 'Initiatives; Risk and Data Privacy')
graphTools = plt.bar(x = indx, height = toolsViews, width = .45, color='tab:cyan', bottom = six, label = 'Tools')

    
# Set labels
plt.xticks(indx, months)
plt.xlabel("Month")
plt.ylabel("Total Views")
plt.title('Views per Day, Grouped by Month, YTD')
plt.legend()

plt.show()

我该怎么做呢?为了进一步解释,我希望在一月份的堆叠条上方有约 10,000 个,在二月的上方有约 8,000 个,依此类推。提前致谢!

【问题讨论】:

    标签: python matplotlib jupyter-notebook data-visualization


    【解决方案1】:

    更新:想通了,我只需要手动总结,然后我使用了plt.text。在之前的单元格中,我已将 months 初始化为月份名称列表。

    解决方案:

    monthTotals = []
    for month in months:
        total = 0
        total += int(okrs_results[okrs_results['Month'] == month]['Page Views'])
        total += int(blog_results[blog_results['Month'] == month]['Page Views'])
        total += int(landing_page_results[landing_page_results['Month'] == month]['Page Views'])
        total += int(teams_results[teams_results['Month'] == month]['Page Views'])
        total += int(events_results[events_results['Month'] == month]['Page Views'])
        total += int(initiatives_results[initiatives_results['Month'] == month]['Page Views'])
        total += int(tools_results[initiatives_results['Month'] == month]['Page Views'])
        monthTotals.append(total)
    
    okrViews = okrs_results['Page Views'].tolist()
    blogViews = blog_results['Page Views'].tolist()
    landingPageViews = landing_page_results['Page Views'].tolist()
    teamsViews = teams_results['Page Views'].tolist()
    eventsViews = events_results['Page Views'].tolist()
    initiativesViews = initiatives_results['Page Views'].tolist()
    toolsViews = tools_results['Page Views'].tolist()
    indx = np.arange(len(months))
    
    # Calculate starting position for each bar
    okrsAndBlogs = [i+j for i,j in zip(okrViews, blogViews)]
    okrsBlogsLanding = [i+j for i,j in zip(landingPageViews, okrsAndBlogs)]
    four = [i+j for i,j in zip(teamsViews, okrsBlogsLanding)]
    five = [i+j for i,j in zip(eventsViews, four)]
    six = [i+j for i,j in zip(initiativesViews, five)]
    
    # Set figure size
    plt.figure(figsize=(19,10))
    
    # Add each bar
    graphBlogs = plt.bar(x = indx, height = blogViews, width = .45, color='tab:blue', label = 'Blogs')
    graphOkrs = plt.bar(x = indx, height = okrViews, width = .45, color='tab:pink', bottom = blogViews, label = 'OKRs')
    graphLanding = plt.bar(x = indx, height = landingPageViews, width = .45, color='green', bottom = okrsAndBlogs, label = 'Landing Page')
    graphTeams = plt.bar(x = indx, height = teamsViews, width = .45, color='orange', bottom = okrsBlogsLanding, label = 'Teams')
    graphEvents = plt.bar(x = indx, height = eventsViews, width = .45, color='tab:olive', bottom = four, label = 'Events')
    graphInitiatives = plt.bar(x = indx, height = initiativesViews, width = .45, color='tab:purple', bottom = five, label = 'Initiatives; Risk and Data Privacy')
    graphTools = plt.bar(x = indx, height = toolsViews, width = .45, color='tab:cyan', bottom = six, label = 'Tools')
    
    # Show sum on each stacked bar
    for i, v in enumerate(monthTotals):
        if v != 0:
            plt.text(indx[i] - .2, v, str(v))
        
    # Set labels
    plt.xticks(indx, months)
    plt.xlabel("Month")
    plt.ylabel("Total Views")
    plt.title('Views per Day, Grouped by Month, YTD')
    plt.legend()
    
    plt.show()
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-04
      • 2023-04-01
      • 1970-01-01
      • 2018-11-30
      • 2012-12-24
      • 2018-01-17
      • 2019-11-28
      • 1970-01-01
      相关资源
      最近更新 更多