【问题标题】:plot multiple arrows between scatter points在散点之间绘制多个箭头
【发布时间】:2020-10-26 23:59:49
【问题描述】:

我正在尝试在两组散点之间绘制多个箭头。使用ax.plot 绘制一条线很容易。但我试图实现一个箭头而不是一条线。箭头似乎没有在点之间对齐。

所以如果线图在下面初始化,它工作正常。但箭袋图并不会单独绘制相同的线。

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

df = pd.DataFrame(np.random.randint(-50,50,size=(100, 4)), columns=list('ABCD'))

fig, ax = plt.subplots()

x1 = df['A']
y1 = df['B']

x2 = df['C']
y2 = df['D']

AB = plt.scatter(x1, y1, c = 'blue', marker = 'o', s = 10, zorder = 3)
CD = plt.scatter(x2, y2, c = 'red', marker = 'o', s = 10, zorder = 2)

# plot line between points
#ax.plot([x1,x2],[y1,y2], color = 'black', linestyle = '--', linewidth = 0.5)

ax.quiver([x1, x2], [y1, y2])

【问题讨论】:

    标签: python matplotlib arrow-functions


    【解决方案1】:

    根据documentation,见scale_units选项,你需要:angles='xy', scale_units='xy', scale=1 in quiver

    AB = ax.scatter(x1, y1, c = 'blue', marker = 'o', s = 10, zorder = 3)
    CD = ax.scatter(x2, y2, c = 'red', marker = 'o', s = 10, zorder = 2)
    
    ax.quiver(x1, y1, (x2-x1), (y2-y1), angles='xy', scale_units='xy', scale=1)
    plt.show()
    

    输出:

    【讨论】:

    • 谢谢@Quang Hoang
    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多