pyplot module of matplotlib Library
1. Draw line chart
plot(x,y)#x. Y can be an expression xlabel("title_name",fontsize=14)#Set x,y labels tick_params("both",labelsize=14)#Set both of x y scale axis([x_min,x_max,y_min,y_max])#Set axis range fill_between(x,y1,y2,facecolor='bule',alpha=0.1)#Fill two wire
2. scatter plot
scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolor='none',s=40,alpha=0.5)#cm method can set color mapping; edgcolor is contour color; c is scatter color; alpha is transparency savefig('fil_name.png',bbox_inches='tight') #Automatically save the generated map and cut off the blank axes().get_xaxis().set_visible(Fasle) #Hide axis figure(figsize=(10,6)) #Drawing window size
pygal Library
bar=Bar()#Bar class, creating bar instances bar.x_label=[] #Set image parameters bar.add(' Label ',frequencies) #Frequency is the input value bar.renger_to_file(' .svg') #Save results in svg format
CSV module
import csv with open(filename) as f: reader=csv.reader(f)#Create reader object header_row=next(reader)#Returns the next line of the file. The first call result is the header dates,highs=[],[] for row in reader: high=int(row[1]) date=datetime.strptime(row[0],"%Y-%m-%d")#Read the time line in CVS dates.append(date) highs.append(high)
Autofmt? Xdate()? Automatically adjust X-axis labels to avoid overlap
How to deal with data missing
1. Use try except to handle exceptions
2. Use continue
3.remove()
4.del
Json module
Json.dump (number, f obj) ා two arguments: the data to be stored nmber, and the file object f obj
json.load (f obj) loads the information in the. json file
Mapping the world's population
world_populations.py import pygal import pygal.maps.world from country_codes import get_country_code from pygal.style import RotateStyle filename='population_data.json' with open(filename) as f: pop_data=json.load(f) cc_populations={}#Create a dictionary with population for pop_dict in pop_data: if pop_dict['Year']=='2010': cuntry_name=pop_dict['Country Name'] population=int(float(pop_dict['Value'])) code=get_country_code(cuntry_name) if code: cc_populations[code]=population cc_pops_1,cc_pops_2,cc_pops_3={},{},{}#Group by number of people for cc,pop in cc_populations.items(): if pop<10000000: cc_pops_1[cc]=pop elif pop<1000000000: cc_pops_2[cc]=pop else: cc_pops_3[cc]=pop wm=pygal.maps.world.World()#Create a Worldmap instance wm_style=RotateStyle('#336699') wm=pygal.maps.world.World(style=wm_style)#pygal.Worldmap() does not exist wm.title='World Popolations in 2010' wm.add('0-10m',cc_pops_1) wm.add('10m-1bn',cc_pops_2 ) wm.add('>1bn',cc_pops_3 ) wm.render_to_file('world_population.svg') country_code.py from pygal.maps.world import COUNTRIES #pygal.i18n no longer exists, find country code def get_country_code(country_name): '''Return country code according to specified country''' for code,name in COUNTRIES.items():#COUNTRIES country name - country code pair if name==country_name: return code return None
Use API(requests package)
API web application interface for program interaction to request specific information using aggregate URL
requestPython interacts with websites
import requests import pygal # Make an API call, and store the response. url = 'https://api.github.com/search/repositories?q=language:python&sort=stars' r = requests.get(url)#Store response object in variable r print("Status code:", r.status_code)status_code Verify that the request was successful # Change the API response value. json format to Python dictionary format response_dict = r.json() print("Total repositories:", response_dict['total_count']) # Explore warehouse information repo_dicts = response_dict['items']#The items value is a list containing many dictionaries, each of which contains warehouse information names, plot_dicts = [], [] for repo_dict in repo_dicts: names.append(repo_dict['name']) plot_dict = { 'value': repo_dict['stargazers_count'], 'label': repo_dict['description'], 'xlink': repo_dict['html_url'], } plot_dicts.append(plot_dict)