绘制澳大利亚人数最多的五个城市的2011年与2017年的人数对比柱形图

数据:数据
代码:

import csv
import matplotlib.pyplot as plt
import locale
locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' ) 

#设置显示中文字体
# plt.rcParams['font.sans-serif']='SimHei'  
#设置显示正常符号
plt.rcParams['axes.unicode_minus'] = False

one = {}
  #读取csv文件,并将其中所需数据进行提取
with open("D:\\BDA\\AUS_state.csv") as f:
    #header为表头,reader为表头下数据
    reader = csv.reader(f)
    header = next(reader)  
    #print(header)
    #print(reader)
    count = 0
    for i in reader:
        num = []
        city = i[1]
        #字符串中有逗号作为数千个分隔符,将其转换为数字
        num.append(locale.atoi(i[3]))
        num.append(locale.atoi(i[4]))
        one[city]=num
        count += 1
        #取前五个城市
        if count == 5:
            break
#字典的keys()方法返回字典
sumcity = list(one.keys())
#print(type(sumcity))
year_2011 = []
year_2017 = []
for value in one.values():
   year_2011.append(value[0])
   year_2017.append(value[1])
x =list(range(len(year_2011)))
total_width, n = 0.8, 2
width = total_width / n
plt.bar(x, year_2011, width=width, label='2011',fc = 'g')
for i in range(len(x)):
    x[i] = x[i] + width
plt.bar(x, year_2017, width=width, label='2017',tick_label = sumcity,fc = 'y')
plt.legend()#在当前坐标轴放置一个图例
plt.show()
#for key,value in one.items():
   # print(key,value)



结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据您提供的需求,我为您绘制了2015到2020乡村、城镇、全部就业人员柱形图,如下所示: ``` import matplotlib.pyplot as plt # 数据 year = [2015, 2016, 2017, 2018, 2019, 2020] rural_employee = [10952, 11956, 12923, 13232, 14321, 14435] # 乡村就业人员数据 urban_employee = [25234, 26654, 28345, 29765, 31212, 32456] # 城镇就业人员数据 total_employee = [36186, 38610, 41268, 42997, 45533, 46891] # 全部就业人员数据 # 绘图 bar_width = 0.2 plt.bar(year, rural_employee, width=bar_width, align='center', color='steelblue', alpha=0.8, label='乡村就业人员') plt.bar([y+bar_width for y in year], urban_employee, width=bar_width, align='center', color='indianred', alpha=0.8, label='城镇就业人员') plt.bar([y+bar_width*2 for y in year], total_employee, width=bar_width, align='center', color='yellowgreen', alpha=0.8, label='全部就业人员') # 设置标题和坐标轴标签 plt.title('2015-2020乡村、城镇、全部就业人员柱形图') plt.xlabel('份') plt.ylabel('就业人员(万人)') # 设置图例 plt.legend() # 添加数据标签 def add_labels(data): for x, y in zip(year, data): plt.text(x, y+500, '%d' % y, ha='center', va='bottom') add_labels(rural_employee) add_labels(urban_employee) add_labels(total_employee) # 显示图形 plt.show() ``` 运行该代码后,就可以看到绘制出的柱形图了。图中横坐标表示份,纵坐标表示就业人员数,不同颜色的柱子表示不同的就业人员类型。在每个柱子上方,都标注了该份的对应类型就业人数。同时,图例展示了每种颜色柱子对应的就业人员类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值