-4

I would like to plot a time series with bars in python. With bar width being start and end time for each bar. The event type just as a label and the counts making the height of a bar. The data looks as follows, in a pandas dataframe.

                       end                       event counts
start           
2016-10-02 16:58:00     2016-10-02 17:28:00     700     181
2016-10-03 07:07:00     2016-10-03 07:35:50     800     174
2016-10-03 07:36:00     2016-10-03 08:05:00     100     175
2016-10-04 19:47:00     2016-10-04 20:02:00     50      91
2016-10-05 08:09:00     2016-10-05 08:17:00     100     49

Any idea?

Thanks

Mario L
  • 507
  • 1
  • 6
  • 15
  • I would check out this question: https://stackoverflow.com/questions/42437349/candlestick-plot-from-a-pandas-dataframe – AtHeartEngineer Dec 01 '17 at 09:31
  • I guess many people have an idea. But in order to get an answer here you would need to show what you have tried and in how far other resources have not helped you. This is for sure not a straight forward task, so one would he happy to *help*, but not do all the work for you. Start by coding what you *can* do, provide that as a [mcve]. – ImportanceOfBeingErnest Dec 01 '17 at 09:38
  • You may start by looking at [this question](https://stackoverflow.com/questions/26498627/bar-plot-with-timedelta-as-bar-width). – ImportanceOfBeingErnest Dec 01 '17 at 09:44
  • @AtHeartEngineer thanks for the idea, not what I meant but useful – Mario L Dec 01 '17 at 10:38

1 Answers1

0

Not sure if I get your question right but you could use fill_between(). I don't know if pandas can do what you're asking for.

Since it's not a lot of data the resulting plot is not very spectacular.

from io import StringIO
import matplotlib.patches as patches

# Just preparing the data ..

data = """         
start,end,event,counts
2016-10-02 16:58:00,2016-10-02 17:28:00,700,181
2016-10-03 07:07:00,2016-10-03 07:35:50,800,174
2016-10-03 07:36:00,2016-10-03 08:05:00,100,175
2016-10-04 19:47:00,2016-10-04 20:02:00,50,91
2016-10-05 08:09:00,2016-10-05 08:17:00,100,49
"""

df = pd.read_table(StringIO(data), sep=',')    
df.start = pd.to_datetime(df.start)
df.end = pd.to_datetime(df.end)

# Plotting ..

fig = plt.figure(figsize=(16, 4.5))
ax = fig.gca()

for index, r in df.iterrows():    
    x = (r.start.value, r.end.value)
    h = r.counts    
    ax.fill_between(x, 0, h)
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378