数据:数据
代码:
import csv
import matplotlib.pyplot as plt
#设置显示中文字体
plt.rcParams['font.sans-serif']='SimHei'
#设置显示正常符号
plt.rcParams['axes.unicode_minus'] = False
two = {}
with open("D:\\BDA\\AUS_state.csv") as f:
#header为表头,reader为表头下数据
reader = csv.reader(f)
header = next(reader)
#print(header)
#print(reader)
for i in reader:
#将增长率后面的%去掉
up = i[5]
up = float(up[:-1])
two[i[1]] = up
count += 1
#增长率降序排列
# sorted函数排好序后,要绑定一个对象(赋值),不绑定字典会自动打乱顺序
two=sorted(two.items(),key = lambda x:x[1],reverse = True)
up = []
city = []
count = 0
for key,value in two :
city.append(key)
up.append(value)
count += 1
if count == 5:
break
plt.plot(city,up,linewidth=3,color='c',marker='o',
markerfacecolor='blue',markersize=12)
plt.xlabel('城市')
plt.ylabel('增长率(%)')
plt.title('澳大利亚2011-2017人口增长前五城市')
plt.show()
结果:
在用excel打开csv文件时出现乱码,没有处理出现以下错误:
解决乱码方法