2

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
Aasheet Kumar
  • 341
  • 1
  • 2
  • 9
  • you're passing the entire dataframe as your argument for `y`. What is the column of data you want to plot called? – asongtoruin May 03 '18 at 14:14
  • `tracedata1 = df3.groupby('Berth').sum()['Days Spent']` I Also tried this from the original dataframe : `Bar(x= df3.groupby('Berth').sum().reset_index()['Berth'], y = df3.groupby('Berth').sum()['Days Spent'])` The result is the same – Aasheet Kumar May 03 '18 at 14:23

1 Answers1

2

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)
asongtoruin
  • 9,794
  • 3
  • 36
  • 47
  • Just tried it exactly how you wrote it, still giving me the same result. – Aasheet Kumar May 03 '18 at 14:40
  • Would creating a list From Berths and then adding a string to all values and then passing it through plotly help ? sorry if thats a stupid idea i just dont know what is going on here – Aasheet Kumar May 03 '18 at 14:43
  • Can you add in a sample of `df3` into your question? Try doing `print(df3.head())` and copying that in – asongtoruin May 03 '18 at 14:49
  • I edited part of df3 in the original post, its only the columns i am using for this graph but the original has about 63 columns. Also i just tried with TIme instead of days spent and had the same issue – Aasheet Kumar May 03 '18 at 15:01
  • Thank you! that worked, and apparently adding a string to the Berth column also fixed it, but your solution is much simpler and looks Better. Thank you once again! – Aasheet Kumar May 03 '18 at 15:28