正如标题所示,我很难看到使用哪个函数来使用Python中的Folium来突出显示“mouseover”函数中的每个国家/地区。 我有一个功能性的Choropleth地图,目前有一些使用Plot.ly的功能。
以正确的方式提供任何帮助或指示将非常感激=)
源代码如下,对于我一直在使用Jupyter Notebook的项目:
import urllib.request, json
import json
import numpy as np
#import pycountry as pc
import pandas as pd
import csv
import itertools
import plotly.plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)
#EXTRACTING DATA AND CONVERTING IT TO A 1-DIMENSIONAL ARRAY
with urllib.request.urlopen("http://api.population.io:80/1.0/countries") as url:
slash = "/"
api_data = json.loads(url.read().decode())
twoD_data = np.array(list(api_data.values())) #turns the dictionary into a 2D array
all_countries = twoD_data.ravel()
country_ready_for_fetch = []
#FORMATTING COUNTRIES FOR URL-CALLS LATER
country_and_countryCodes_csv = pd.read_csv("http://kejser.org/wp-content/uploads/2014/06/Country.csv", delimiter='|')
country_and_countryCodes = country_and_countryCodes_csv[["CountryName","Alpha3Code"]]
chained_country_list = set(itertools.chain.from_iterable(country_and_countrycodes)) & set(all_countries)
for country in chained_country_list:
try:
if '/' not in country:
j = country.replace(" ","%20")
country_ready_for_fetch.append(j)
except AttributeError:
pass
#EXTRACTING THE POPULATION DATA
year = "/2013"
date = year + '-01-01'
country_population = []
for country in country_ready_for_fetch:
url = "http://api.population.io:80/1.0/population/"
with urllib.request.urlopen(url + country + date) as url:
data = json.loads(url.read().decode())
country_population.append(country)
country_population.append(data)
result = {}
for i in range(0, len(country_population), 2):
result[country_population[i]] = country_population[i+1]['total_population']['population']
result = {key.replace('%20', ' '): value for key, value in result.items()}
l = np.array(country_and_countrycodes).tolist()
for i in l:
i.append(result.get(i[0]))
df = pd.DataFrame(l)
df.columns = ['Country','CountryCode','Population']
df['CountryCode'] = df['CountryCode'].str.upper()
data = dict(type = 'choropleth',
locations = df['CountryCode'],
z = df['Population'],
text = df['Country'],
# colorscale ='Portland',
colorbar = dict(title = 'Population in thousands'))
layout = dict(title = '2013 global Population',
geo = dict(showframe = False,
showlakes = True,
showrivers = True,
showcoastlines = True,
coastlinewidth = 6,
coastlinecolor = 'rgb(127,255,0)',
rivercolor = 'rgb(85,173,240)',
lakecolor = 'rgb(85,173,240)',
projection = {'type': 'stereographic'}))
choromap3 = go.Figure(data = [data], layout = layout)
plot(choromap3)