def conlanger_map(): foo = dict([(country.iso.lower(), country.count) for country in country_most_common()]) custom_style = Style( background='#fff', plot_background='#fff', foreground='#ffffff', foreground_light='#ffffff', foreground_dark='#ffffff', opacity='.6', opacity_hover='.9', transition='400ms ease-in', colors=('#527C3A', '#E8537A', '#E95355', '#E87653', '#E89B53') ) chart = Worldmap(style=custom_style) chart.no_prefix = True chart.disable_xml_declaration = True chart.show_legend = False chart.add('Conlangers', foo) return chart.render()
import pygal import pygal_maps_world #import base64 from pygal.maps.world import World #from ipywidgets import HTML from flask import Flask from flask import request print("1") if request.method == 'POST': print("1") countryCode = request.form['code'] else: print('No country code entered!') wm = World() wm.force_uri_protocol = 'http' wm.title = "Women Political leader distribution across countries" wm.add('Leaders', {'ca': 4, 'mx': 1, 'us': 6}) #wm.chart.render() wm.render_to_file('map.svg') #b64 = base64.b64encode(wm.render()) #src = 'data:image/svg+xml;charset=utf-8;base64,'+b64 #HTML('<embed src={}></embed>'.format(src))
filename = 'population_data.json' with open(filename) as f: pop_data = json.load(f) cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population cc1, cc2, cc3 = {}, {}, {} for cc, popu in cc_populations.items(): if popu < 10000000: cc1[cc] = popu elif popu < 1000000000: cc2[cc] = popu else: cc3[cc] = popu wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) wm = World(style=wm_style) wm.title = 'Population in world' wm.add('< 10m', cc1) wm.add('10m - 1b', cc2) wm.add('> 1b', cc3) wm.render_to_file('world_populations_new.svg')
from pygal.maps.world import World wm = World() wm.title = 'Populations of Countries in North America' wm.add("North America", {"ca":3412600, 'us': 309349000, 'mx': 113423000}) wm.render_to_file("C:\\Users\\Asymmetry\\Desktop\\na_populations.svg")
for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # Podzielenie państw na trzy grupy według liczebności populacji. cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} 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 # Wyświetlenie liczby państw w każdej z trzech grup. print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3)) wm_style = RS('#336699', base_style=LCS) wm = World(style=wm_style) wm.force_uri_protocol = 'http' wm.title = 'Populacja na świecie w 2010 roku (dane dla poszczególnych państw)' wm.add('0 - 10 mln', cc_pops_1) wm.add('10 mln - 1 mld', cc_pops_2) wm.add('>1 mld', cc_pops_3) wm.render_to_file('world_population.svg')
cc_gdp = {} for gdp_dict in gdp_data: if gdp_dict['Year'] == '2010': country_name = gdp_dict['Country Name'] gdp = int(float(gdp_dict['Value'])) code = get_country_code(country_name) if code: cc_gdp[code] = gdp # Group the countries into 3 gdp levels. cc_gdp_1, cc_gdp_2, cc_gdp_3 = {}, {}, {} for cc, gdp in cc_gdp.items(): if gdp < 1000000000: cc_gdp_1[cc] = gdp elif gdp < 10000000000000: cc_gdp_2[cc] = gdp else: cc_gdp_3[cc] = gdp # Plot and output to file wm_style = RS('#336699', base_style=LCS) wm = World(style=wm_style) wm.force_uri_protocol = 'http' wm.title = 'World GDP in 2010, by Country' wm.add('0-1bn', cc_gdp_1) wm.add('1bn-1tr', cc_gdp_2) wm.add('>1tr', cc_gdp_3) wm.render_to_file('world_gdp.svg')
# Open file and extract data filename = "life_female.csv" with open(filename) as f: lifedata = csv.reader(f) # Get to the proper column line on the csv for n in range(1, 6): next(lifedata) # Make a new dictionary. Translate the country names # to country_codes, and put them in as keys and the # life expectancy data as values (taken from the 2014 data set) lrates = {} for rowdata in list(lifedata): ccode = get_country_code(rowdata[0]) try: lrates[ccode] = float(rowdata[58]) except ValueError: continue # Plot new graph and output to file wm_style = RS('#336699', base_style=LCS) wm = World(style=wm_style) wm.force_uri_protocol = 'http' wm.title = 'Female Life Expectancy in 2014, by Country' wm.add('Years', lrates) wm.render_to_file('female_mortality.svg')
for gdp_dict in gdp_data: if gdp_dict['Year'] == 2016: country_name = gdp_dict['Country Name'] value = int(gdp_dict['Value']) code = get_country_code(country_name) if code: values[code] = value # Value levels. cc_value_1, cc_value_2, cc_value_3 = {}, {}, {} for c, v in values.items(): if v < 100000000000: cc_value_1[c] = v elif v < 1000000000000: cc_value_2[c] = v else: cc_value_3[c] = v # Prints numerical value of the three categories of GDP. print(len(cc_value_1), len(cc_value_2), len(cc_value_3)) wm = World() wm.title = "World Gross Domestic Product in 2016, by country" # Keeps value from being scientific notation. wm.value_formatter = lambda x: "{:,}".format(x) wm.add('< 100B', cc_value_1) wm.add('100B - 1T', cc_value_2) wm.add('> 1T', cc_value_3) wm.render_to_file('world_gdp_2016.svg')
print(country_name + ': missing_data') else: lf_data[country_name] = lf_value cc_lf = {} for country, value in lf_data.items(): code = get_country_code(country) if code: cc_lf[code] = value else: print('ERROR - ' + country_name) cc_lf_1, cc_lf_2, cc_lf_3 = {}, {}, {} for key, value in cc_lf.items(): if value < 5000000: cc_lf_1[key] = value elif value < 50000000: cc_lf_2[key] = value else: cc_lf_3[key] = value print(len(cc_lf_1), len(cc_lf_2), len(cc_lf_3)) wm_style = RotateStyle('#556677', base_style=LightColorizedStyle) wm = World(style=wm_style) wm.title = 'Labor force by Country (year: 2010)' wm.add('0-5m', cc_lf_1) wm.add('5m-50m', cc_lf_2) wm.add('>50m', cc_lf_3) wm.render_to_file('world_labor_force.svg')
from pygal.maps.world import World wm = World() wm.force_uri_protocol = 'http' wm.title = 'Wielkość populacji w krajach Ameryki Północnej' wm.add('Ameryka Północna', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')
cc_population={} for pop_dict in pop_date:#将每个字典存储在pop_dict中 if pop_dict['Year']=='2010': #for循环是一次操作一个值 country_name=pop_dict['Country Name'] population=int(float(pop_dict['Value'])) code=get_country_code(country_name) if code: cc_population[code]=population #字典的赋值 else: print("ERROR"+":"+country_name) for cc,pop in cc_population.items(): if pop<100000000: cc_pop1[cc]=pop elif pop<1000000000: cc_pop2[cc]=pop else: cc_pop3[cc]=pop wm_style=RotateStyle('#336699',base_style=LightColorizedStyle) wm=World(style=wm_style) wm.title="World Population in 2010,by Country" wm.add('0-10m',cc_pop1) wm.add('10m-1bn',cc_pop2) wm.add('>1bn',cc_pop3) wm.render_to_file('word_population.svg')
import json from country_codes import get_country_code from pygal.maps.world import World from pygal.style import RotateStyle # Wczytanie danych i umieszczenie ich na liście. filename = 'gdp_json.json' with open(filename) as f: pop_data = json.load(f) # Utworzenie słownika danych dotyczących populacji. cc_populations = {} for pop_dict in pop_data: if str(pop_dict['Year']) == '2010': country_name = pop_dict['Country Name'] population = pop_dict['Value'] code = get_country_code(country_name) if code: cc_populations[code] = float(population) print(cc_populations) wm_style = RotateStyle('#336699') wm = World(style=wm_style) wm.force_uri_protocol = 'http' wm.value_formatter = lambda x: "%.2f" % x wm.add('GDP', cc_populations) wm.render_to_file('world_gdp.svg')
from pygal.maps.world import World wm = World() wm.force_uri_protocol = 'http' wm.title = 'North, Central, and South America' wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 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('americas.svg')
import pygal from pygal.maps.world import World wm = World() wm.title = "North, Central and South America" wm.add("North America", ['ca', 'mx', 'us']) wm.add("Central America", ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add("South America", ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf',f 'gy', 'pe', 'py', 'sr', 'uy', 've']) wm.render_to_file('americas.svg')
gdp = int(float(pop_dict['Value'])) code = get_country_code(country) if code: cc_gdps[code] = gdp else: print('Error - ', country ) # Group the countries into 3 gdp levels. # Less than 5 billion, less than 50 billion, >= 50 billion. # Also, convert to billions for displaying values. cc_gdps_1, cc_gdps_2, cc_gdps_3 = {}, {}, {} for cc, gdp in cc_gdps.items(): if gdp < 5000000000: cc_gdps_1[cc] = round(gdp / 1000000000) elif gdp < 50000000000: cc_gdps_2[cc] = round(gdp / 1000000000) else: cc_gdps_3[cc] = round(gdp / 1000000000) # See how many countries are in each level. print(len(cc_gdps_1), len(cc_gdps_2), len(cc_gdps_3)) wm_style = RS('#336699', base_style=LCS) wm = World(style=wm_style) wm.title = 'Global GDP in 2014, by Country (in billions USD)' wm.add('0-5bn', cc_gdps_1) wm.add('5bn-50bn', cc_gdps_2) wm.add('>50bn', cc_gdps_3) wm.render_to_file('global_gdp.svg')
def get_country_code(country_name): for code, name in COUNTRIES.items(): if name == country_name: return code return None gdps = r.json() gdps_lys = [] for gdp in gdps: if len(gdps_lys) == 0: gdps_lys.append({'name': gdp['Country Name'], 'year': gdp['Year'],'value': gdp['Value']}) else: if gdps_lys[-1]['name'] == gdp['Country Name']: gdps_lys[-1]['year'] = gdp['Year'] gdps_lys[-1]['value'] = gdp['Value'] else: gdps_lys.append({'name': gdp['Country Name'], 'year': gdp['Year'],'value': gdp['Value']}) countries = {} ans = 0 for gdp in gdps_lys: code = get_country_code(gdp['name']) if code: countries[code] = gdp['value']*1000000000 else: print(gdp['name']) print (ans) wm = World() wm.title = 'Gpd of counries' wm.add('',countries) wm.render_to_file('Gdps.svg')
from pygal.maps.world import World wm = World() wm.title = 'Poppulations of Countries in North America' wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')
from pygal.maps.world import World wm = World() wm.title = 'North, Central, and South America' wm.add('North America', ['ca', 'mx', 'us']) 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('americas.svg')
# Group the countries into 3 IMF-credit levels; # this time, we need a separate "n/a" level for countries # where values were unavailable. imf_credit_n, imf_credit_1, imf_credit_2, imf_credit_3 = {}, {}, {}, {} for cc, imf_credit in cc_imfcredits.items(): # none_to_zero needed because pygal skips None's, when plotting if imf_credit == None: imf_credit_n[cc] = none_to_zero(imf_credit) elif imf_credit < 1000000000: # x < 1B imf_credit_1[cc] = imf_credit elif imf_credit < 10000000000: # 1B <= x < 10B imf_credit_2[cc] = imf_credit else: # 10B <= x imf_credit_3[cc] = imf_credit # See how many countries are in each level. print(len(imf_credit_n), len(imf_credit_1), len(imf_credit_2), len(imf_credit_3)) # Plot the data. wm_style = RS('#336699') wm = World(style=wm_style) wm.title = 'Use of IMF Credit in 2010 (in USD), by Country' wm.add('n/a', imf_credit_n) wm.add('0-1bn', imf_credit_1) wm.add('1bn-10bn', imf_credit_2) wm.add('>10bn', imf_credit_3) wm.render_to_file('use_of_imf_credit.svg')
from pygal.maps.world import World wm = World() wm.force_uri_protocol = 'http' wm.title = ('Wielkość populacji w krajach Ameryki Północnej') wm.add('Ameryka Północna', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')
for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # Group the countries into 3 population levels cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} 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 # See how many countries are in each level # print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3)) wm_style = RS('#336699', base_style=LCS) wm = World(style=wm_style) wm.title = 'World Population in 2010, by Country' wm.add('0-10m', cc_pops_1) wm.add('10-1bn', cc_pops_2) wm.add('>1n', cc_pops_3) wm.add('2010', cc_populations) wm.render_to_file('world_population.svg')
country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # Group the countries into 3 poplulation levels. cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} 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 # See how many countries are in each level. print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3)) wm = World() wm_style =RotateStyle('#336699') wm = World(style=wm_style) wm.title = 'World Population in 2010, by Country' wm.add('0-10m', cc_pops_1) wm.add('10m-1b', cc_pops_2) wm.add('>1b', cc_pops_3) wm.render_to_file('world_population.svg')
#print(country_name+": "+str(population)) if code: #print(code+": "+str(population)) cc_poplations[code] = population else: print('ERROR - ' + country_name) #Agrupa países em três níveis populacionais cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_poplations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop # Vê quantos países estão em cada nível print(len(cc_pops_1), len(cc_pops_1), len(cc_pops_3)) wm_style = RotateStyle('#336699') wm = World(style=wm_style) wm.title = 'World Population in 2010, by Country' #wm.add('2010', cc_poplations) 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')
from pygal.maps.world import World wm = World() wm.force_uri_protocol = 'http' wm.title = 'Populations of Countries in North America' wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')
# if code is not None, add the data to the dictionary # print(countryName + ": " + '{:,}'.format(gdpValue)) if code: ccGdp[code] = gdpValue # to distinguish the countries from each other, we will have to put them in # separate dictionaries to plot them separately ccGdp1, ccGdp2, ccGdp3 = {}, {}, {} # showing results in milliards is IMO better to see the real difference # between the countries for code, val in ccGdp.items(): if val < 500000000000: ccGdp1[code] = round(val / 1000000000) elif val < 5000000000000: ccGdp2[code] = round(val / 1000000000) else: ccGdp3[code] = round(val / 1000000000) '''print("over 5,000 mld countries: " + str(len(ccGdp3))) print("500 - 5,000 mld countries: " + str(len(ccGdp2))) print("below 500 mld countries: " + str(len(ccGdp1)))''' # Create a map and fill it with data wm = World(style=CleanStyle) wm.title = "Countries GDP, 2016" wm.add('over 5,000 mld', ccGdp3) wm.add("500 - 5,000 mld", ccGdp2) wm.add("below 500 mld", ccGdp1) wm.render_to_file('world_gdp.svg')
# json.load() converts the data into a format Python can work with pop_data = json.load(f) # build a dictionary of population data cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # group the countries into 3 population levels cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 10000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop wm = World() wm.force_uri_protocol = 'http' wm.title = 'World Population in 2010, by Country' wm.add('0-10,', cc_pops_1) wm.add('10m-1bn', cc_pops_2) wm.add('>1bn', cc_pops_3) wm.render_to_file('world_population.svg')
gdp_populations = {} for gdp_dict in gdp_data: if gdp_dict['Year'] == 2016: country_name = gdp_dict['Country Name'] gdp_value = float(gdp_dict['Value']) code = get_country_code(country_name) if code: gdp_populations[code] = gdp_value # Group the countries into 3 population levels. gdp_pops_1, gdp_pops_2, gdp_pops_3 = {}, {}, {} for cc, gdp in gdp_populations.items(): if gdp < 100000000000: gdp_pops_1[cc] = gdp elif gdp < 10000000000000: gdp_pops_2[cc] = gdp else: gdp_pops_3[cc] = gdp # See how many countries are in each level. print(len(gdp_pops_1), len(gdp_pops_2), len(gdp_pops_3)) wm_style = RotateStyle('#FF0000', base_style=LightColorizedStyle) wm = World(style=wm_style) wm.title = 'World GDP in 2016, by Country' wm.add('0-100bn', gdp_pops_1) wm.add('100bn-10tn', gdp_pops_2) wm.add('>10tn', gdp_pops_3) wm.render_to_file('world_gdp.svg')
# from pygal.maps.world import World wm = World() wm.title = 'North, Central, and South America' wm.add('North America', ['ca', 'mx', 'us']) 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('americas.svg')
from pygal.maps.world import World wm = World() wm.force_uri_protocol = 'http' wm.title = 'Ameryka Północna, Środkowa i Południowa' wm.add('Ameryka Północna', ['ca', 'mx', 'us']) wm.add('Ameryka Środkowa', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add( 'Ameryka Południowa', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'sr', 'uy', 've']) wm.render_to_file('americas.svg')