簡體   English   中英

MatPlotLib:如何在堆積條形圖的每個條形上顯示總和標簽?

[英]MatPlotLib: How to show sum labels on each bar for a stacked bar chart?

我正在使用 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()

我該怎么做呢? 為了進一步解釋,我希望比 1 月的堆積柱高約 10,000,比 2 月的堆積柱高約 8,000,依此類推。 提前致謝!

當前圖表

更新:想通了,我只需要手動總結,然后我使用plt.text 在之前的單元格中,我將months初始化為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()

結果:

解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM