【发布时间】:2018-12-05 10:00:21
【问题描述】:
这是我的数据框:
6month final-formula Question Text numPatients6month
286231 1 0.031730 CI_FINANCE 977
286270 1 0.147390 CI_MJO 977
286276 1 0.106448 CI_CONCENTRATING 977
286700 2 0.010323 CI_MJO 775
286323 2 0.018065 CI_FINANCE 775
286401 2 0.034839 CI_CONCENTRATING 775
286228 3 0.032020 CI_CONCENTRATING 812
286238 3 0.061576 CI_MJO 812
286292 3 0.008621 CI_FINANCE 812
286690 4 0.008097 CI_MJO 741
286342 4 0.005398 CI_FINANCE 741
286430 4 0.060729 CI_CONCENTRATING 741
286481 5 0.009840 CI_FINANCE 813
287441 5 0.008610 CI_MJO 813
286362 5 0.041820 CI_CONCENTRATING 813
286360 6 0.021622 CI_CONCENTRATING 740
286492 6 0.017568 CI_FINANCE 740
286494 6 0.014865 CI_MJO 740
286482 7 0.015464 CI_FINANCE 776
286483 7 0.042526 CI_MJO 776
286599 7 0.011598 CI_CONCENTRATING 776
286361 8 0.024490 CI_CONCENTRATING 735
286989 8 0.004082 CI_FINANCE 735
286402 8 0.021769 CI_MJO 735
287119 9 0.003916 CI_FINANCE 766
286408 9 0.011749 CI_MJO 766
286399 9 0.019582 CI_CONCENTRATING 766
286267 10 0.019337 CI_CONCENTRATING 724
286249 10 0.037293 CI_MJO 724
286810 10 0.008287 CI_FINANCE 724
我已将此数据框绘制为堆积条形图。
此堆积条形图基于(6month,final-formula)。
如您所见,数据框中有numPatients6month。
我想在每个堆叠条的类别上显示这个数字。
例如:
所以根据上面的堆积条,我想在第一个条中显示蓝色的977,为CI_Finance 显示977,即orange color。
它不同于this问题,因为它不是堆叠条,
另外不同于this,因为我将显示我的数据框中的另一列(numPatients6month),而不是y-axis 中的列。
y 轴是final-formula,但我想在每个堆叠条的每种颜色上显示numPatients6month。
作为信息,我使用以下代码绘制了上面的内容:
df = dffinal.drop('numPatients6month', 1).groupby(['6month','Question Text']).sum().unstack('Question Text')
df.columns = df.columns.droplevel()
ax=df.plot(kind='bar', stacked=True)
import matplotlib.pyplot as plt
plt.xticks(range(0,10), ['6month','1 year','1.5 year','2 year','2.5 year','3 year','3.5 year','4 year','4.5 year','5 year'], fontsize=8, rotation=45)
plt.title('Cognitive Impairement-Stack bar')
plt.show()
谢谢,:)
【问题讨论】:
-
所以您希望每个条形中的相同数字的三倍?一次是蓝色,一次是橙色,一次是绿色?
-
SpghttCd 感谢回复,此数据帧编号相同,但可能不同。每个问题文本的 numPatients6month 列中的任何数字。所以有三个问题文本所以三个数字,但是应该从 numPatients6month 调用该数字
标签: python pandas dataframe matplotlib data-analysis