Pepef
May 28, 2020, 2:33pm
1
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.
When i try to plot a histogram counting cluster for each time shift, i’m experimenting some issue.
fig = px.histogram(live_data,
x="time_shift",
color="cluster",
histfunc='count')
My X axis is not sorted, however my dataframe is. I can’t figure why ?
Thanks in advance for your help
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.
1 Like
Pepef
May 28, 2020, 5:34pm
3
Thx for your answer @Emmanuelle !
You are right my time_shift
axis is an object.
As you suggested I change format to date.
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.
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.
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 :
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 ?