I want to create a Scatter3D plot, which user can freely choose X, Y, and Z axis input values. But I always got key errors on the parameters of callback functions. Please help!
@app.callback(
Output('graph-with-slider', 'figure'),
[Input('year-slider', 'value'),
Input('xaxis-column', 'value'),
Input('yaxis-column', 'value'),
Input('zaxis-column', 'value'),
Input('xaxis-type', 'value'),
Input('yaxis-type', 'value'),
Input('zaxis-type', 'value')]
)
def update_figure(selected_year,
xname,
yname,
zname,
xaxis_type,
yaxis_type,
zaxis_type,):
filtered_df = df[df.year == selected_year]
traces = []
for i in filtered_df.continent.unique():
df_by_continent = filtered_df[filtered_df[‘continent’] == i]
traces.append(go.Scatter3d(
x=df_by_continent[‘xname’],
y=df_by_continent[‘yname’],
z=df_by_continent[‘zname’],
text=df_by_continent[‘country’],
mode=‘markers’,
opacity=0.5,
marker={
‘size’: 5,
‘line’: {‘width’: 0.5, ‘color’: ‘white’}
},
name=i
))
return {
'data': traces,
'layout': go.Layout({
'font': {'family': 'Raleway'},
'hovermode': 'closest',
'margin': {'r': 0, 't': 0, 'l': 0, 'b': 0},
'showlegend' : True,
'scene' : {
'xaxis': {'showbackground': True, 'backgroundcolor': 'rgb(230, 230, 230)', 'gridcolor': 'rgb(255, 255, 255)', 'title': xname, 'type' : 'linear' if xaxis_type == 'Linear' else 'log', 'zerolinecolor': 'rgb(255, 255, 255)'},
'yaxis': {'showbackground': True, 'backgroundcolor': 'rgb(230, 230, 230)', 'gridcolor': 'rgb(255, 255, 255)', 'title': yname, 'type' : 'linear' if yaxis_type == 'Linear' else 'log', 'zerolinecolor': 'rgb(255, 255, 255)'},
'zaxis': {'showbackground': True, 'backgroundcolor': 'rgb(230, 230, 230)', 'gridcolor': 'rgb(255, 255, 255)', 'title': zname, 'type' : 'linear' if zaxis_type == 'Linear' else 'log', 'zerolinecolor': 'rgb(255, 255, 255)'},
'camera': {'up': {'x': 0, 'y': 0, 'z': 1}, 'center': {'x': 0, 'y': 0, 'z': 0}, 'eye': {'x': 0.08, 'y': 2.2, 'z': 0.08}}
}
})
}
Below are the error messages:
* Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
127.0.0.1 - - [25/Sep/2018 20:36:14] “GET / HTTP/1.1” 200 -
127.0.0.1 - - [25/Sep/2018 20:36:14] “GET /_dash-layout HTTP/1.1” 200 -
127.0.0.1 - - [25/Sep/2018 20:36:14] “GET /_dash-dependencies HTTP/1.1” 200 -
127.0.0.1 - - [25/Sep/2018 20:36:14] “GET /favicon.ico HTTP/1.1” 200 -
[2018-09-25 20:36:15,014] ERROR in app: Exception on /_dash-update-component [POST]
Traceback (most recent call last):
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py”, line 3063, in get_loc
return self._engine.get_loc(key)
File “pandas_libs\index.pyx”, line 140, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\index.pyx”, line 162, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\hashtable_class_helper.pxi”, line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas_libs\hashtable_class_helper.pxi”, line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘xname’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 2292, in wsgi_app
response = self.full_dispatch_request()
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_compat.py”, line 35, in reraise
raise value
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1813, in full_dispatch_request
rv = self.dispatch_request()
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py”, line 1799, in dispatch_request
return self.view_functionsrule.endpoint
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\dash\dash.py”, line 871, in dispatch
return self.callback_map[target_id]‘callback’
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\dash\dash.py”, line 811, in add_context
output_value = func(*args, **kwargs)
File “”, line 104, in update_figure
x=df_by_continent[‘xname’],
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py”, line 2685, in getitem
return self._getitem_column(key)
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py”, line 2692, in _getitem_column
return self._get_item_cache(key)
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py”, line 2486, in _get_item_cache
values = self._data.get(item)
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py”, line 4115, in get
loc = self.items.get_loc(item)
File “C:\Users\ychen\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py”, line 3065, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File “pandas_libs\index.pyx”, line 140, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\index.pyx”, line 162, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\hashtable_class_helper.pxi”, line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas_libs\hashtable_class_helper.pxi”, line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘xname’
127.0.0.1 - - [25/Sep/2018 20:36:15] “POST /_dash-update-component HTTP/1.1” 500 -