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

标签 python python-3.x pandas matplotlib 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  |
+------+------------+--------+

我正在尝试绘制一个条形图,其中 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()

我期待显示类似这张图的东西

具有相同的传说

enter image description here

最佳答案

首先由 DataFrame.pivot reshape , plot and last add labels by this :

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')

pic

关于python - 比较行时如何绘制条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56702605/

相关文章:

python - 使用 SWIG 在 C++ Python 包装类中用二进制数据填充 char*

python - 使用基于现有行值的条件在 Pandas 中创建新列并返回另一行的值

python - 如何使用 Python 2.7 和 Python 3.4 启动两个 Spyder 实例?

python-3.x - 即使安装 ffmpeg 也找不到 FFserver 命令

python - Pandas 错误 - 为什么我的对象是混合类型?

python - 如何在 while 循环中连接两个值?

python - 你能检查一下 Python 中的 doctest 是否引发了异常吗?

python-3.x - 第一次运行 gekko 时,从 werkzeug.http' 得到这个错误 "cannot import name ' dump_csp_header'

python - Pandas 读取 csv 替换#DIV/0!和#VALUE!与南

python - 用 "sign"数字填充 DataFrame