简体   繁体   中英

Plotting irregular time-series (multiple) from dataframe using ggplot

I have a df structured as so:

   UnitNo        Time           Sensor
0   1.0   2016-07-20 18:34:44    19.0
1   1.0   2016-07-20 19:27:39    19.0
2   3.0   2016-07-20 20:45:39    17.0
3   3.0   2016-07-20 23:05:29    17.0
4   3.0   2016-07-21 01:23:30    11.0
5   2.0   2016-07-21 04:23:59    11.0
6   2.0   2016-07-21 17:33:29    2.0
7   2.0   2016-07-21 18:55:04    2.0

I want to create a time-series plot where each UnitNo has its own line (color) and the y-axis values correspond to Sensor and the x-axis is Time . I want to do this in ggplot , but I am having trouble figuring out how to do this efficiently. I have looked at previous examples but they all have regular time series, ie, observations for each variable occur at the same times which makes it easy to create a time index. I imagine I can loop through and add data to plot(?), but I was wondering if there was a more efficient/elegant way forward.

I think you need pivot or set_index and unstack with DataFrame.plot :

df.pivot('Time', 'UnitNo','Sensor').plot()

Or:

df.set_index(['Time', 'UnitNo'])['Sensor'].unstack().plot()

图形

If some duplicates:

df = df.groupby(['Time', 'UnitNo'])['Sensor'].mean().unstack().plot()
df = df.pivot_table(index='Time', columns='UnitNo',values='Sensor', aggfunc='mean').plot()
df.set_index('Time').groupby('UnitNo').Sensor.plot();

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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