Folium自定义弹出窗口

时间:2017-03-13 15:34:14

标签: python folium

使用Folium 0.3.0运行Python 3.5.2。

每当我点击具有数据的特定国家/地区时,我都会尝试创建自定义弹出窗口。我设法让弹出窗口出现,但我迷失了将值动态传递给它们。我熟悉Python,但对JSON等很新。

我从拥有一个Series对象final_dict开始。此外,我有一个国家多边形数据集countries_reduced.json。在example found here之后,我破解了folium.GeoJson模板,当我将鼠标悬停在包含数据的国家/地区时,包括突出显示,并在我点击国家/地区时显示弹出窗口。弹出窗口本身显示得很好,但到目前为止,我只是设法传递来自final_dict某些值(例如final_dict[0] all 这些国家,如行

var html_aaa = $('<div id="html_aaa" style="width: 100.0%; height: 100.0%;">"""+ "{0:.2f}%".format(final_dict[0] * 100) + """</div>')[0];

在下面的代码段中。例如,如图here所示,美国的弹出窗口显示为-0.02%,但它应显示为0.18%

如何将与点击后的国家/地区相对应的值动态传递到弹出窗口中?

我的代码如下:

import pandas as pd
import numpy as np

# Data
array = np.array([-0.000247 , 0.00178 , -0.0183 , 0.00831 , 0.0135 , -0.00266 , 0.00461])
final_dict = pd.Series(array, index=['JPN', 'USA', 'ARG', 'DEU', 'SWE', 'FIN' , 'FRA'])

# Create map
import json
import folium
geo_json_data = json.load(open('countries_reduced.json'))
linear = folium.LinearColormap(['red','yellow','green'], vmin=-0.02, vmax=0.02)
linear.to_step(21)

m = folium.Map([0,0], tiles='Mapbox Bright', zoom_start=2)
g = folium.GeoJson(
    geo_json_data,
    style_function=lambda feature: {
        'fillColor': linear(final_dict[feature['id']]),                    
        'color' : 'black',
        'weight' : 2,
        'dashArray' : '5, 5'
        }, 
    ).add_to(m)

 # Following example provided here: https://github.com/python-visualization/folium/issues/341,
 # I overwrite the g_Template 
from jinja2 import Template    
g._template = Template("""
            {% macro script(this, kwargs) %}
               var {{this.get_name()}} = {};                
               var popup_aaa = L.popup({maxWidth: '300'});
               var html_aaa = $('<div id="html_aaa" style="width: 100.0%; height: 100.0%;">"""+ "{0:.2f}%".format(final_dict[0] * 100) + """</div>')[0];
               popup_aaa.setContent(html_aaa);                

                {{this.get_name()}}.style = function(feature) {return feature.properties.style;};

                {{this.get_name()}}.highlightStyle = function(feature) {return {
                        weight: 5,
                        color: '#666',
                        dashArray: '',
                        fillOpacity: 0.7
                        };
                    };
                {{this.get_name()}}.onEachFeature = function onEachFeature(feature, layer) {
                    layer.on({
                        mouseover: function(e) {
                            e.target.setStyle({{this.get_name()}}.highlightStyle(e.target.feature));
                            e.target.bindPopup(popup_aaa);},
                        mouseout: function(e) {
                            {{this.get_name()}}.geoJson.resetStyle(e.target);
                            }
                        });
                    };
                {{this.get_name()}}.geoJson = L.geoJson(
                    {% if this.embed %}{{this.style_data()}}{% else %}"{{this.data}}"{% endif %},{
                        style : {{this.get_name()}}.style,
                        onEachFeature: {{this.get_name()}}.onEachFeature
                        })
                    .addTo({{this._parent.get_name()}});
            {% endmacro %}
""")    

# And draw the map with additions, save
m.add_child(g)
m.add_child(linear)
m.save('map4.html') 

1 个答案:

答案 0 :(得分:1)

您在final_dict[0]中使用的索引适用于日本,而非美国。

确保您可以将该行更改为final_dict['USA']

相关问题