大家好,我是Alvin Han。基于兴趣自学Python有5个月了,最近全球流行新冠病毒,想做一个用python写的数据可视化各年龄段的新冠病毒的人数(这其实是我们Data mining课大作业)。
由于初学python,代码显得十分青涩,本人想多多改进此代码。望各方大佬多多指点,不吝赐教!
作者初学python,小白萌新一枚~~勿喜勿喷,勿喜请点击上方关闭键?。
import csv
from matplotlib import pyplot as plt
filename = 'Coronavirus1.csv'
with open(filename) as f:
reader = csv.reader(f)
store = []
header_row = next(reader)
for i in range(841):
header_row = next(reader)
store.append(header_row[7])
store = [int(i) for i in store]
for j in range(841):
if 0
store[j] = 10
elif 20<=store[j]<40:
store[j] = 30
elif 40<=store[j]<60:
store[j] = 50
elif 60<=store[j]<80:
store[j] = 70
else:
store[j] = 90
y_axis = []
y_axis.append(store.count(10))
y_axis.append(store.count(30))
y_axis.append(store.count(50))
y_axis.append(store.count(70))
y_axis.append(store.count(90))
x_axis = ['0-20','20-40','40-60','60-80','>80']
plt.bar(x=x_axis, height=y_axis, label='Age',alpha=0.8)
for a,b in enumerate(y_axis):
plt.text(a,b,'%s'%b,ha='center',va='bottom')
plt.title('The population of age for COVID-19')
plt.xlabel("Age")
plt.ylabel("Acount")
plt.legend()
plt.show()
CSV的源文件在我的下载中有