《Python 程式設計:從入門到實踐》第十六章(下載資料)練習題答案
阿新 • • 發佈:2018-12-11
import csv from datetime import datetime from matplotlib import pyplot as plt filename = 'sitka_weather_07-2014.csv' with open(filename) as f: reader = csv.reader(f) header_row = next(reader) #print(header_row) #for index,column_header in enumerate(header_row): # print(index,column_header) dates,highs = [],[] for row in reader: current_date = datetime.strptime(row[0],'%Y-%m-%d') dates.append(current_date) high = int(row[1]) highs.append(high) #print(highs) fig = plt.figure(dpi=128,figsize=(10,6)) plt.plot(dates,highs,c='red') plt.title('Daily high temperatures,July 2014',fontsize = 24) plt.xlabel('',fontsize=16) fig.autofmt_xdate() plt.ylabel('Temperature(F)',fontsize = 16) plt.tick_params(axis='both',which='major',labelsize=16) plt.show() ———————————————————————————————————————————————— #高低溫 # -*- coding: gbk -*- import csv from datetime import datetime from matplotlib import pyplot as plt filename = 'sitka_weather_2014.csv' with open(filename) as f: reader = csv.reader(f) header_row = next(reader) #print(header_row) #for index,column_header in enumerate(header_row): # print(index,column_header) dates,highs,lows = [],[],[] for row in reader: current_date = datetime.strptime(row[0],'%Y-%m-%d') dates.append(current_date) high = int(row[1]) highs.append(high) low = int(row[3]) lows.append(low) #print(highs) fig = plt.figure(dpi=128,figsize=(10,6)) plt.plot(dates,highs,c='red',alpha = 0.5) plt.plot(dates,lows,c='blue',alpha = 0.5) plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.1) plt.title('Daily high and low temperatures- 2014',fontsize = 24) plt.xlabel('',fontsize=16) fig.autofmt_xdate() plt.ylabel('Temperature(F)',fontsize = 16) plt.tick_params(axis='both',which='major',labelsize=16) plt.show() —————————————————————————————————————————————————————————————————— import csv from datetime import datetime from matplotlib import pyplot as plt filename = 'sitka_weather_2014.csv' with open(filename) as f: reader = csv.reader(f) header_row = next(reader) #print(header_row) #for index,column_header in enumerate(header_row): # print(index,column_header) dates,highs,lows = [],[],[] for row in reader: try: current_date = datetime.strptime(row[0],'%Y-%m-%d') high = int(row[1]) low = int(row[3]) except ValueError: print(current_date,'missing data') else: dates.append(current_date) highs.append(high) lows.append(low) #print(highs) fig = plt.figure(dpi=128,figsize=(10,6)) plt.plot(dates,highs,c='red',alpha = 0.5) plt.plot(dates,lows,c='blue',alpha = 0.5) plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.1) plt.title('Daily high and low temperatures- 2014 Sitka',fontsize = 24) plt.xlabel('',fontsize=16) fig.autofmt_xdate() plt.ylabel('Temperature(F)',fontsize = 16) plt.tick_params(axis='both',which='major',labelsize=16) plt.show() ———————————————————————————————————————————————————————————— import json from pygal_maps_world.i18n import COUNTRIES #for country_code in sorted(COUNTRIES.keys()): # print(country_code,COUNTRIES[country_code]) def get_country_code(country_name): for code,name in COUNTRIES.items(): if name == country_name: return code return None filename = 'population_data.json' with open(filename) as f: pop_data = json.load(f) for pop_dict in pop_data: if pop_dict['Year'] == '2010': region = pop_dict['Country Name'] population = pop_dict['Value'] #print(region+":"+ str(population)) code = get_country_code(region) if code: print(code +":"+ str(population)) else: print('ERROR - '+region) ___________________________________ # -*- coding: gbk -*- import pygal import json from pygal_maps_world.i18n import COUNTRIES from pygal.style import RotateStyle, LightColorizedStyle def get_country_code(country_name): for code,name in COUNTRIES.items(): if name == country_name: return code return None filename = 'population_data.json' with open(filename) as f: pop_data = json.load(f) cc_popuations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': region = pop_dict['Country Name'] population = pop_dict['Value'] code = get_country_code(region) if code: cc_popuations[code]= population cc_pops_1,cc_pops_2,cc_pops_3 = {},{},{} for cc,pop in cc_popuations.items(): if int(pop)<10000000: cc_pops_1[cc] = pop elif int(pop)<1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3)) #wm_style = RotateStyle('#336699') #wm_style = LightColorizedStyle wm_style = RotateStyle('#336699',base_style=LightColorizedStyle) wm = pygal.maps.world.World(style = wm_style) wm.title = 'Worlds Population in 2010' wm.add('0-10m',cc_pops_1) wm.add('10m-1bn',cc_pops_2) wm.add('>1bn',cc_pops_3) #wm.add('2010',cc_popuations) #wm.add('North America', {'ca':34126000, 'mx':309349000, 'us':113423000}) #wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) #wm.add('South America', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', #'gy', 'pe', 'py', 'sr','uy','ve']) wm.render_to_file('world_pupulations.svg')