简体   繁体   English

从单个数据帧列绘制多行

[英]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.

相关问题 从单个 dataframe 绘制多个图 - Plotting multiple graphs from a single dataframe Matplotlib 从 Dataframe 中的一列绘制不同的线 - Matplotlib plotting different lines from one column in Dataframe Python / Pandas / Bokeh:用数据框中的图例绘制多行 - Python / Pandas / Bokeh: plotting multiple lines with legends from dataframe 使用 Pandas 数据框以不同颜色绘制多条线 - Plotting multiple lines, in different colors, with pandas dataframe 如何从数据框中的列中选择多个特定值进行绘图 - How to select multiple specific values from a column in dataframe for plotting 从 dataframe 中的多列创建单列 - Create a single column from multiple columns in a dataframe 来自数据框,熊猫中单个列的多个条件 - Multiple conditions from single column in dataframe, pandas 从一个数据框中绘制多条线并添加辅助轴以绘制不同的数据框 - Pandas - Plotting multiple lines from one dataframe and adding a secondary axis to plot a different dataframe - Pandas 如何使用 Pandas 将 csv 中的多行读取到单个数据帧行中 - How to read multiple lines from csv into a single dataframe row with pandas 从熊猫数据框中绘制数据线,由一列中的键而不是由不同列标识 - Plotting data lines from pandas dataframe, identified by keys in one column instead by different columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM