[关闭]
@SuHongjun 2020-05-28T09:56:56.000000Z 字数 2074 阅读 305

Python Day23:综合作业(2)

Python 2020春季学期


Python专业开发工具:PyCharm

下载页面:https://www.jetbrains.com/edu-products/download/#section=pycharm-edu

PyCharm基本操作:
Create New Project:设置 location、将Project Interpreter设为"Existing interpreter"
File -- Settings -- Editor:修改Color Scheme、Font
Ctrl + /:注释、取消注释

综合作业

第二步:

如何读取Excel文件中的数据:
以xlrd3模块为例:https://pypi.org/project/xlrd3/
安装xlrd3模块:
C:\Users\shj>pip install xlrd3 -i https://pypi.douban.com/simple

  1. #对excel的操作
  2. import xlrd3
  3. xl = xlrd3.open_workbook(r'E:\try\Python\综合作业\zsyh.xls')
  4. #通过索引获取工作表
  5. table = xl.sheets()[0]
  6. print(table)
  7. # 获取第3行:营业收入的内容
  8. row = table.row_values(2) #索引从0开始
  9. print(row)
  10. # 获取第26行: 五、净利润的内容
  11. row = table.row_values(25)
  12. print(row)

第三步:

统计图:matplotlib模块、.......
C:\Users\shj>pip install matplotlib -i https://pypi.douban.com/simple

  1. import matplotlib
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import xlrd3 #对excel的操作
  5. xl = xlrd3.open_workbook(r'E:\try\Python\综合作业\zsyh.xls')
  6. #通过索引获取工作表
  7. table = xl.sheets()[0]
  8. print(table)
  9. # 获取第1行:报表日期
  10. riqi = table.row_values(0) #索引从0开始
  11. print(riqi)
  12. riqi = riqi[1:]
  13. print(riqi)
  14. riqi = map(int,riqi)
  15. riqi = map(str,riqi)
  16. print(riqi)
  17. riqi = list(riqi)
  18. print(riqi)
  19. # 获取第3行:营业收入的内容
  20. yysr = table.row_values(2) #索引从0开始
  21. print(yysr)
  22. yysr = yysr[1:]
  23. print(yysr)
  24. # 获取第26行: 五、净利润的内容
  25. jlr = table.row_values(25)
  26. print(jlr)
  27. jlr = jlr[1:]
  28. print(jlr)
  29. #.......继续修改........以下代码......
  30. labels = ['G1', 'G2', 'G3', 'G4', 'G5']
  31. men_means = [20, 34, 30, 35, 27]
  32. women_means = [25, 32, 34, 20, 25]
  33. x = np.arange(len(labels)) # the label locations
  34. width = 0.35 # the width of the bars
  35. fig, ax = plt.subplots()
  36. rects1 = ax.bar(x - width/2, men_means, width, label='Men')
  37. rects2 = ax.bar(x + width/2, women_means, width, label='Women')
  38. # Add some text for labels, title and custom x-axis tick labels, etc.
  39. ax.set_ylabel('Scores')
  40. ax.set_title('Scores by group and gender')
  41. ax.set_xticks(x)
  42. ax.set_xticklabels(labels)
  43. ax.legend()
  44. def autolabel(rects):
  45. """Attach a text label above each bar in *rects*, displaying its height."""
  46. for rect in rects:
  47. height = rect.get_height()
  48. ax.annotate('{}'.format(height),
  49. xy=(rect.get_x() + rect.get_width() / 2, height),
  50. xytext=(0, 3), # 3 points vertical offset
  51. textcoords="offset points",
  52. ha='center', va='bottom')
  53. autolabel(rects1)
  54. autolabel(rects2)
  55. fig.tight_layout()
  56. plt.show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注