Возможно, вы ошиблись, когда фильтровали данные по value
, предоставленному из dropdown
.Код:
from flask import Flask
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import pandas as pd
import plotly
import plotly.graph_objs as go
server = Flask(__name__)
app = dash.Dash(server=server)
df = pd.DataFrame({"Pos": [4, 5, 7, 8],
"Neg": [3, 6, 8, 9],
"Compound": [7, 11, 15, 17],
"values": [1, 2, 3, 4]})
app.layout = html.Div(
[
html.H1("Scatter with dropdown"),
dcc.Dropdown(
id='dropdown',
options=[{'label': 'Pos', 'value': 'Pos'},
{'label': 'Neg', 'value': 'Neg'},
{'label': 'Compound', 'value': 'Compound'}],
value='Compound'
),
html.Div(id='scatter'),
])
@app.callback(
Output("scatter", "children"),
[Input("dropdown", "value")],
)
def change_scatter(value):
"""Change scatter according to dropdown."""
global df
dff = df[['values', value]]
return html.Div(dcc.Graph(
id='scatter-plot',
figure={
'data': [
{
'x': dff['values'],
'y': dff[value],
'type': 'scatter',
# 'text': dff[value],
'name': value
}
],
'layout': {
'xaxis': {'title': 'Position'},
'yaxis': {'title': 'Values'},
}
}
))
if __name__ == "__main__":
app.run_server(debug=True, port=8888)
Вывод: Надеюсь, этот пример поможет вам найти ошибку