编程语言
首页 > 编程语言> > python-带元组键的字典中的条形图

python-带元组键的字典中的条形图

作者:互联网

我有一本以元组为键的字典,如下所示:

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

我正在尝试构建一个条形图,在x轴上使用字典键,在Y轴上使用字典值:

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')

但是我收到以下错误:

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

另一方面,如果我将上面的代码更改为下面的代码,结果将得到一个空图.

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

有办法解决这个问题吗?

谢谢,

解决方法:

既然你在这里有熊猫标签…

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

标签:pandas,bar-chart,plotly,python
来源: https://codeday.me/bug/20191108/2007905.html