使用破折号绘制带有下拉菜单的 y 轴图
plotly graph y-axis with dropdown using dash
我无法创建响应图:
有人知道我做错了什么吗?
# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
html.H1("Index Scatterplot"),
dcc.Graph(id='graph'),
html.Label([
"constituent",
dcc.Dropdown(
id='constituent-dropdown', clearable=False,
value='AAPL.O', options=[
{'label': c, 'value': c}
for c in ['AAPL.O', 'AMZN.O', 'TSLA.O']
]),
])# Define callback to update graph
@app.callback(
Output('graph', 'figure'),
[Input("constituent-dropdown", "value")]
)
def update_figure(constituent):
return px.scatter(
data, x=".SPX", y=constituent, color="size",
color_continuous_scale='plasma',
render_mode="webgl", title="Return Distribution"
)# Run app and display result inline in the notebook
app.run_server(mode='inline', port=XXXX)
文件“C:xxxx.py”,第 21 行
def update_figure(成分):
^
语法错误:语法无效
这是我使用的基本代码,用于更改颜色。
# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
html.H1("Index Scatterplot"),
dcc.Graph(id='graph'),
html.Label([
"colorscale",
dcc.Dropdown(
id='colorscale-dropdown', clearable=False,
value='plasma', options=[
{'label': c, 'value': c}
for c in px.colors.named_colorscales()
])
]),
])# Define callback to update graph
@app.callback(
Output('graph', 'figure'),
[Input("colorscale-dropdown", "value")]
)
def update_figure(colorscale):
return px.scatter(
data, x=".SPX", y="AAPL.O", color="size",
color_continuous_scale=colorscale,
render_mode="webgl", title="Return Distribution"
)# Run app and display result inline in the notebook
app.run_server(mode='inline', port=XXXX)
感谢任何帮助。
- 为了使您的代码正常工作,我需要获取 数据
- 您的核心问题是不匹配的方括号和圆括号。我建议使用代码格式化程序来帮助解决这个问题
- 已包含在回调
中定义列和色阶的功能
import plotly.express as px
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import dash
from jupyter_dash import JupyterDash
import pandas_datareader.data as web
import datetime as dt
data = (
web.DataReader(
"sp500", "fred", start="2021-01-01", end=dt.datetime.now().strftime("%d-%b-%Y")
)
.rename(columns={"sp500": ".SPX"})
.join(
web.DataReader(
["AAPL", "AMZN", "TSLA"], "yahoo", start="2021-01-01", end="2021-08-29"
)["Open"].pipe(lambda d: d.rename(columns={c: f"{c}.O" for c in d.columns})),
how="inner",
)
).assign(size=lambda d: d.mean(axis=1))
# Build App
app = JupyterDash(__name__)
app.layout = html.Div(
[
html.H1("Index Scatterplot"),
dcc.Graph(id="graph"),
html.Label(
[
"constituent",
dcc.Dropdown(
id="constituent-dropdown",
clearable=False,
value="AAPL.O",
options=[
{"label": c, "value": c} for c in data.columns if ".O" in c
],
),
]
),
html.Label(
[
"colorscale",
dcc.Dropdown(
id="colorscale-dropdown",
clearable=False,
value="plasma",
options=[
{"label": c, "value": c} for c in px.colors.named_colorscales()
],
),
]
),
]
)
# Define callback to update graph
@app.callback(Output("graph", "figure"), Input("constituent-dropdown", "value"), Input("colorscale-dropdown","value"))
def update_figure(constituent, colorscale):
return px.scatter(
data,
x=".SPX",
y=constituent,
color="size",
color_continuous_scale=colorscale,
render_mode="webgl",
title="Return Distribution",
) # Run app and display result inline in the notebook
app.run_server(mode="inline")
我无法创建响应图:
有人知道我做错了什么吗?
# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
html.H1("Index Scatterplot"),
dcc.Graph(id='graph'),
html.Label([
"constituent",
dcc.Dropdown(
id='constituent-dropdown', clearable=False,
value='AAPL.O', options=[
{'label': c, 'value': c}
for c in ['AAPL.O', 'AMZN.O', 'TSLA.O']
]),
])# Define callback to update graph
@app.callback(
Output('graph', 'figure'),
[Input("constituent-dropdown", "value")]
)
def update_figure(constituent):
return px.scatter(
data, x=".SPX", y=constituent, color="size",
color_continuous_scale='plasma',
render_mode="webgl", title="Return Distribution"
)# Run app and display result inline in the notebook
app.run_server(mode='inline', port=XXXX)
文件“C:xxxx.py”,第 21 行 def update_figure(成分): ^ 语法错误:语法无效
这是我使用的基本代码,用于更改颜色。
# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
html.H1("Index Scatterplot"),
dcc.Graph(id='graph'),
html.Label([
"colorscale",
dcc.Dropdown(
id='colorscale-dropdown', clearable=False,
value='plasma', options=[
{'label': c, 'value': c}
for c in px.colors.named_colorscales()
])
]),
])# Define callback to update graph
@app.callback(
Output('graph', 'figure'),
[Input("colorscale-dropdown", "value")]
)
def update_figure(colorscale):
return px.scatter(
data, x=".SPX", y="AAPL.O", color="size",
color_continuous_scale=colorscale,
render_mode="webgl", title="Return Distribution"
)# Run app and display result inline in the notebook
app.run_server(mode='inline', port=XXXX)
感谢任何帮助。
- 为了使您的代码正常工作,我需要获取 数据
- 您的核心问题是不匹配的方括号和圆括号。我建议使用代码格式化程序来帮助解决这个问题
- 已包含在回调 中定义列和色阶的功能
import plotly.express as px
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import dash
from jupyter_dash import JupyterDash
import pandas_datareader.data as web
import datetime as dt
data = (
web.DataReader(
"sp500", "fred", start="2021-01-01", end=dt.datetime.now().strftime("%d-%b-%Y")
)
.rename(columns={"sp500": ".SPX"})
.join(
web.DataReader(
["AAPL", "AMZN", "TSLA"], "yahoo", start="2021-01-01", end="2021-08-29"
)["Open"].pipe(lambda d: d.rename(columns={c: f"{c}.O" for c in d.columns})),
how="inner",
)
).assign(size=lambda d: d.mean(axis=1))
# Build App
app = JupyterDash(__name__)
app.layout = html.Div(
[
html.H1("Index Scatterplot"),
dcc.Graph(id="graph"),
html.Label(
[
"constituent",
dcc.Dropdown(
id="constituent-dropdown",
clearable=False,
value="AAPL.O",
options=[
{"label": c, "value": c} for c in data.columns if ".O" in c
],
),
]
),
html.Label(
[
"colorscale",
dcc.Dropdown(
id="colorscale-dropdown",
clearable=False,
value="plasma",
options=[
{"label": c, "value": c} for c in px.colors.named_colorscales()
],
),
]
),
]
)
# Define callback to update graph
@app.callback(Output("graph", "figure"), Input("constituent-dropdown", "value"), Input("colorscale-dropdown","value"))
def update_figure(constituent, colorscale):
return px.scatter(
data,
x=".SPX",
y=constituent,
color="size",
color_continuous_scale=colorscale,
render_mode="webgl",
title="Return Distribution",
) # Run app and display result inline in the notebook
app.run_server(mode="inline")