制图。 Python 在地图上用色标显示移动速度
Cartography. Vizualizing speed of movement with color scale on map in Python
我在 csv 中有 3 个主要值(经度、纬度和速度)。使用 Folium 库我可以用经度和纬度映射位置 degree.my 当前代码是:
import pandas as pd
from geopy.geocoders import Nominatim
import folium
from pandas import *
import numpy as np
from matplotlib import cm
import folium
df = DataFrame({'lon': [9.899193, 9.899236, 9.899292, 9.899331, 9.899372, 9.899411, 9.89946, 9.899518, 9.899393, 9.899409, 9.899456, 9.899498, 9.89952, 9.899558, 9.8996, 9.899625, 9.899646, 9.899659, 9.899678, 9.899707, 9.899745, 9.899778], 'lat': [48.849231, 48.849201, 48.849163, 48.849128, 48.84909, 48.849056, 48.84901, 48.848968, 48.849018, 48.849014, 48.848979, 48.848949, 48.848926, 48.84888, 48.848831, 48.848789,48.848762, 48.848735, 48.848712, 48.848686,48.848655, 48.848632 ], 'speed' : [15, 25, 32, 45, 50, 75, 64, 32, 13, 23, 42, 13, 23, 42, 64, 32, 13, 23, 42, 13, 23, 42]})
ave_lt = sum(df['lat'])/len(df)
ave_lg = sum(df['lon'])/len(df)
points = zip(df['lat'], df['lon'])
points = list(points)
my_map = folium.Map(location=[ave_lt, ave_lg], zoom_start=14)
folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)
my_map
到这里它运行良好并给我这样的输出:
但现在我还想从 csv 中获取速度属性,以在当前地图上生成色标(基于速度值)。例如,如果速度在 0-20 之间,则那部分线为红色,如果速度在 20-60 之间,则为黄色,如果速度高于 60,则该线为绿色。可以在 python 中完成吗?有人可以帮我吗?我想得到这样的输出:
对于这个问题,我很抱歉,但我是 python 的新人,我真的需要这样做,在此先感谢!!非常感谢您的帮助!
试试这个:
首先编写如下两个函数
def draw_polylines(points, speeds, map):
colors = [speed_color(x) for x in speeds]
n = len(colors)
# Need to have a corresponding color for each point
if n != len(points):
raise ValueError
i = 0
j = 1
curr = colors[0]
while i < n and j < n:
if colors[i] != colors[j]:
line = folium.PolyLine(points[i:j], color=curr, weight=2.5, opacity=1)
line.add_to(map)
curr = colors[j]
i = j
j += 1
if i < j:
folium.PolyLine(points[i:j], color=curr, weight=2.5, opacity=1).add_to(map)
def speed_color(speed):
if speed < 0:
raise ValueError
elif speed >= 0 and speed < 20:
return 'red'
elif speed >= 20 and speed < 60:
return 'yellow'
else:
return 'green'
然后,行后
my_map = folium.Map(location=[ave_lt, ave_lg], zoom_start=14)
打这个电话
draw_polylines(points, df['speed'], my_map)
我在你给定的数据上试过了,它似乎有效。一定要自己复习一下。
本质上,线段颜色是逐点决定的,每一个都使用相应的速度。如果没有每个点的速度,或者有负速度(编写更广泛的错误测试;数据通常不干净!)此代码将抛出错误。
我在 csv 中有 3 个主要值(经度、纬度和速度)。使用 Folium 库我可以用经度和纬度映射位置 degree.my 当前代码是:
import pandas as pd
from geopy.geocoders import Nominatim
import folium
from pandas import *
import numpy as np
from matplotlib import cm
import folium
df = DataFrame({'lon': [9.899193, 9.899236, 9.899292, 9.899331, 9.899372, 9.899411, 9.89946, 9.899518, 9.899393, 9.899409, 9.899456, 9.899498, 9.89952, 9.899558, 9.8996, 9.899625, 9.899646, 9.899659, 9.899678, 9.899707, 9.899745, 9.899778], 'lat': [48.849231, 48.849201, 48.849163, 48.849128, 48.84909, 48.849056, 48.84901, 48.848968, 48.849018, 48.849014, 48.848979, 48.848949, 48.848926, 48.84888, 48.848831, 48.848789,48.848762, 48.848735, 48.848712, 48.848686,48.848655, 48.848632 ], 'speed' : [15, 25, 32, 45, 50, 75, 64, 32, 13, 23, 42, 13, 23, 42, 64, 32, 13, 23, 42, 13, 23, 42]})
ave_lt = sum(df['lat'])/len(df)
ave_lg = sum(df['lon'])/len(df)
points = zip(df['lat'], df['lon'])
points = list(points)
my_map = folium.Map(location=[ave_lt, ave_lg], zoom_start=14)
folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)
my_map
到这里它运行良好并给我这样的输出:
对于这个问题,我很抱歉,但我是 python 的新人,我真的需要这样做,在此先感谢!!非常感谢您的帮助!
试试这个:
首先编写如下两个函数
def draw_polylines(points, speeds, map):
colors = [speed_color(x) for x in speeds]
n = len(colors)
# Need to have a corresponding color for each point
if n != len(points):
raise ValueError
i = 0
j = 1
curr = colors[0]
while i < n and j < n:
if colors[i] != colors[j]:
line = folium.PolyLine(points[i:j], color=curr, weight=2.5, opacity=1)
line.add_to(map)
curr = colors[j]
i = j
j += 1
if i < j:
folium.PolyLine(points[i:j], color=curr, weight=2.5, opacity=1).add_to(map)
def speed_color(speed):
if speed < 0:
raise ValueError
elif speed >= 0 and speed < 20:
return 'red'
elif speed >= 20 and speed < 60:
return 'yellow'
else:
return 'green'
然后,行后
my_map = folium.Map(location=[ave_lt, ave_lg], zoom_start=14)
打这个电话
draw_polylines(points, df['speed'], my_map)
我在你给定的数据上试过了,它似乎有效。一定要自己复习一下。
本质上,线段颜色是逐点决定的,每一个都使用相应的速度。如果没有每个点的速度,或者有负速度(编写更广泛的错误测试;数据通常不干净!)此代码将抛出错误。