我有 3 个主要值(经度、纬度和速度)。使用 Folium 库,我可以用 lon 和 lat 度数映射位置。但现在我还想把速度和色标放在一起。例如,如果速度在 0-20 之间,那部分线为红色,如果速度在 20-60 之间,则为黄色,如果速度高于 60,则该线为绿色。可以在python中做到吗?有人可以帮我解决这个问题吗?我目前的代码是:
my_map = folium.Map(location=[ave_lat, ave_long], zoom_start=14)
folium.PolyLine(points, color="blue", weight=2.5, opacity=1).add_to(my_map)
my_map
这里的“点”是 lon 和 lat 对。但我的 csv 中也有速度列。我的输出是这样的。有人可以帮我解决这个问题吗?谢谢!
但我想为数据可视化添加速度列以获得这样的东西
最佳答案
我想我不妨添加我自己的答案,因为来自@GlobalTraveler 的答案涉及绘制许多线条,我认为这有点脏。
似乎在folium中确实没有选项可以做到这一点,但是您可以改为绘制多个标记,并单独为它们着色
import numpy as np
from matplotlib import cm
import folium
# rgb tuple to hexadecimal conversion
def rgb2hex(rgb):
rgb = [hex(int(256*x)) for x in rgb)]
r, g, b = [str(x)[2:] for x in rgb]
return "#{}{}{}".format(r, g, b)
# Defines the color mapping from speeds to rgba
color_mapper = cm.ScalarMappable(cmap=cm.cividis)
rgb_values = color_mapper.to_rgba(speeds)[:3] # keep rgb and drop the "a" column
colors = [rgb2hex(rgb) for rgb in rgb_values]
my_map = folium.Map(location=[ave_lat, ave_long], zoom_start=14)
for point, color, speed in zip(points, colors, speeds):
folium.CircleMarker(location=point,
radius=1.25,
popup=str(speed),
fill_color=color).add_to(my_map)
my_map
为此,您需要有一个数组
points
有 2 列和一个数组 speeds
与 points
一样多的行.请注意,您可以更改
cm.cividis
适合您需要的任何内容(请参阅引用 here)
关于python - 在 Python 中用色标可视化位置的移动速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53024723/