跳过在matplotlib中连接的第一个和最后一个点

马梅德

我试图在图中连接不同颜色的点。但不知何故,它也连接第一和最后一点我想(当我改变'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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Matplotlib在绘图时将最后一个连接到第一个点

matplotlib-cpp 连接数据集中的第一个和最后一个点

在gnuplot中,第一个点和最后一个点相互连接

R:删除连接第一个和最后一个点的线

Python雷达图连接第一个和最后一个点

python27 matplotlib:连接的第一个和最后一个元素

从字符串中删除第一个和最后一个点(如果存在)

在比 Excel 窗口宽的 Excel 图表中检测第一个和最后一个可见点

连接列表的最后一个和第一个值

Mysql查询获取内部连接查询中第一个和最后一个事务之间的天数

如何加入列表,第一个和最后一个是 Python 中的连接字符?

跳过连接第一个值

熊猫从组中获取列的第一个和最后一个值

如何删除列中的第一个单词和最后一个单词?

如何选择<tr>中的第一个和最后一个<td>?

获取groupby中的第一个和最后一个值

Yii2 ListView中的第一个和最后一个项目

删除组中的第一个和最后一个观测值

在R中查找条纹的第一个和最后一个日期

sqlite中的第一个和最后一个功能

从字符串中删除第一个和最后一个空行

R:识别重复组中的第一个和最后一个元素

MySql 从查询中获取第一个和最后一个结果

删除数组中的第一个和最后一个元素

从解析的数组中获取第一个和最后一个日期

ul列表中的第一个和最后一个VISIBLE元素

在Fortran数组中查找第一个和最后一个正值

NumPy数组中的第一个和最后一个元素

如何获取文件中的第一个和最后一个日期