import matplotlib.pyplot as pt import numpy as np with open('popu.txt','r') as fp: populations = fp.readlines() city = list() popu = list() for p in populations: cc, pp =p.split(',') city.append(cc) popu.append(int(pp)) ind = np.arange(len(city)) pt.bar(ind,popu) pt.xticks(ind+0.5,city) pt.title('program 13-2') pt.show()
结果
popu.txt
NTP,3971250 TP,2704974 TY,2108786 TC,2746112 TN,1885550 KS,2778729 YLC,458037