0 1 A 色>td style="ext-align:right;">0.44 <1td>401 2 2 3 4 5 6 7 8 9 10 11 12 13 14 15
我想知道是否有人能帮我。我在多个网站上搜索了大量的答案,想知道我现在的处境,我相信我已经接近答案了,但我正在为最后一点而挣扎。
我正在尝试使用plotly express 制作一个交互式图形
我有一个看起来像这样的数据帧:
标签- 更新菜单是静态的,每个下拉菜单都是独立的。因此,为了提供动态和链接的外观,请构建一个这样做的静态结构
- 下面的代码显示了这一点,但没有重新生成列选择器
- IMHO,切换到短划线要简单得多,在那里,使用回调与图形交互以实现您想要的行为变得非常简单
fig = px.bar(
df_test,
y="value1",
)
# Create a button list for the value columns
buttonlist = [
{
"args": [
{
"y": [df_test.loc[df_test["color"].eq("red"), col]],
"updatemenus": [
{"buttons":[{"label":"Back","method":"relayout","args":[{}]}]},
{
"buttons": [
{
"label": f"Color {c}",
"method": "restyle",
"args": [
{"y": [df_test.loc[df_test["color"].eq(c), col]]}
],
}
for c in df_test["color"].unique()
],
"y": 0.9,
}
],
}
],
"label": str(col),
"method": "relayout",
}
for col in df_test.columns[3:]
]
# Update the layout to include the dropdown menus, and to show titles etc
fig.update_layout(
title="Test",
yaxis_title="value",
xaxis_title="Color",
updatemenus=[
{"buttons":buttonlist, "showactive":True},
],
)
使用短划线
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.express as px
import pandas as pd
import io
df_test = pd.read_csv(io.StringIO(""" num label color value1 value2 value3
1 A red 0.4 4 40
2 A blue 0.2 2 20
3 A green 0.3 3 30
4 A red 0.6 6 60
5 A blue 0.7 7 70
6 A green 0.4 4 40
7 B blue 0.2 2 20
8 B green 0.4 4 40
9 B red 0.4 4 40
10 B green 0.2 2 20
11 C red 0.1 1 10
12 C blue 0.3 3 30
13 D red 0.8 8 80
14 D blue 0.4 4 40
15 D green 0.6 6 60
16 D yellow 0.5 5 50"""), sep="s+")
# Build App
app = JupyterDash(__name__)
app.layout = html.Div(
[
dcc.Dropdown(
id="color",
options=[{"label": x, "value": x} for x in df_test["color"].unique()],
value="red",
),
dcc.Dropdown(
id="value",
options=[{"label": x, "value": x} for x in df_test.columns[3:]],
value="value1",
),
dcc.Graph(id="graph1"),
]
)
@app.callback(
Output("graph1", "figure"),
Input("color", "value"),
Input("value", "value"),
)
def update_graph1(color, value):
print(color, value)
return px.bar(df_test.loc[df_test["color"].eq(color)], y=value)
# Run app and display result inline in the notebook
app.run_server(mode="inline")