Python 更新图形时出错无效的组件属性图形
仅从excel文件创建一个简单的图形,即可正确加载,但在更新图形时,破折号会返回此错误:Python 更新图形时出错无效的组件属性图形,python,plotly,plotly-dash,Python,Plotly,Plotly Dash,仅从excel文件创建一个简单的图形,即可正确加载,但在更新图形时,破折号会返回此错误: Failed component prop type: Invalid component prop figure key props supplied to Graph. Bad object: { “props”: { “id”: “average_country”, “figure”: { “data”: [ { “x”: [ “DE”, “ES”, “FR”, “IT”, “UK” ],
Failed component prop type: Invalid component prop figure key props supplied to Graph.
Bad object: {
“props”: {
“id”: “average_country”,
“figure”: {
“data”: [
{
“x”: [
“DE”,
“ES”,
“FR”,
“IT”,
“UK”
],
“y”: [
[
2365.56,
4528.33875,
4851.085,
4325.14,
2107.921428571429
]
],
“type”: “bar”
}
],
“layout”: {
“title”: “Hyperzone”
}
}
},
“type”: “Graph”,
“namespace”: “dash_core_components”
}
Valid keys: [
“data”,
“layout”,
“frames”
]
我只是用一个数据透视表来创建一些国家的数据平均值,这是我的代码,数据透视表是正确的,并用我想要的值输出一个有效的结果,当我尝试用回调函数更新图形时,它返回一条错误消息
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from dash.dependencies import Input, Output
HP= pd.read_excel(r’C:\Users\salinejs\HP.xlsx’)
Columns = [‘Station’, ‘Week’, ‘Owner’, ‘Cost’,‘Country’]
HP_filtered = HP[Columns]
avaliable_weeks= HP_filtered[‘Week’].unique()
external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]
app = dash.Dash(name, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.Div([
html.Div([
html.Div([
dcc.Dropdown(
id= 'Week_filter',
options = [{'label':j, 'value':j}for j in avaliable_weeks],
value= 'Week 42'
),
],
style= {'float':'left', 'display':'inline-block'}
),
html.Div([
dcc.Graph(
id='average_country',
)
],
)
]
),
],
)
@app.callback(
Output(‘average_country’, ‘figure’),
[Input(‘Week_filter’,‘value’)]
)
def update_value(week):
HP_filtered_week = HP_filtered[ HP_filtered['Week'] == week ]
pv = pd.pivot_table(HP_filtered_week, values='Cost', columns='Country', index='Week')
print(pv)
print(pv.columns)
return dcc.Graph(
id='average_country',
figure={
'data': [{'x': pv.columns , 'y' : pv.values,'type': 'bar'
}],
'layout':{
'title': 'Cost',
}
})
if name == ‘main’:
app.run_server(debug=True)
有点晚了-你现在可能已经整理好了,但我只是有同样的错误,这里的答案可以节省我的时间 这是因为您将回调输出发送到figure,但函数返回的是整个dcc.Graph(),而不仅仅是figure 改变
return dcc.Graph(
id='average_country',
figure={
'data': [{'x': pv.columns , 'y' : pv.values,'type': 'bar'
}],
'layout':{
'title': 'Cost',
}
})
到
有点晚了-你现在可能已经整理好了,但我只是有同样的错误,这里的答案可以节省我的时间 这是因为您将回调输出发送到figure,但函数返回的是整个dcc.Graph(),而不仅仅是figure 改变
return dcc.Graph(
id='average_country',
figure={
'data': [{'x': pv.columns , 'y' : pv.values,'type': 'bar'
}],
'layout':{
'title': 'Cost',
}
})
到