These variables are all lists containing 4 elements. I am trying to get the arrows to show in between these 4 elements for all the lists below in the line plot. In another words, I will have 3 arrows for each of the 4 lines in the plot shown below.
I came across this post that suggested the use of quiver
but I am not sure how to apply it to my use case
CodePudding user response:
import numpy as np
import matplotlib.pyplot as plt
median_a, median_b, median_c, median_d = [np.random.random((4, 1)) for _ in range(4)]
median1, median2, median3, median4 = [np.random.random((4, 1)) for _ in range(4)]
x1 = median_a
x2 = median_b
x3 = median_c
x4 = median_d
y1 = median1
y2 = median2
y3 = median3
y4 = median4
deltaX1, deltaX2, deltaX3, deltaX4 = [x[1:]-x[:-1] for x in [x1, x2, x3, x4]]
deltaY1, deltaY2, deltaY3, deltaY4 = [y[1:]-y[:-1] for y in [y1, y2, y3, y4]]
line1 = plt.plot(x1, y1,'bo-',label='B0')
line2 = plt.plot(x2, y2,'go-',label='B1')
line3 = plt.plot(x3, y3,'ko-',label='B2')
line4 = plt.plot(x4, y4,'ro-',label='B3')
arrows1 = plt.quiver(x1[:-1], y1[:-1], deltaX1, deltaY1, scale_units='xy', angles='xy', scale=1)
arrows2 = plt.quiver(x2[:-1], y2[:-1], deltaX2, deltaY2, scale_units='xy', angles='xy', scale=1)
arrows3 = plt.quiver(x3[:-1], y3[:-1], deltaX3, deltaY3, scale_units='xy', angles='xy', scale=1)
arrows4 = plt.quiver(x4[:-1], y4[:-1], deltaX4, deltaY4, scale_units='xy', angles='xy', scale=1)
plt.title("Flow")
plt.ylabel("Speed")
plt.xlabel("Size")
plt.legend()
plt.show()
CodePudding user response:
To prevent the double plotting in the other answers, you can specify the arrow colour directly. Also, since you are plotting four times it would be easier to do it in a loop.
xx = [median_a, median_b, median_c, median_d]
yy = [median1, median2, median3, median4]
colors = ['b', 'g', 'k', 'r']
for idx, (x, y) in enumerate(zip(xx, yy)):
Q = plt.quiver(x[:-1], y[:-1], x[1:]-x[:-1], y[1:]-y[:-1], color=colors[idx], scale_units='xy', angles='xy', scale=1)
plt.quiverkey(Q, 0.95, 1-idx/10, 0.05, label=f'B{idx}', labelpos='N')
plt.title("Flow")
plt.ylabel("Speed")
plt.xlabel("Size")
plt.show()