简体   繁体   English

如何在已经绘制的线图中绘制垂直线?

[英]How to plot vertical lines on an already plotted line plot?

I am trying to plot vertical lines in a line lot that I have already plotted. 我试图在已经绘制的线条中绘制垂直线。

Just a small portion of my data looks as follows: 我的数据只有一小部分如下:

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

While plotting the entire data on the graph, I use the following code: 在图形上绘制整个数据时,我使用以下代码:

x = data['Time_Elapsed']
y = data['EscAct_Curr_A']
plt.plot(x, y)
plt.show()

and I get the following graph: 我得到下面的图:

在此处输入图片说明

What I want to do now is to find the minimum time of each StepID and plot a vertical line on the graph above. 我现在想做的是找到每个StepID的最短时间,并在StepID绘制一条垂直线。

For example: 例如:

From the above data we can see that 0.0 is the minimum time for StepID 1 , so a vertical line must be drawn at 0.0 and must be named as 1, then for StepID 2 , 0.101 is the minimum time, so a vertical line at 0.101 must be drawn and must be named as 2 and so on. 从上面的数据中我们可以看到,0.0是StepID 1的最短时间,因此必须在0.0处绘制一条垂直线,并且必须将其命名为1,对于StepID 2 ,0.101是最短时间,因此一条垂直线应为0.101必须绘制,并且必须命名为2,依此类推。

I would like to know how can this be done either in matplotlib or in seaborn 我想知道如何在matplotlib或seaborn中完成此操作

Thanks 谢谢

A simple approach: 一个简单的方法:

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

Compute your mintimes, say as a list. 计算您的最小时间,以列表形式表示。 Then, before plt.show put in a for loop for all of your mintimes and stepids (there should be one stepid for each mintime): 然后,在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' )

So y_convenient would be some height that you prefer to show the stepids at. 因此y_convenient会是您希望显示步长的高度。 I have indicated some formatting possibilities. 我已经指出了一些格式化的可能性。 You will want to tweak things, such as maybe adding an offset to mintime[i] for readability. 您可能需要进行调整,例如可能为了提高可读性而在mintime [i]中添加一个偏移量。

I guess the problem is also finding the minimums, the vertical line is already answered here 我想问题也在寻找最小值,垂直线已经在这里回答

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.

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