在大叶草中用地理数据框绘制彩色多边形 [英] Plot colored polygons with geodataframe in folium
问题描述
我正在尝试在大叶中绘制雷达数据,而我快到了.我遵循了这个示例(等高线图数据( ,lon,value)并导出GeoJSON ),以将我的数据转换为GeoJson格式.
I'm trying to plot radar data in folium, and I'm almost there. I followed this example (Contour plot data (lat,lon,value) within boundaries and export GeoJSON) to get my data into a GeoJson format.
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.
This outputs two columns: geometry and 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...
从那里制作我的叶子地图:
From there I make my folium map:
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)
这创建了我漂亮的叶片图,但是多边形根本没有上色.如何获得轮廓以正确的颜色填充?并修复不透明度?
This creates my nice folium map, but the polygons are not colored at all. How do I get the contours to be filled with the right colors? And fix the opacity?
推荐答案
我想我明白了.在我之前的代码中,polygon.get_facecolor()返回0-1范围内的RGBA值列表.我添加了此功能(从此帖子修改):
I think I figured it out. In my previous code, polygon.get_facecolor() returns a list of RGBA values ranging from 0-1. I added this function (modified from 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
将其转换为十六进制字符串.然后:
to convert it to a hex string. Then:
gdf['RGBA'] = convert_to_hex(colors)
然后绘制大叶中的颜色,我这样做:
Then to plot the colors in folium, I do:
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值,而是十六进制字符串.)
and that created a really nice looking plot! (The property name is a bit misleading - it's not actually RGBA values, but hex strings.)
这篇关于在大叶草中用地理数据框绘制彩色多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!