簡體   English   中英

在 Python 中使用顏色標度可視化移動速度

[英]Vizualizing speed of movement with color scale for location in Python

我有 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 中確實沒有選項可以做到這一點,但是您可以改為繪制多個標記,並分別為它們着色

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 f"#{r}{g}{b}"

# Defines the color mapping from speeds to rgba
color_mapper = cm.ScalarMappable(cmap=cm.cividis)
rgb_values = [c[:3] for c in color_mapper.to_rgba(speeds)] # 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),
                        color=color).add_to(my_map)
my_map

為此,您需要有一個包含 2 列的數組points和一個數組speedspoints一樣多的線。 請注意,您可以將cm.cividis更改為適合您需要的任何內容(請參閱 此處的參考)

您可以將 rgba 值添加到每個點的顏色關鍵字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM