pandas 叶上的热图
问题描述
我有以下语法,无法实现我正在尝试的操作:
df_bedarf = pd.read_csv('C:/Users/xxxxxx/Desktop/xxxxx/xxxxxx.csv', sep = ";")
df_bedarf.head()
df_locations = df_bedarf[["Latitude", "Longitude"]]
location_list = df_locations.values.tolist()
location_list_size= len(location_list)
## Creating a Points Map
map_points = folium.Map(location = [47.57087, 13.43828], zoom_start = 7)
for point in range(0, location_list_size):
folium.Circle(
location = location_list[point],
popup=df_bedarf["Suburb"][point] + ": " + df_bedarf["Sort"][point],
radius = 10000, blur = 10000,
min_opacity =0.3,
gradient = {.3: "yellow", .6: "orange", 1: "red"},
max_zoom =1,
).add_to(map_points)
map_points
我希望渐变的颜色变得更红,t/a越高。
CSV头部如下:
Unnamed: 0 Suburb Sort t/a Latitude Longitude
0 0 Wien CC 2272 48.201900 16.370000
1 1 Graz LD 426 47.079675 15.420325
2 2 Feldbach LD 248 46.952187 15.888309
3 3 Zerlach RE 2041 46.944865 15.650902
4 4 Gnas SM 1488 46.874198 15.826138
相反,我的地图上只有蓝色圆圈。 我必须更改什么才能获得根据数量(t/a)改变颜色的圆形
提前感谢并致以问候
它应该看起来像这样:
解决方案
更改每个标记的颜色的最简单方法是对数值列使用bin分类功能,并为该数值设置所需的颜色名称。如果要增加颜色数,请增加存储箱和标签。
import pandas as pd
import numpy as np
import io
data = '''
Suburb Sort t/a Latitude Longitude
0 Wien CC 2272 48.201900 16.370000
1 Graz LD 426 47.079675 15.420325
2 Feldbach LD 248 46.952187 15.888309
3 Zerlach RE 2041 46.944865 15.650902
4 Gnas SM 1488 46.874198 15.826138
'''
df_locations = pd.read_csv(io.StringIO(data), delim_whitespace=True)
colors = pd.cut(df_locations['t/a'], bins=4, labels=['hotpink', 'tomato', 'red', 'crimson'])
colors = colors.to_list()
df_locations
Suburb Sort t/a Latitude Longitude marker_color
0 Wien CC 2272 48.201900 16.370000 crimson
1 Graz LD 426 47.079675 15.420325 hotpink
2 Feldbach LD 248 46.952187 15.888309 hotpink
3 Zerlach RE 2041 46.944865 15.650902 crimson
4 Gnas SM 1488 46.874198 15.826138 red
map_points = folium.Map(location = [47.57087, 13.43828], zoom_start = 7)
for idx, row in df_locations.iterrows():
folium.Circle(
location = [row['Latitude'], row['Longitude']],
popup=row["Suburb"] + ": " + row["Sort"],
radius = 10000,
# blur = 10000,
color=colors[idx],
fill_color=colors[idx],
# min_opacity=0.3,
# max_zoom=1,
).add_to(map_points)
map_points
相关文章