222
# coding utf-8 # import matplotlib import numpy as np import matplotlib.pyplot as plt import matplotlib import xlrd def read_excel(path): try: list = [] file = xlrd.open_workbook(path) sheet = file.sheet_by_index(0) rows = sheet.row_values for line in range(0, sheet.nrows): list.append(rows(line)) return list except: pass def fun1(): # population = read_excel(r‘renkou2016.xlsx‘) # 人口列表 population=[[‘年末总人口(万人)‘, 138271.0], [‘0-14岁人口(万人)‘, 23091.0], [‘15-64岁人口(万人)‘, 100246.0], [‘65岁及以上人口(万人)‘, 14933.0]]#测试数据 date = [] name = [] for i in range(0,len(population)): date.append(population[i][1]) name.append(population[i][0]) # 中文乱码的处理 plt.rcParams[‘font.sans-serif‘] = [‘SimHei‘] # 步骤一(替换sans-serif字体) plt.rcParams[‘axes.unicode_minus‘] = False # 步骤二(解决坐标轴负数的负号显示问题) # 绘图 plt.bar(range(4), date, align = ‘center‘,color=‘steelblue‘, alpha = 0.8) # 添加轴标签 plt.ylabel(‘人口‘) # 添加标题 plt.title(‘2016年人口各年龄阶段分布的柱形图‘) # 添加刻度标签 plt.xticks(range(len(date)), name) # 设置Y轴的刻度范围 plt.ylim([5000,150000]) # 为每个条形图添加数值标签 for x, y in enumerate(date): plt.text(x, y+100, ‘%s‘ % round(y, 1), ha=‘center‘) # 显示图形 plt.show() return plt if __name__ == ‘__main__‘: plt = fun1() plt.show()
原文地址:https://www.cnblogs.com/donke/p/10126286.html
时间: 2024-11-25 23:42:26