Sco*_*son 5 python plotly plotly-dash
我试图返回一个条形图,但用于条形图的列根据下拉值选择而变化。我将字典返回到 graph.figure 输出,但收到以下错误;
dash.exceptions.InvalidCallbackReturnValue:
The callback for property \figure``
of component \graph-one` returned a value`
which is not JSON serializable.
In general, Dash properties can only be
dash components, strings, dictionaries, numbers, None,
or lists of those.
Run Code Online (Sandbox Code Playgroud)
我的返回值是一本字典。代码如下;
def create_grouped_bar(df):
return [
go.Bar(
x=df['date'],
y=df[col],
text=df[col],
hoverinfo='text+name',
name=col.title(),
)
for col in df.columns
]
@app.callback(
Output('graph-one', 'figure'),
[Input('filtered_df', 'children'),
Input('freq-radio', 'value'),
Input('graph-one-drop', 'value'),
Input('graph-one-radio', 'value')])
def update_graph(df_json, freq, cols, amount):
dff = pd.read_json(df_json, orient='split')
plot_cols = falls_dd_dict[cols]
plot_df = dff[['date_time_occurred']+plot_cols].copy()
plot_df['date_time_occurred'] =
pd.to_datetime(plot_df.date_time_occurred)
plot_df['date'] = plot_df['date_time_occurred'].dt.to_period(freq)
plot_df = plot_df.groupby(['date']).count()
if amount != 'all':
included_cols = plot_df.sum().sort_values(ascending=False) [:int(amount)].index.tolist()
plot_df = plot_df[included_cols]
bars = create_grouped_bar(plot_df.reset_index())
figure = {'data': bars,
'layout': go.Layout(
title='Changes in Enrollment',
margin={'pad': 3, 'l': 45, 'r': 35, 't': 55, 'b': 25},
barmode='group',
xaxis={
'title': 'Month',
'showgrid': False,
'showline': True,
},
yaxis={
'title': 'Census',
'showgrid': False,
},
legend=dict(orientation="h", y=-0.15)
)
}
return figure
Run Code Online (Sandbox Code Playgroud)
当我打印图时,它看起来像这样;
{'data': [Bar({
'hoverinfo': 'text+name',
'name': 'Date',
'text': array(['2017-12', '2018-01', '2018-02', '2018-03', '2018-04', '2018-05',
'2018-06', '2018-07', '2018-08', '2018-09'], dtype='<U7'),
'x': array([Period('2017-12', 'M'), Period('2018-01', 'M'), Period('2018-02', 'M'),
Period('2018-03', 'M'), Period('2018-04', 'M'), Period('2018-05', 'M'),
Period('2018-06', 'M'), Period('2018-07', 'M'), Period('2018-08', 'M'),
Period('2018-09', 'M')], dtype=object),
'y': array([Period('2017-12', 'M'), Period('2018-01', 'M'), Period('2018-02', 'M'),
Period('2018-03', 'M'), Period('2018-04', 'M'), Period('2018-05', 'M'),
Period('2018-06', 'M'), Period('2018-07', 'M'), Period('2018-08', 'M'),
Period('2018-09', 'M')], dtype=object)
}), Bar({
'hoverinfo': 'text+name',
'name': 'Location',
'text': array(['29', '37', '39', '28', '22', '29', '40', '24', '43', '29'], dtype='<U2'),
'x': array([Period('2017-12', 'M'), Period('2018-01', 'M'), Period('2018-02', 'M'),
Period('2018-03', 'M'), Period('2018-04', 'M'), Period('2018-05', 'M'),
Period('2018-06', 'M'), Period('2018-07', 'M'), Period('2018-08', 'M'),
Period('2018-09', 'M')], dtype=object),
'y': array([29, 37, 39, 28, 22, 29, 40, 24, 43, 29])
}), Bar({
'hoverinfo': 'text+name',
'name': 'Date_Time_Occurred',
'text': array(['29', '37', '39', '28', '22', '29', '40', '24', '43', '29'], dtype='<U2'),
'x': array([Period('2017-12', 'M'), Period('2018-01', 'M'), Period('2018-02', 'M'),
Period('2018-03', 'M'), Period('2018-04', 'M'), Period('2018-05', 'M'),
Period('2018-06', 'M'), Period('2018-07', 'M'), Period('2018-08', 'M'),
Period('2018-09', 'M')], dtype=object),
'y': array([29, 37, 39, 28, 22, 29, 40, 24, 43, 29])
})], 'layout': Layout({
'barmode': 'group',
'legend': {'orientation': 'h', 'y': -0.15},
'margin': {'b': 25, 'l': 45, 'pad': 3, 'r': 35, 't': 55},
'title': 'Changes in Enrollment',
'xaxis': {'showgrid': False, 'showline': True, 'title': 'Month'},
'yaxis': {'showgrid': False, 'title': 'Census'}
})}
Run Code Online (Sandbox Code Playgroud)
我几乎尝试了plotly api的所有变体,包括;
返回实际的 dcc.Graph 对象作为 html.Div 的子对象,将 bar 列表设置为跟踪字典列表而不是 go.Bar() 并使用 dict(data=bars, layout=layout) 而不是字典多于。
小智 1
我有类似的事情。我花了很长时间才意识到,在我的例子中,我的更新函数需要输出两个列表元素,因为我有两个output()。在你的情况下,我的预感是,问题可能出在输入端,你有 4 个值,其中一个值的 ID 是否可能有拼写错误),而输出端没有问题。
| 归档时间: |
|
| 查看次数: |
6781 次 |
| 最近记录: |