简体   繁体   中英

plot multiple arrows between scatter points - Matplotlib

I'm trying to plot multiple arrows between two sets of scatter points. Plotting a line is easy enough with ax.plot . But I'm trying to implement an arrow instead of a line. The arrows don't appear to be aligning between the points.

So if the line plot is initialised below, it works fine. But the quiver plot does not plot alone the same lines.

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])

According to the documentation , see scale_units option, you need: 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()

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM