简体   繁体   English

matplotlib 中带有箭头的线图,用于绘制 4 个元素列表的所有 4 条线

[英]Line plot with arrows in matplotlib for all 4 lines plotted with lists of 4 elements

These variables are all lists containing 4 elements.这些变量都是包含 4 个元素的列表。 I am trying to get the arrows to show in between these 4 elements for all the lists below in the line plot.我试图让箭头显示在折线图中以下所有列表的这 4 个元素之间。 In another words, I will have 3 arrows for each of the 4 lines in the plot shown below.换句话说,我将为下图中的 4 条线中的每条线设置 3 个箭头。

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.我遇到了这篇建议使用quiver的帖子,但我不确定如何将它应用到我的用例Line plot with arrows in matplotlib任何建议都值得赞赏。

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)

在此处输入图像描述

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

在此处输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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