Search code examples
pythonpandasplottime-seriespython-ggplot

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, i.e., 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.


Solution

  • df.set_index('Time').groupby('UnitNo').Sensor.plot();
    

    enter image description here