Matplotlib将散点图点与可在MPLS手册中显示的双向箭头线连接起来

2024-05-18 12:45:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有如下散点图:

enter image description here

我想把所有的蓝点和红点用一条双头箭头连接起来,这条箭头可以用MPL手册显示出来

散点图如传单所示:

enter image description here

代码如下:

import matplotlib.pyplot as plt
import mplleaflet
import pandas as pd
import numpy as np

def leaflet_plot_stations():

    df = pd.read_csv('C:/Users/...') #load lon and lat

    # lon= df['Branch_Lon'].tolist()
    # lat = df['Branch_Lat'].tolist()
    lon = df['Hub_Lon'].tolist()
    lat = df['Hub_lat'].tolist()
    df['demand'] = 20
    df['size'] = df['demand'].apply(lambda x: x / 20 if x > 300 else x)
    print(df)
    size = df['size'].tolist()

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.scatter(lon, lat, c='blue', alpha=0.7, s=size, label='Local Hub')
    ax1.scatter(121.222824, 28.901852, c='r', alpha=0.7, s=120, label='Gateway Hub')

    mplleaflet.show()

leaflet_plot_stations()

Tags: importdfsizeplotasplt箭头hub
1条回答
网友
1楼 · 发布于 2024-05-18 12:45:10

您可以使用annotate来创建箭头,手动创建FancyArrowPatch稍微容易一些。然后,您只需在设置了正确起点/终点的点之间进行迭代

points_x, points_y = np.random.random(size=(2,25))
origin_x, origin_y = np.random.random(size=(2,))

fig, ax = plt.subplots()
ax.plot(points_x, points_y, 'o', color='blue', alpha=0.5)
ax.plot(origin_x, origin_y, 'o', color='red', alpha=0.5, ms=10)

for x,y in zip(points_x, points_y):
    ax.annotate('', xy=(origin_x, origin_y), xytext=(x,y), arrowprops=dict(arrowstyle='<|-|>'))

enter image description here

相关问题 更多 >

    热门问题