简体   繁体   English

使用标准化 x 轴在同一 x 轴上绘制多条线

[英]Plotting multiple lines on same x-axis with a normalized x-axis

I have a data frame where one column is my data values (“dFF”) and another column is my timestamps where the data was recorded ("Time). I also have a list of timestamps where events occurred. I want to plot the data 3 seconds before and 5 seconds after each event, with multiple events on the same plot.我有一个数据框,其中一列是我的数据值(“dFF”),另一列是我记录数据的时间戳(“时间)。我还有一个事件发生的时间戳列表。我想 plot 数据每个事件之前 3 秒和之后 5 秒,在同一个 plot 上有多个事件。

df.head()
       Time       dFF
0  0.500267  0.617687
1  0.516673  0.737019
2  0.533079  0.801859
3  0.549485  0.762987
4  0.565891  0.572441

fig, ax0 = plt.subplots()
events = [24.541, 35.193, 45.461, 71.554, 95.954, 108.658, 134.592, 147.914, 163.671]
#Plot trials
for event in events:
     begin = event - 3.0
     end   = event + 5
     in_between = df['Time'].between(begin, end, inclusive=True)
     ax0.plot(df['Time'].loc[in_between], df["dFF"].loc[in_between])
plt.show()

The plots should essentially be on top of each other.这些图基本上应该在彼此之上。 But since I am using the time stamps as the x-coordinates, they are instead plotting across the whole time axis.但由于我使用时间戳作为 x 坐标,它们改为在整个时间轴上绘制。 Is there a good way to standardize the x-axis so that I can get the plots on top of each other?有没有一种标准化 x 轴的好方法,以便我可以将这些图放在一起? There are the same number of data points for each event in the 8 second window.在 8 秒 window 中,每个事件的数据点数量相同。

The desired graph should look something like this: Desired plot output with multiple lines on same x-axis所需的图形应如下所示: Desired plot output 在同一 x 轴上有多条线

The two graphs that I currently make (whole trace and subsets) look like this and this .我目前制作的两张图(整个轨迹和子集)看起来像thisthis

To answer my question for anyone who comes across this in the future, I made a separate array for the x-values.为了为将来遇到此问题的任何人回答我的问题,我为 x 值制作了一个单独的数组。 I created an array using np.linspace from -3 to 5 (3 seconds before to 5 seconds after) using the length of the event array as the number of values.我使用事件数组的长度作为值的数量,使用np.linspace从 -3 到 5(前 3 秒到后 5 秒)创建了一个数组。

#Plot trials
fig,ax0 = plt.subplots()
for event in events:
     begin = event - 3.0
     end   = event + 5
     in_between = df['Time'].between(begin, end, inclusive=True)
     tmp = df.loc[in_between]
     event_array = tmp['dFF'].to_numpy()
     x_mask = np.linspace(-3,5,len(event_array))
     ax0.plot(x_mask, df[dff].loc[in_between])
plt.show()

Which resulted in a plot that looks like this:这导致 plot 看起来像这样:

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

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