本文介绍了如何按正确顺序绘制分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作标准化考试的熟练程度分组条形图。以下是我的代码:
bush_prof_boy = bush.groupby(['BOY Prof'])['BOY Prof'].count() bush_prof_pct_boy = bush_prof_boy/bush['BOY Prof'].count() * 100 bush_prof_eoy = bush.groupby(['EOY Prof'])['EOY Prof'].count() bush_prof_pct_eoy = bush_prof_eoy/bush['EOY Prof'].count() * 100 labels = ['Remedial', 'Below Proficient', 'Proficient', 'Advanced'] fig, ax = plt.subplots() rects1 = ax.bar(x - width/2, bush_prof_pct_boy, width, label='BOY', color='mediumorchid') rects2 = ax.bar(x + width/2, bush_prof_pct_eoy, width, label='EOY', color='teal') ax.set_ylabel('% of Students at Proficiency Level', fontsize=18) ax.set_title('Bushwick Middle Change in Proficiency Levels', fontsize=25) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=25) ax.legend(fontsize=25) plt.yticks(fontsize=15) plt.figure(figsize=(5,15)) plt.show() &BOY";代表&年初和&年终,因此条形图旨在显示在年初和年底落入各个熟练程度级别的学生的百分率。?这张图看起来不错,但当我深入到数字中时,我可以看到eoy的标签是不正确的。这是我的图表:男孩的百分比绘制正确,但eoy的百分比标签错误。以下是实际的百分比,我确信它们是正确的:
BOY % Advanced 14.0 Below Proficient 38.0 Proficient 34.0 Remedial 14.0 EOY % Advanced 39.0 Below Proficient 18.0 Proficient 32.0 Remedial 11.0 推荐答案- 使用来自Kaggle: Brooklyn NY Schools的数据
- 单独计算条形组可能会有问题。最好在一个数据框内进行计算,对数据框进行整形,然后绘制,因为这将确保条形图绘制在正确的组中。
- 由于未提供数据,因此此操作从宽形式的数字数据开始,然后清理和整形数据帧。
- 使用.cut 将数值转换为类别值
- 使用.melt将Dataframe转换为长格式,然后使用.groupby计算'x of Year' 中的百分比
- 用.pivot整形,用pandas.DataFrame.plot绘图
- 在python 3.8、pandas 1.3.1和matplotlib 3.4.2中测试
- 查看条形图标签是否匹配dfp
更多推荐
如何按正确顺序绘制分组条形图
发布评论