数据:数据
代码:
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)
结果: