简体   繁体   中英

pandas plotting duration with different levels

I have some data like this

    level   ts
0   1.0 2014-11-27 16:00:28.211950-08:00
1   1.5 2014-12-04 17:54:56.185200-08:00
2   2.1 2014-12-04 17:59:54.303900-08:00
3   2.6 2014-12-04 18:04:54.307770-08:00
4   1.8 2014-12-04 18:09:55.917890-08:00
5   1.1 2014-12-04 18:14:54.977890-08:00
6   1.0 2014-12-04 18:19:56.729060-08:00

I want to generate a plot where the Y-axis is the level, X-axis is the time within the week (assume the overall range of the x-axis is a week, and project the timestamp onto the specific location). I want to draw lines (or boxes) where, for example, there is a horizontal line from 2014-12-04 17:54:56.185200-08:00 to 2014-12-04 17:59:54.303900-08:00 with a level of 1.5 , another horizontal line from 2014-12-04 17:59:54.303900-08:00 to 2014-12-04 18:04:54.307770-08:00 at the level of 2.1 , etc. Of course, here the timestamps need to be converted to be the values within the week.

How can I do that?

I am still not certain of what exactly you want to achieve, I'm going to take a wild guess that you want to plot x-axis with labels of weekday + hour + minute , which can be achieved by adding new columns and setting multiple indexes, something like this:

import pandas as pd

df
   level                         ts
0    1.0 2014-11-28 00:00:28.211950
1    1.5 2014-12-05 01:54:56.185200
2    2.1 2014-12-05 01:59:54.303900
3    2.6 2014-12-05 02:04:54.307770
4    1.8 2014-12-05 02:09:55.917890
5    1.1 2014-12-05 02:14:54.977890
6    1.0 2014-12-05 02:19:56.729060

df['weekday'] = pd.DatetimeIndex(df['ts']).weekday
# you may use time if you just need timestamp
# df['time'] = pd.DatetimeIndex(df['ts']).time
df['hour'] = pd.DatetimeIndex(df['ts']).hour
df['minute'] = pd.DatetimeIndex(df['ts']).minute
# after that you construct a new dataframe with desired columns
df1 = df[['level', 'weekday', 'hour', 'minute']]
# then plot it by setting index to weekday and time (hour + minute)
df1.set_index(['weekday', 'hour', 'minute']).plot(kind='bar')

在此处输入图片说明

Hope this helps.

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