在 folium 中使用地理数据框绘制彩色多边形
Plot colored polygons with geodataframe in folium
我正在尝试在 folium 中绘制雷达数据,我快完成了。我按照此示例 () 将我的数据转换为 GeoJson 格式。
nb_class = 20
collec_poly = plt.contourf(lons,lats,np.array(poshdata), nb_class,alpha=0.5)
gdf = collec_to_gdf(collec_poly) # From link above
gdf.to_json()
colors = [p.get_facecolor().tolist()[0] for p in collec_poly.collections]
gdf['RGBA'] = colors
gdf
这会输出两列:几何和 RGBA。
RGBA geometry
0 [0.0, 0.0, 0.713903743316, 1.0] (POLYGON ((-71.57032079644679 42.2775236331535...
1 [0.0, 0.0960784313725, 1.0, 1.0] (POLYGON ((-71.56719970703125 42.2721176147460...
2 [0.0, 0.503921568627, 1.0, 1.0] (POLYGON ((-71.55678558349609 42.2721176147460...
3 [0.0, 0.896078431373, 0.970904490829, 1.0] (POLYGON ((-71.52552795410156 42.2849182620049...
4 [0.325743200506, 1.0, 0.641998734978, 1.0] (POLYGON ((-71.49427795410156 42.2939676156927...
5 [0.641998734978, 1.0, 0.325743200506, 1.0] (POLYGON ((-71.47344207763672 42.3003084448852...
6 [0.970904490829, 0.959331880901, 0.0, 1.0] (POLYGON ((-71.26508331298828 42.3200411822557...
7 [1.0, 0.581699346405, 0.0, 1.0] (POLYGON ((-71.15048217773438 42.3333218460720...
从那里我制作了我的 folium 地图:
import folium
# Picked location between Sudbury and Somerville:
maploc = folium.Map(location=[42.377157,-71.236088],zoom_start=11,tiles="Stamen Toner")
folium.GeoJson(gdf).add_to(maploc)
这创建了我漂亮的 folium 地图,但多边形根本没有着色。如何让轮廓填充正确的颜色?并修复不透明度?
不是专家...我刚开始使用 folium 和 jupyter,遇到了类似的问题,但有线条。
你说你有 GeoJson 和多边形,颜色包含在我假设的 json 中。
style_function 可能会帮助您获得想要的东西?
下面的例子是用这个页面制作的:http://geojson.io/
我所要做的就是 "mapping" 和 style_function。
也可以使用自定义函数,请参阅:
https://github.com/python-visualization/folium/blob/master/examples/Colormaps.ipynb
import folium
geoJsonData = {
"features": [
{
"geometry": {
"coordinates": [
[
12.98583984375,
56.70450561416937
],
[
14.589843749999998,
57.604221411628735
],
[
13.590087890625,
58.15331598640629
],
[
11.953125,
57.955674494979526
],
[
11.810302734375,
58.76250326278713
]
],
"type": "LineString"
},
"properties": {
"stroke": "#fc1717",
"stroke-opacity": 1,
"stroke-width": 2
},
"type": "Feature"
},
{
"geometry": {
"coordinates": [
[
14.9468994140625,
57.7569377956732
],
[
15.078735351562498,
58.06916140721414
],
[
15.4302978515625,
58.09820267068277
],
[
15.281982421875002,
58.318144965188246
],
[
15.4852294921875,
58.36427519285588
]
],
"type": "LineString"
},
"properties": {
"stroke": "#1f1a95",
"stroke-opacity": 1,
"stroke-width": 2
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
m = folium.Map(location=[ 56.7, 12.9], zoom_start=6)
folium.GeoJson(geoJsonData,
style_function=lambda x: {
'color' : x['properties']['stroke'],
'weight' : x['properties']['stroke-width'],
'opacity': 0.6,
'fillColor' : x['properties']['fill'],
}).add_to(m)
m
git hub 上的 folium 源代码也包含几个不错的示例:
https://github.com/python-visualization/folium/tree/master/examples
在这里您可以找到要玩的选项:
http://leafletjs.com/reference.html#path-options
希望这能给你带来进步!
我想我明白了。在我之前的代码中,polygon.get_facecolor() returns 一个 RGBA 值列表,范围从 0-1。我添加了这个函数(修改自 this post):
def convert_to_hex(rgba_color) :
red = str(hex(int(rgba_color[0]*255)))[2:].capitalize()
green = str(hex(int(rgba_color[1]*255)))[2:].capitalize()
blue = str(hex(int(rgba_color[2]*255)))[2:].capitalize()
if blue=='0':
blue = '00'
if red=='0':
red = '00'
if green=='0':
green='00'
return '#'+ red + green + blue
将其转换为十六进制字符串。那么:
gdf['RGBA'] = convert_to_hex(colors)
然后为了在 folium 中绘制颜色,我这样做:
maploc = folium.Map(location=[42.377157,-71.236088],zoom_start=10,tiles="Stamen Toner")
colors = []
folium.GeoJson(
gdf,
style_function=lambda feature: {
'fillColor': feature['properties']['RGBA'],
'color' : feature['properties']['RGBA'],
'weight' : 1,
'fillOpacity' : 0.5,
}
).add_to(maploc)
这创造了一个非常好看的情节! (属性 名称有点误导 - 它实际上不是 RGBA 值,而是十六进制字符串。)
我还没有足够的声誉点数来评论,所以这是一个单独的答案来澄清 edub 所接受的答案。
Matplotlib 已经有了 colors.to_hex() 方法:
import matplotlib.colors as cl
colors = [cl.to_hex(c) for c in colors]
这将取代已接受答案中的 convert_to_hex() 方法。
我正在尝试在 folium 中绘制雷达数据,我快完成了。我按照此示例 (
nb_class = 20
collec_poly = plt.contourf(lons,lats,np.array(poshdata), nb_class,alpha=0.5)
gdf = collec_to_gdf(collec_poly) # From link above
gdf.to_json()
colors = [p.get_facecolor().tolist()[0] for p in collec_poly.collections]
gdf['RGBA'] = colors
gdf
这会输出两列:几何和 RGBA。
RGBA geometry
0 [0.0, 0.0, 0.713903743316, 1.0] (POLYGON ((-71.57032079644679 42.2775236331535...
1 [0.0, 0.0960784313725, 1.0, 1.0] (POLYGON ((-71.56719970703125 42.2721176147460...
2 [0.0, 0.503921568627, 1.0, 1.0] (POLYGON ((-71.55678558349609 42.2721176147460...
3 [0.0, 0.896078431373, 0.970904490829, 1.0] (POLYGON ((-71.52552795410156 42.2849182620049...
4 [0.325743200506, 1.0, 0.641998734978, 1.0] (POLYGON ((-71.49427795410156 42.2939676156927...
5 [0.641998734978, 1.0, 0.325743200506, 1.0] (POLYGON ((-71.47344207763672 42.3003084448852...
6 [0.970904490829, 0.959331880901, 0.0, 1.0] (POLYGON ((-71.26508331298828 42.3200411822557...
7 [1.0, 0.581699346405, 0.0, 1.0] (POLYGON ((-71.15048217773438 42.3333218460720...
从那里我制作了我的 folium 地图:
import folium
# Picked location between Sudbury and Somerville:
maploc = folium.Map(location=[42.377157,-71.236088],zoom_start=11,tiles="Stamen Toner")
folium.GeoJson(gdf).add_to(maploc)
这创建了我漂亮的 folium 地图,但多边形根本没有着色。如何让轮廓填充正确的颜色?并修复不透明度?
不是专家...我刚开始使用 folium 和 jupyter,遇到了类似的问题,但有线条。 你说你有 GeoJson 和多边形,颜色包含在我假设的 json 中。
style_function 可能会帮助您获得想要的东西?
下面的例子是用这个页面制作的:http://geojson.io/ 我所要做的就是 "mapping" 和 style_function。 也可以使用自定义函数,请参阅: https://github.com/python-visualization/folium/blob/master/examples/Colormaps.ipynb
import folium
geoJsonData = {
"features": [
{
"geometry": {
"coordinates": [
[
12.98583984375,
56.70450561416937
],
[
14.589843749999998,
57.604221411628735
],
[
13.590087890625,
58.15331598640629
],
[
11.953125,
57.955674494979526
],
[
11.810302734375,
58.76250326278713
]
],
"type": "LineString"
},
"properties": {
"stroke": "#fc1717",
"stroke-opacity": 1,
"stroke-width": 2
},
"type": "Feature"
},
{
"geometry": {
"coordinates": [
[
14.9468994140625,
57.7569377956732
],
[
15.078735351562498,
58.06916140721414
],
[
15.4302978515625,
58.09820267068277
],
[
15.281982421875002,
58.318144965188246
],
[
15.4852294921875,
58.36427519285588
]
],
"type": "LineString"
},
"properties": {
"stroke": "#1f1a95",
"stroke-opacity": 1,
"stroke-width": 2
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
m = folium.Map(location=[ 56.7, 12.9], zoom_start=6)
folium.GeoJson(geoJsonData,
style_function=lambda x: {
'color' : x['properties']['stroke'],
'weight' : x['properties']['stroke-width'],
'opacity': 0.6,
'fillColor' : x['properties']['fill'],
}).add_to(m)
m
git hub 上的 folium 源代码也包含几个不错的示例:
https://github.com/python-visualization/folium/tree/master/examples
在这里您可以找到要玩的选项:
http://leafletjs.com/reference.html#path-options
希望这能给你带来进步!
我想我明白了。在我之前的代码中,polygon.get_facecolor() returns 一个 RGBA 值列表,范围从 0-1。我添加了这个函数(修改自 this post):
def convert_to_hex(rgba_color) :
red = str(hex(int(rgba_color[0]*255)))[2:].capitalize()
green = str(hex(int(rgba_color[1]*255)))[2:].capitalize()
blue = str(hex(int(rgba_color[2]*255)))[2:].capitalize()
if blue=='0':
blue = '00'
if red=='0':
red = '00'
if green=='0':
green='00'
return '#'+ red + green + blue
将其转换为十六进制字符串。那么:
gdf['RGBA'] = convert_to_hex(colors)
然后为了在 folium 中绘制颜色,我这样做:
maploc = folium.Map(location=[42.377157,-71.236088],zoom_start=10,tiles="Stamen Toner")
colors = []
folium.GeoJson(
gdf,
style_function=lambda feature: {
'fillColor': feature['properties']['RGBA'],
'color' : feature['properties']['RGBA'],
'weight' : 1,
'fillOpacity' : 0.5,
}
).add_to(maploc)
这创造了一个非常好看的情节! (属性 名称有点误导 - 它实际上不是 RGBA 值,而是十六进制字符串。)
我还没有足够的声誉点数来评论,所以这是一个单独的答案来澄清 edub 所接受的答案。
Matplotlib 已经有了 colors.to_hex() 方法:
import matplotlib.colors as cl
colors = [cl.to_hex(c) for c in colors]
这将取代已接受答案中的 convert_to_hex() 方法。