使用Python调整Folium中的热图不透明度不使用CSS

问题描述

我仅使用Python,并且想调整热图的最大不透明度。如您在下图中所看到的,基本地图是非常不可见的。 我想查看更多底图。如何调整HeatMap中的参数,以便仅使用Python即可增加热图图层的不透明度?

This is the result



here中,这是完全相同的问题,但是它使用leaflet.js,解决方案使用CSS,而不是Python。无论如何,这是我用来制作地图的代码。

import folium
from folium import plugins
from folium.plugins import HeatMap
import pandas as pd

df = pd.read_csv("the_data.csv") # the data contains lat and long columns,I can't share the data...

coords = [(lat,lng) for lat,lng in zip(restaurants[0].lat.to_list(),restaurants[0].lon.to_list())]

m = folium.Map([-6.204725,106.847009],tiles="CartoDB dark_matter",zoom_start=14)

HeatMap(coords,min_opacity=0.4,max_val = 0.5,gradient={.2: '#482d61',.5:"#5aa7d6",1: '#fffaad'}).add_to(folium.FeatureGroup(name='Heat Map').add_to(m))
folium.LayerControl().add_to(m)

m.save("heatmap.html")

解决方法

您是否尝试过使用其他图块和其他渐变颜色?

如果需要城市细节,当前使用的平铺选项似乎没有用。建议您将'OpenStreetMap''Cartodb Positron'与常规的渐变颜色一起使用。

给出了类似的信息,您可以在其中看到一些城市信息:

m = folium.Map([48.,5.],tiles='OpenStreetMap',zoom_start=6)

HeatMap(data,min_opacity=0.4.add_to(m)

ExampleHeatMap

如果您想坚持使用正在使用的图块,我建议您使用blur参数。

例如默认值为blur = 15

m = folium.Map([48.,tiles='CartoDB dark_matter',zoom_start=6)
HeatMap(data,min_opacity=0.4,blur = 15,gradient={.2: '#482d61',.5:"#5aa7d6",1: '#fffaad'}).add_to(folium.FeatureGroup(name='Heat Map')).add_to(m)

enter image description here

带有blur = 30

的示例
m = folium.Map([48.,blur = 30,1: '#fffaad'}).add_to(folium.FeatureGroup(name='Heat Map')).add_to(m)

enter image description here

当然,模糊参数越大,解释数据就越困难。因为我不知道您的目标,所以我会让您决定最适合您的情况。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...