# Px.histogram having X-axis sorting issue

**URL:** <https://community.plotly.com/t/px-histogram-having-x-axis-sorting-issue/40213>\
**Category:** 📊 Plotly Python\
**Created:** [May 28, 2020, 2:33pm UTC](https://community.plotly.com/t/px-histogram-having-x-axis-sorting-issue/40213 "2020-05-28T14:33:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pepef](https://avatars.discourse-cdn.com/v4/letter/p/a5b964/32.png) [@Pepef](https://community.plotly.com/u/Pepef)\
**Post date:** [May 28, 2020, 2:33pm UTC](https://community.plotly.com/t/px-histogram-having-x-axis-sorting-issue/40213/1 "2020-05-28T14:33:01Z")

</div>

Hi,

I’m experimenting some troubles with my X-axis on my px.histogram.

I’m using Python 3.6.3 and Plotly 4.8.0 .

Data are stored in a pd.DataFrame object. It has 2 columns :

- time\_shift (datetime) : 15 min range
- cluster (int) : output from a clustering algorithm

Dataframe is sorted on “time\_shift” column.

![df](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/d/d2049c15eac830a18f6cbb59b5a2463a670d6e7b.png)

When i try to plot a histogram counting cluster for each time shift, i’m experimenting some issue.

```auto
        fig = px.histogram(live_data,
                           x="time_shift",
                           color="cluster",
                           histfunc='count')

```

 ![graph](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/7/7a50ebb1698712657da6b7c96bf50c24d27a0e28.png)

My X axis is not sorted, however my dataframe is. I can’t figure why ?

Thanks in advance for your help

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [May 28, 2020, 5:05pm UTC](https://community.plotly.com/t/px-histogram-having-x-axis-sorting-issue/40213/2 "2020-05-28T17:05:32Z")

</div>

Hi @Pepef welcome to the forum! Probably your `time_shift` axis is of string or object type and therefore it is considered as a categorical axis by plotly. You can change the format to datatime and see if the axis is sorted as expected.

---

<div class="post-metadata">

**Author:** ![Pepef](https://avatars.discourse-cdn.com/v4/letter/p/a5b964/32.png) [@Pepef](https://community.plotly.com/u/Pepef)\
**Post date:** [May 28, 2020, 5:34pm UTC](https://community.plotly.com/t/px-histogram-having-x-axis-sorting-issue/40213/3 "2020-05-28T17:34:24Z")

</div>

Thx for your answer 🙂 @Emmanuelle !

You are right my `time_shift` axis is an object.  
 ![dtypes](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/a/add3c8371e3b35f6f31199284cc9c4678015fef6.png)

As you suggested I change format to date.

```auto
fig = px.histogram(live_data,
                           x="time_shift",
                           color="cluster",
                           histfunc="count")
        fig.update_layout(xaxis = dict(type = 'date'))

```

However it didn’t fix anything.

 ![graph](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/0/00a8bd025ce6552a42264d8e69bb700e24bd48e8.png)

My object is actually a `datetime.time` and not a` datetime.datetime`.

I can trick it by adding a fake date to be a ` datetime.datetime` and only displaying hours and minutes.

```auto
       fig = px.histogram(live_data,
                           x="date_time_shift",
                           color="cluster",
                           histfunc="count")
        fig.update_layout(xaxis = dict(tickformat = '%H:%M',
                                               type = 'date'),
                          bargroupgap = 0.2)

```

It produces this :

 ![graph](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/1/1f49750ef99fb3740824c738ba2a9c18b0e8d336.png)

It’s much better however it’s not following my time range of 15 minutes per bar. (now each bar represents 1 hour)

Do you have any idea on which parameters i can set to format my axis ?
