Python代码错误:应用style_function方法更改颜色后,编译器给我一个错误

时间:2019-02-04 11:09:05

标签: python-3.x lambda folium geopy

我正在使用folium库。我想将地图第二层的bgcolor更改为“黄色”。

我已经尝试了下面的代码,一切似乎都还可以,但是我认为这是.read()或诸如此类的问题:

import folium
import pandas


data = pandas.read_csv("Volcanoes.txt")
lat = list(data['LAT'])
lon = list(data['LON'])
elev = list(data['ELEV'])


def high_color(elevation):
    if elevation < 1100:
        return "green"
    elif 1100 <= elevation < 3000:
        return 'orange'
    else:
        return 'red'
notmap = folium.Map(location=[40.954898799999995, -121.3610001], zoom_start=5)

feature_group = folium.FeatureGroup(name="My Map")

for lt, ln, el in zip(lat, lon, elev):
    feature_group.add_child(folium.CircleMarker(location=[lt, ln], popup = str(el)+" m.",
    radius=8, fill_color = high_color(el), color = 'grey', fill = True, fill_opacity = 0.7))

feature_group.add_child(folium.GeoJson(data = open('world.json', 'r', encoding='utf-8-sig'),
style_function = lambda x: {'fillColor':'yellow'}.read()))


notmap.add_child(feature_group)
notmap.save("map1.html")


Traceback (most recent call last):
  File "map.py", line 27, in <module>
    style_function = lambda x: {'fillColor':'yellow'}.read()))
  File "/home/john/.local/lib/python3.6/site-packages/folium/features.py", line 418, in __init__
    raise ValueError('Unhandled object {!r}.'.format(data))
ValueError: Unhandled object <_io.TextIOWrapper name='world.json' mode='r' encoding='utf-8-sig'>.

1 个答案:

答案 0 :(得分:0)

[已解决] 我只是更改.read()方法的位置。

以下行应如下所示:

feature_group.add_child(folium.GeoJson(data = open('world.json', 'r', encoding='utf-8-sig').read(), style_function = lambda x: {'fillColor':'yellow'}))