我试图在图中连接不同颜色的点。但不知何故,它也连接第一和最后一点我想(当我改变'o'
到'o-'
)。我在那儿通过了几个问题,但没有找到答案。
初始图:
发生了什么:
我正努力将第一个点连接到最后一个点以显示轨迹。
码:
for k in range(0, n_clusters):
x=[]
y=[]
for j in range(0, len(final_cluster_result[k])):
x_res = list(final_cluster_result[k][j].longitude)
y_res = list(final_cluster_result[k][j].latitude)
x.append(x_res)
y.append(y_res)
x = [s for t in x for s in t]
y = [s for t in y for s in t]
plt.plot(x,y,'o',markersize=3)
plt.grid(which='both')
plt.title(f'Clustering {k}',fontsize=14)
plt.xlabel("Longitude",fontsize=15)
plt.ylabel("Latitude",fontsize=15)
不幸的是,我无法为您提供帮助,matplotlib
但我可以提出另一个绘图库。我发现拥有类似空间数据的经验(我发现您具有经度),我发现plotly's mapbox
创建交互式地图非常容易,在交互式地图中可以轻松绘制轨迹。
如果您有兴趣,请参考以下示例代码。我从一个较旧的项目中留下了几个(不一定需要)布局属性。该代码输出一个html文件,您可以打开该文件以从浏览器访问交互式地图。
import pandas as pd
import plotly.graph_objects as go
import plotly.offline
df = # dataframe with at least 'longitude','latitude','cluster' columns and 1 row for each point
your_color_list = # specify color for each cluster, using a colors list with length = n_clusters
# define the lines to be plotted
map_lines = []
for cluster, group in df.groupby('cluster'): # group dataframe by cluster and iterate over the groups
for i in range(len(group)): # iterate over each group (cluster) and plot the lines
map_lines.append(
go.Scattermapbox(
lon=list(group['longitude']),
lat=list(group['latitude']),
mode='lines',
line=go.scattermapbox.Line(
width=4,
color=your_color_list[i],
),
)
)
# define the map's layout
map_layout = dict(
autosize=True,
showlegend=False,
height=850,
font=dict(color='#191A1A'),
titlefont=dict(color='#191A1A', size=18),
margin=dict(
l=25,
r=25,
b=25,
t=35
),
hovermode='closest',
plot_bgcolor='#fffcfc',
paper_bgcolor='#fffcfc',
legend=dict(font=dict(size=10), orientation='h'),
title='Visualization of trajectories',
mapbox=dict(
accesstoken = your_access_token, # get a free token from plotly site
style='outdoors',
center={'lon': 22,'lat': 40}, # define the coordinates of the center of your plot, now your map is over Greece
bearing=0,
zoom=14, # map zoom
)
)
fig = go.Figure(
data=map_lines,
layout=map_layout)
plotly.offline.plot(
fig,
filename='your_file_name_here.html')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句