嗨,我目前正在尝试在python破折号上构建散点图,在这里我可以将下拉列表(由不同的列名组成)连接到散点图。因此,当我更改下拉列表的值时,散点图将根据下拉列表的值显示不同的值。但是,我的问题是列名本质上是一个字符串,并且下拉列表由这些值的字典组成,因此很难传递。这是我现在拥有的代码:
html.Div([dcc.Dropdown(
id='mood',
options=[
{'label': 'Positive', 'value': 'Positive'},
{'label': 'Negative', 'value': 'Negative'},
{'label': 'Compound', 'value': 'Compound'}],
value='Compound')], style={'width':'15%'}
),
html.Div([
dcc.Graph(id='linear')]),
html.Div([
dcc.Graph(id='linear2')])
])
@app.callback(
dash.dependencies.Output('linear','figure'),
[dash.dependencies.Input('mood','value')])
def update_graph(mood_name):
y=file.get(column) ==mood_name]
scatter=go.scatter(y=y,marker=dict(
color='rgb(0,191,255)', # code for sky blue 0,191,255
line=dict(
color='rgb(8,48,107)',
width=1.5,
)),opacity=0.6,name='Sentiment')
layout=go.Layout(xaxis={'title': 'Tweets'},
yaxis={'title': 'Polarity'},
title= 'Tweets',
hovermode='closest')
return {'data':[scatter],
'layout':[layout]}
def更新图函数在传递这些列名(正,负和复合)时遇到麻烦。任何帮助将不胜感激,谢谢!
答案 0 :(得分:0)
在从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)