Aasheet Kumar
Aasheet Kumar

Reputation: 351

Plotly x axis values not same as input

I am trying to create a bar chart using the following table and code:

tracedata1:

Berth
01E                                 56.0
01W                                  0.0
02                                  59.0
08                                  92.0
09                                   5.0
14                                  19.0
15                                   8.0
16                                  68.0
17                                  30.0

bar chart code :

trace1 = Bar(x = tracedata1.reset_index()['Berth'], y = tracedata1)
data1 = [trace1]
layout1 = Layout(xaxis = dict(tickangle = -45))
fig1 = dict(data = data1, layout = layout1)
iplot(fig1)

and it gives me the following graph:

enter image description here

I dont get why the x axis is only showing the Int values and not any other. The x axis input is of object type.

EDIT: df3 in question:

ATA                 ATD                Begin Receive    Berth Time       Days Spent
2018-04-14 03:50:00 2018-04-19 14:35:00 NaN                 29 5 days 10:45:00  5.0
2018-04-30 16:20:00 2018-05-01 12:58:00 2018-Apr-16 07:00   C2 0 days 20:38:00  0.0

Upvotes: 2

Views: 992

Answers (1)

asongtoruin
asongtoruin

Reputation: 10359

plotly is reading your x values, seeing some of them are numbers and automatically plotting the bars as numbers. We want to override that - so tell it your axis type is 'category'. Here's some slightly neatened code:

tracedata1 = df3.groupby('Berth', as_index=False).agg({'Days Spent': 'sum'})

trace1 = Bar(x=tracedata1['Berth'], y=tracedata1['Days Spent'])
data1 = [trace1]
layout1 = Layout(xaxis=dict(tickangle=-45, type='category'))
fig1 = dict(data=data1, layout=layout1)
iplot(fig1)

Upvotes: 2

Related Questions