Create bar between start and end time on chart

I have these results using jupyter notebook / pandas / plotly ā€“

These refer to start and end times of a person working on days throughout the month of July.

Instead of two points on each end, what Iā€™d really like are barsā€¦ ideally something like this (which I made on adobe illustrator)ā€¦ any help?

My code looks like this:

trace_start = go.Scatter(
x=df.date,
y=df[ā€˜stimeā€™],
mode=ā€˜markersā€™,
name = ā€œstart timeā€,
line = dict(color = ā€˜greenā€™),
opacity = 0.8)

trace_end = go.Scatter(
x=df.date,
y=df[ā€˜etimeā€™],
mode=ā€˜markersā€™,
name = ā€œend timeā€,
line = dict(color = ā€˜redā€™),
opacity = 0.8)

data = [trace_start,trace_end]

layout = dict(
title = ā€œTime Workedā€,
xaxis = dict(
range = [ā€˜2018-07-01ā€™,ā€˜2018-07-31ā€™])
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename = ā€˜Time Workedā€™)

1 Like

Hi @pjford,

You can use the base property of the bar trace to control the starting height of bars in a bar chart. For example:

import pandas as pd
import plotly.graph_objs as go
fig = go.FigureWidget()

dates = pd.date_range('2018-01', '2018-12', freq='MS')
fig.add_bar(x=dates,
            y=[2, 3, 4, 1, 4, 5, 3, 4, 5, 3, 2, 3],
            base=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
fig

Note that in this case y is not the top edge of the bar, but the height above the barā€™s base

Hope that helps!
-Jon

1 Like

This in theory seems like it would be a good solution, but when I run the sample code youā€™ve provided it runs forever and I have to restart jupyter notebook because it never finishesā€¦ any thoughts on whatā€™s happening?

Hmm, no I donā€™t have any ideas.

Maybe something is going wrong for you with FigureWidget. Try displaying with iplot like you were originally:

import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

fig = go.Figure()

dates = pd.date_range('2018-01', '2018-12', freq='MS')
fig.add_bar(x=dates,
            y=[2, 3, 4, 1, 4, 5, 3, 4, 5, 3, 2, 3],
            base=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
iplot(fig)

Thank you so much! I was able to get your example running. I changed the format a bit to make it about timeā€¦ and came up with this below.

Iā€™m still doing something wrong as there are these problems:

  1. The duration/bar-length is incorrect
  2. The X-axis is not showing the Jul-4 entry
  3. The Y-axis appears to be out of orderā€¦ 8am, 730am, 815amā€¦
  4. The Y-axis does not go far enough up

import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

fig = go.Figure()

btime = pd.to_datetime(['2018-07-02 08:00','2018-07-03 07:30','2018-07-04 08:15'])

timedur = [pd.Timedelta('8 hours'),pd.Timedelta('8.25 hours'),pd.Timedelta('7.75 hours')]

thedates = pd.date_range('2017-07', '2017-08', freq='D')


fig.add_bar(x=thedates,

            y=timedur,

            base=btime)

iplot(fig)
Breakdown Start time End time
8:54 AM 9:05 AM 9:05 AM
8:55 AM 9:09 AM 9:16 AM
9:00 AM 9:07 AM 9:11 AM
9:03 AM 9:09 AM 9:14 AM
9:06 AM 9:22 AM 9:23 AM
9:07 AM 9:22 AM 9:22 AM
9:09 AM 9:12 AM 9:20 AM
9:10 AM 9:15 AM 9:21 AM
9:11 AM 9:22 AM 9:34 AM
9:15 AM 9:17 AM 9:18 AM
9:16 AM 9:37 AM 9:40 AM
9:16 AM 9:23 AM 9:31 AM
9:17 AM 9:33 AM 9:34 AM
9:19 AM 9:22 AM 9:31 AM

I have source file like this, how to generate chart that you shown, please help me get in ms excel or anything else

Thanks for the information. I will try to figure it out for more.

www.myaarpmedicare.com