![](/img/trans.png)
[英]Line plot with arrows in matplotlib for all 4 lines plotted with lists of 4 elements
[英]How to plot vertical lines on an already plotted line plot?
我试图在已经绘制的线条中绘制垂直线。
我的数据只有一小部分如下:
EscAct_Curr_A StepID Time_Elapsed
0.122100122272968 1 0.0
0.0 2 0.101
0.0 2 0.432
0.122100122272968 2 1.422
0.122100122272968 2 2.422
0.122100122272968 2 3.422
0.122100122272968 2 4.432
0.122100122272968 2 5.938
0.122100122272968 2 6.49
0.122100122272968 5 7.928
0.122100122272968 5 8.938
0.122100122272968 5 9.938
在图形上绘制整个数据时,我使用以下代码:
x = data['Time_Elapsed']
y = data['EscAct_Curr_A']
plt.plot(x, y)
plt.show()
我得到下面的图:
我现在想做的是找到每个StepID
的最短时间,并在StepID
绘制一条垂直线。
例如:
从上面的数据中我们可以看到,0.0是StepID 1
的最短时间,因此必须在0.0处绘制一条垂直线,并且必须将其命名为1,对于StepID 2
,0.101是最短时间,因此一条垂直线应为0.101必须绘制,并且必须命名为2,依此类推。
我想知道如何在matplotlib或seaborn中完成此操作
谢谢
一个简单的方法:
m=0
tm=data['Time_Elapsed']
for i,val in enumerate(data['StepID']):
if(val!=m):#detect change in val
m=val
plt.plot([tm[i],tm[i]],[0,1])#plot a vertical line
计算您的最小时间,以列表形式表示。 然后,在plt.show
之前,将所有分钟数和plt.show
放入for
循环中(每个分钟数应有一个stepid):
for i in range(len(mintimes)):
plt.axvline(x=mintime[i], color='b')
plt.figtext( mintime[i], y_convenient, str(stepid[i]), color='tab:brown', size='x-small', fontweight='bold' )
因此y_convenient
会是您希望显示步长的高度。 我已经指出了一些格式化的可能性。 您可能需要进行调整,例如可能为了提高可读性而在mintime [i]中添加一个偏移量。
import numpy as np
# build an array with the stepIDs
stepIDs = np.unique(data['stepID'])
minTimes = np.zeros_like(stepIDs)
# then loop through it
for j in range(len(stepIDs)):
currentID = stepIDs[j]
currentTimes = data['Time_Elapsed'][np.where(data['stepID'] == currentID)]
minTimes[j] = min(currentTimes)
# then just plot the lines as explained in
# https://stackoverflow.com/questions/24988448/how-to-draw-vertical-lines-on-a-given-plot-in-matplotlib
for minTime in minTimes:
plt.axvline(x=minTimes)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.