Pandas学习笔记-07_柱状图优化

孤街醉人 提交于 2020-01-25 17:52:22

1. Excel

选中图标,插入,二维条形图,即可生成下图

2. Pandas

import pandas as pd
import matplotlib.pyplot as plt

students = pd.read_excel('C:/Tools/Python/Pandas/010/Students.xlsx')
students.sort_values(by='2017', inplace=True, ascending=False)
# 以最近的一年数据(2017)为基准进行降序排序
print(students)
students.plot.bar('Field', ['2016', '2017'], color=['orange', 'Red'])
plt.title('International Students by Field', fontsize=16)
plt.xlabel('Field', fontweight='bold')
plt.ylabel('Number', fontweight='bold')
plt.tight_layout()
ax = plt.gca()  # gca = get current axis
ax.set_xticklabels(students['Field'], rotation=40, ha='right') # 重新铺x轴上的文字
# 默认旋转以文字的中心为基准进行旋转。 设置ha=’right‘,则设置为水平对齐,以右边的点为基准
plt.gcf().subplots_adjust(left=0.2, bottom=0.42)
# gcf:得到当前的figure 图形, 子图形的调整:左边留出20%的宽度,底部42%的宽度
plt.show()

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!