比较行时如何绘制条形图?

Ben*_*enz 3 python matplotlib python-3.x pandas jupyter-notebook

我在此数据集上绘制条形图时遇到麻烦。

+------+------------+--------+
| Year | Discipline | Takers |
+------+------------+--------+
| 2010 | BSCS       |   213  |
| 2010 | BSIS       |   612  |
| 2010 | BSIT       |   796  |
| 2011 | BSCS       |   567  |
| 2011 | BSIS       |   768  |
| 2011 | BSIT       |   504  |
| 2012 | BSCS       |   549  |
| 2012 | BSIS       |   595  |
| 2012 | BSIT       |   586  |
+------+------------+--------+
Run Code Online (Sandbox Code Playgroud)

我正在尝试绘制一个条形图,该条形图用3条形表示每年的接受者数量。这是我做的算法。

import matplotlib.pyplot as plt
import pandas as pd

Y = df_group['Takers']
Z = df_group['Year']

df = pd.DataFrame(df_group['Takers'], index = df_group['Discipline'])
df.plot.bar(figsize=(20,10)).legend(["2010", "2011","2012"])

plt.show()
Run Code Online (Sandbox Code Playgroud)

我希望显示类似此图的内容

具有相同的传说

在此处输入图片说明

jez*_*ael 5

首先重塑DataFrame.pivot,绘制,最后添加标签,如下所示

ax = df.pivot('Discipline', 'Year','Takers').plot.bar(figsize=(10,10))

for p in ax.patches: 
    ax.annotate(np.round(p.get_height(),decimals=2), (p.get_x()+p.get_width()/2., p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')
Run Code Online (Sandbox Code Playgroud)

图片