# Horizontal bar plotly with datetime

**URL:** https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769
**Category:** 📊 Plotly Python
**Created:** [August 5, 2022, 4:56pm UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769 "2022-08-05T16:56:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mainul](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mainul](https://community.plotly.com/u/mainul)
#### Post date: [August 5, 2022, 4:56pm UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769/1 "2022-08-05T16:56:47Z")

</div>

I wanted to create a timeline graph with horizontal bar. I tried the following code:

```auto
import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(
    y=['AndOn'],
    x=["22-07-01 00:00:8"],
    name='SF Zoo',
    orientation='h',
    legendgroup="group1",
    marker=dict(
        color='rgba(246, 78, 139, 0.6)',
        line=dict(color='rgba(246, 78, 139, 1.0)', width=1)
    )
))
fig.add_trace(go.Bar(
    y=['AndOn'],
    x=["22-07-01 00:00:18"],
    name='LA Zoo',
    legendgroup="group2",
    orientation='h',
    marker=dict(
        color='rgba(58, 71, 80, 0.6)',
        line=dict(color='rgba(58, 71, 80, 1.0)', width=1)
    )
))

fig.add_trace(go.Bar(
    y=['AndOn'],
    x=["22-07-01 00:00:5"],
    name='SF Zoo',
    legendgroup="group1",
    orientation='h',
    marker=dict(
        color='rgba(246, 78, 139, 0.6)',
        line=dict(color='rgba(246, 78, 139, 1.0)', width=1)
    )
))

fig.update_layout(barmode='stack')
fig.show()

```

The date time in x axis is not showing correct. one solution could be to use `px.timeline()` like [this] answer. ([python - plotly horizontal stacked bar chart not working with x-axis in dates - Stack Overflow](https://stackoverflow.com/questions/65914314/plotly-horizontal-stacked-bar-chart-not-working-with-x-axis-in-dates))  
But i don’t want to use plotly express. How to do this using graph object?

---

<div class="post-metadata">

### Author: ![jlfsjunior](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jlfsjunior/32/16061_2.png) [@jlfsjunior](https://community.plotly.com/u/jlfsjunior)
#### Post date: [August 5, 2022, 6:04pm UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769/2 "2022-08-05T18:04:58Z")

</div>

Hi,

You can use the figure factory, but it is not a graph object and it is pretty similar to `px.timeline`: [Gantt charts in Python](https://plotly.com/python/gantt/#deprecated-figure-factory)

---

<div class="post-metadata">

### Author: ![mainul](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mainul](https://community.plotly.com/u/mainul)
#### Post date: [August 5, 2022, 9:27pm UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769/3 "2022-08-05T21:27:48Z")

</div>

can i use it to make subplots (not facet)?

Also, I can not understand, why the DateTime is not correct?

---

<div class="post-metadata">

### Author: ![jlfsjunior](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jlfsjunior/32/16061_2.png) [@jlfsjunior](https://community.plotly.com/u/jlfsjunior)
#### Post date: [August 6, 2022, 10:50am UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769/4 "2022-08-06T10:50:34Z")

</div>

> [@mainul](#):
>
> Also, I can not understand, why the DateTime is not correct?

What do you mean?

---

<div class="post-metadata">

### Author: ![mainul](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mainul](https://community.plotly.com/u/mainul)
#### Post date: [August 6, 2022, 2:10pm UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769/5 "2022-08-06T14:10:03Z")

</div>

I mean, if i run the code that i have given , the timeline is not correct.

---

<div class="post-metadata">

### Author: ![mainul](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mainul](https://community.plotly.com/u/mainul)
#### Post date: [August 17, 2022, 9:07am UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769/6 "2022-08-17T09:07:15Z")

</div>

I think this is bug in Bar graph. It gets confused with date in y axis (or x in case of horizontal bar).  
Anyway, i solved this by using scatterplot.

```auto
import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(y=[0,0],x=["2022-07-15 12:00:08","2022-07-15 12:00:18"],mode="lines",line=dict(color="red",width=100)))
fig.add_trace(go.Scatter(y=[0,0],x=["2022-07-15 12:00:18","2022-07-15 12:00:22"],mode="lines",line=dict(color="green",width=100)))

fig.add_trace(go.Scatter(y=[0,0],x=["2022-07-15 12:00:22","2022-07-15 12:00:25"],mode="lines",showlegend=False,line=dict(color="red",width=100)))

fig.update_layout(yaxis_range=[0,0])
fig.show()

```

---

<div class="post-metadata">

### Author: ![Tunneller](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@Tunneller](https://community.plotly.com/u/Tunneller)
#### Post date: [August 15, 2023, 2:34am UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769/7 "2023-08-15T02:34:03Z")

</div>

This is an old post but I just needed (and found) the same solution for javascript: you can enter the base on a horizontal stacked plot using string times, and the use x as an offset to base, where x is in milliseconds.

https://codepen.io/PlotlyBeginner/embed/preview/xxQvade?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=xxQvade

Has example.

---

<div class="post-metadata">

### Author: ![Tunneller](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@Tunneller](https://community.plotly.com/u/Tunneller)
#### Post date: [August 15, 2023, 2:35am UTC](https://community.plotly.com/t/horizontal-bar-plotly-with-datetime/66769/8 "2023-08-15T02:35:06Z")

</div>

Now that is interesting… the “raw” javascript makes a lovely plot. But not as a link? Anyway, you can find it.
