onr
onr

Reputation: 296

Bar Plot from a dictionary with tuple keys

I have a dictionary with tuple as keys, like shown below:

{('Friday', 0): 108, ('Friday', 1): 110, ('Friday', 2): 75, ... ('Sunday', 23): 120}

I`m trying to build a Bar plot, that with the dictionary keys in x axis and dictionary values in Y axis:

trace0 = go.Bar(
    x=pump_dry_day_beh_dic.keys(),
    y=pump_dry_day_beh_dic.values(),
    name ='yyy'

)

fig = tools.make_subplots(rows=1, cols=1, specs=[[{}]],
                          shared_xaxes=True, shared_yaxes=True,
                          vertical_spacing=0.001)

fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 1)

fig['layout'].update(height=600, width=1000,  title='xxx')
fig['layout']['xaxis1'].update(title='day-hour')
fig['layout']['yaxis1'].update(title='Values')
plotly.offline.iplot(fig, filename=' xxx')

But I`m getting the following error:

 The 'x' property is an array that may be specified as a tuple,
    list, numpy array, or pandas Series

On the other hand, if I change the above code to the code below, I have as result an empty plot.

   x=list(pump_dry_day_beh_dic.keys()),
    y=list(pump_dry_day_beh_dic.values()),
    name ='yyyy'

Is there a way how I can solve this?

Thank you,

Upvotes: 3

Views: 1250

Answers (1)

ysakamoto
ysakamoto

Reputation: 2532

Since you have pandas tag here...

import pandas as pd

pump_dry_day_beh_dic = {('Friday', 0): 108, ('Friday', 1): 110, 
                        ('Friday', 2): 75, ('Sunday', 23): 120}
pd.DataFrame([pump_dry_day_beh_dic]).T.plot.bar()

enter image description here

Upvotes: 4

Related Questions