如何显示多个字典在matplotlib条形图中每一列的总数?

猴面包树1988

我的代码:

import numpy as np
import matplotlib.pyplot as plt

#sample data
ME_Requests = {'John Doe': 47, 'Amanda Doe': 27, 'Maria Doe': 26}
non_ME_Requests = {'John Doe': 105, 'Amanda Doe': 64, 'Maria Doe': 48}
month="Apr-2020"
X = np.arange(len(ME_Requests))
ax = plt.subplot(111)
ax.bar(X, ME_Requests.values(), width=0.2, align='center')
ax.bar(X-0.2, non_ME_Requests.values(), width=0.2, align='center')
ax.legend(('ME_Requests','non_ME_Requests'))
plt.xticks(X, ME_Requests.keys())
plt.title("Emails Closed per team member in {}".format(month) , fontsize=17)
plt.savefig('img.png')
plt.show()

我的条形图当前:

在此处输入图片说明

所需输出:

在此处输入图片说明

我正在尝试以下方法:

#sample data
ME_Requests = {'John Doe': 47, 'Amanda Doe': 27, 'Maria Doe': 26}
non_ME_Requests = {'John Doe': 105, 'Amanda Doe': 64, 'Maria Doe': 48}
month="Apr-2020"

x = np.arange(len(month))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(X, ME_Requests.values(), width=0.2, align='center')
rects2 = ax.bar(X-0.2, non_ME_Requests.values(), width=0.2, align='center')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('# Requests')
ax.set_title("Emails Closed per team member in {}".format(month) , fontsize=17)
ax.set_xticks(X, ME_Requests.keys())
ax.set_xticklabels(month)
ax.legend(('ME_Requests','non_ME_Requests'))


def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')


autolabel(rects1)
autolabel(rects2)

fig.tight_layout()
plt.show()

但是存在一个问题,因为它不会显示用户名(John Doe等),而是显示“ p”或“ r”等。请参见下文:

在此处输入图片说明

有人可以帮我吗?提前致谢!

约翰·C

ax.set_xticklabels(month)应该用代替ax.set_xticklabels(ME_Requests.keys())

要使标签位于条形图之间,您可以为左侧条形图减去width/2X为右侧条形图添加width/2X要删除刻度线,ax.tick_params(axis='x', length=0)可以使用。

import numpy as np
import matplotlib.pyplot as plt

# sample data
ME_Requests = {'John Doe': 47, 'Amanda Doe': 27, 'Maria Doe': 26}
non_ME_Requests = {'John Doe': 105, 'Amanda Doe': 64, 'Maria Doe': 48}
month = "Apr-2020"
X = np.arange(len(ME_Requests))
fig, ax = plt.subplots()
rects1 = ax.bar(X + 0.1, ME_Requests.values(), width=0.2, align='center')
rects2 = ax.bar(X - 0.1, non_ME_Requests.values(), width=0.2, align='center')
ax.legend(('ME_Requests', 'non_ME_Requests'))
ax.set_xticks(X)
ax.set_xticklabels(ME_Requests.keys())
ax.set_title(f"Emails Closed per team member in {month}", fontsize=17)
ax.tick_params(axis='x', length=0)

def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate(f'{height:.0f}',
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

fig.tight_layout()
plt.show()

结果

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何获得每一列的总数

如何使用实时图表在条形图的每一列上添加标签

Matplotlib在一张图中绘制多个条形图

使用 matplotlib 在条形图中并排显示多列

如何在matplotlib的一个图中从熊猫数据框添加多个条形图?

如何在条形图中每个条形显示总数的百分比 Tableau

堆叠条形图仅显示每隔一列

如何将条形图居中以显示某一列的差异?

如何在熊猫数据框中的每一列中绘制唯一值的数量作为条形图?

Amchart 4:如何在条形图中单击条形/列时突出显示条形/列

堆叠的条形图,其中每一列都是Altair中具有多列的条形图

如何用一张图上显示的两个字典中的值注释熊猫条形图?

如何在条形图的条形图中显示值?

如何在matplotlib条形图中的条形上方显示Y值?

如何消除Matplotlib条形图中的条形之间的间隙

如何在matplotlib pyplot条形图中反转条形大小?

一个条形图中的多个条形

如何在 Matplotlib 的子图中添加多个条形图

如何在 Matplotlib 的条形图中的图例上分配多个标签和颜色?

如何在matplotlib的条形图中添加多个数据标签

如何在Matplotlib中的堆叠水平条形图中显示数据值

如何在水平堆叠条形图中显示条形值

在matplotlib中的多个条形图中保留xticks

ggplot 条形图将多个值显示为一个图中的标签

如何有条件地在条形图上的海洋条形图中显示数据框的不同列值?

如何在R中的同一图中显示并排条形图以及堆叠条形图?

按升序/降序,Matplotlib,Python在图中显示条形图

在分组的条形图中显示负值的问题(matplotlib)

如何为 matplotlib 条形图中的特定列设置不同的颜色?