【发布时间】:2018-05-16 04:39:38
【问题描述】:
我有一个条形图,其中显示 2 个条形图,一个代表 2015 年,一个代表 2016 年。
- x轴显示药物名称
- y 轴显示规定的数量
我想在条上显示注释只是为了更清楚地说明规定的数字。我尝试了以下但没有任何反应?
有人知道我哪里出错了吗?
这是我的代码
for p in ax.patches:
ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
fig = plt.figure() # Create matplotlib figure
ax = fig.add_subplot(111) # Create matplotlib axes
width = 0.4
df.Occurance_x.plot(kind='bar', color='purple', ax=ax, width=width, position=1)
df.Occurance_y.plot(kind='bar', color='blue', ax=ax, width=width, position=0)
ax.set_ylabel('Amount Prescribed in 1 year')
x_labels = df.VTM_NM
ax.set_xticklabels(x_labels, rotation=30)
plt.legend(['2016', '2017'], loc='upper right')
ax.set_title('BNF Chapter 1 Top 5 drugs prescribed')
plt.show()
【问题讨论】:
-
你需要在
for后面的那一行输入4个空格。
标签: python python-3.x pandas matplotlib jupyter-notebook