[英]Plotting multiple lines from single dataframe column
I am trying to plot a Time - Space Diagram from a gps dataset using matplotlib. 我试图使用matplotlib从gps数据集绘制时空图。 Currently I have a large dictionary of dataframes .
目前我有一个庞大的数据帧字典。 Every dataframe in my dictionary is for a single vehicle .
我的词典中的每个数据框都是针对单个车辆的。
After lots of filtering I currently have the two columns I need for every vehicle which is "Time" column as Datetime(already formatted and can be plotted) and "Distance" column as float64 type. 经过大量过滤后,我目前每辆车都需要两列,其中“时间”列为日期时间(已格式化并可绘制),“距离”列为float64类型。
My current plotting data looks like this as a dataframe : 我当前的绘图数据看起来像一个数据帧:
Time Distance
06:00 0
06:01 0,2
. . .
. . .
. . .
06:45 15
06:46 0
06:47 0,1
. . .
. . .
. . .
07:15 15
07:16 0
As you can see my distance column changes between 0-15 . 正如您所看到的,我的距离列在0-15之间变化。 What I want to do is that I want every 0-15 data to be represented with different line in a Time - Space diagram .
我想要做的是我希望每个0-15数据在时空图中用不同的线表示。
What I want to plot is something similliar to this ; 我想要绘制的是类似的东西;
! ! https://cramster-image.s3.amazonaws.com/definitions/CL-3347V2.png
https://cramster-image.s3.amazonaws.com/definitions/CL-3347V2.png
How can I plot my Distance column for every 0-15 section with different lines ? 如何使用不同的线条为每个0-15部分绘制我的距离列?
Thanks for the help 谢谢您的帮助
One way is to create a new column that labels each run of consecutive non-decreasing values with a unique label, then unstack
those labels into columns. 一种方法是创建一个新列,使用唯一标签标记每个连续非递减值的运行,然后将这些标签
unstack
为列。 Each DataFrame column is plotted as a separate data series. 每个DataFrame列都绘制为单独的数据系列。
# Example data, a bit different from yours
df = pd.DataFrame({'Distance': [0.0, 0.2, 0.4, 0.6, 14.0, 15.0,
0.0, 0.1, 14.0, 15.0,
0.0, 0.3],
'Time': ['06:00', '06:01', '06:02', '06:03', '06:44', '06:45',
'06:46', '06:47', '07:14', '07:15',
'07:16', '07:17']})
# Convert time strings to datetime if needed
df['Time'] = pd.to_datetime(df['Time'])
# Add column that labels each run of non-decreasing values
df['Vehicle'] = df['Distance'].diff().lt(0).cumsum()
df
Time Distance Vehicle
0 2019-03-29 06:00:00 0.0 0
1 2019-03-29 06:01:00 0.2 0
2 2019-03-29 06:02:00 0.4 0
3 2019-03-29 06:03:00 0.6 0
4 2019-03-29 06:44:00 14.0 0
5 2019-03-29 06:45:00 15.0 0
6 2019-03-29 06:46:00 0.0 1
7 2019-03-29 06:47:00 0.1 1
8 2019-03-29 07:14:00 14.0 1
9 2019-03-29 07:15:00 15.0 1
10 2019-03-29 07:16:00 0.0 2
11 2019-03-29 07:17:00 0.3 2
# Reshape to one column per vehicle
df.set_index(['Time', 'Vehicle'])['Distance'].unstack()
Vehicle 0 1 2
Time
2019-03-29 06:00:00 0.0 NaN NaN
2019-03-29 06:01:00 0.2 NaN NaN
2019-03-29 06:02:00 0.4 NaN NaN
2019-03-29 06:03:00 0.6 NaN NaN
2019-03-29 06:44:00 14.0 NaN NaN
2019-03-29 06:45:00 15.0 NaN NaN
2019-03-29 06:46:00 NaN 0.0 NaN
2019-03-29 06:47:00 NaN 0.1 NaN
2019-03-29 07:14:00 NaN 14.0 NaN
2019-03-29 07:15:00 NaN 15.0 NaN
2019-03-29 07:16:00 NaN NaN 0.0
2019-03-29 07:17:00 NaN NaN 0.3
# plot
df.set_index(['Time', 'Vehicle'])['Distance'].unstack().plot(marker='.')
you could do a direct plt.plot(df.time, df.dist)
and get this: 你可以直接
plt.plot(df.time, df.dist)
并得到这个:
Or you can do similar to Peter's solution without stacking in case you have a lot of time chunks: 或者你可以做类似于Peter的解决方案而不用堆叠,以防你有很多时间块:
df['chunk'] = df['dist'].diff().lt(0).cumsum()
fig, ax = plt.subplots(1,1)
df.groupby('chunk').plot(x='time', y='dist', ax=ax, legend=False, c='b')
plt.show()
and get 得到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.