Issue
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 Line plot with arrows in matplotlib Any suggestion is appreciated.
median1
median2
median3
median4
median_a
median_b
median_c
median_d
import matplotlib.pyplot as plt
x1 = median_a
x2 = median_b
x3 = median_c
x4 = median_d
y1 = median1
y2 = median2
y3 = median3
y4 = median4
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')
plt.title("Flow")
plt.ylabel("Speed")
plt.xlabel("Size")
plt.legend(bbox_to_anchor=(1.5, 1),
bbox_transform=plt.gcf().transFigure)
Solution
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()
Answered By - Colim
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.