回调说 x 和 y 不能同时是列引用列表或 o 列列表的问题
Ploty problem with callbacks saying x and y cannot be both list of column references or list o columns
考虑以下在 flask 应用程序中使用的破折号应用程序:
"""Instantiate a Dash app."""
import dash
from dash import dcc, Input, Output, html
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from .layout import html_layout
from .data import create_cluster_dataframe, create_stacked_dataframe
def init_dashboard(server):
"""Create a Plotly Dash dashboard."""
dash_app = dash.Dash(
server=server,
routes_pathname_prefix="/dashapp/",
external_stylesheets=[
"/static/dist/css/styles.css",
"https://fonts.googleapis.com/css?family=Lato",
],
)
# Load DataFrame
df = create_stacked_dataframe()
df1 = create_cluster_dataframe()
# Custom HTML layout
dash_app.index_string = html_layout
# Create Layout
dash_app.title = "ActiSleep Monitoring App"
dash_app.layout = html.Div(
children=[
dcc.Graph(
id="histogram-graph",
figure=go.Figure(
data=[
go.Bar(
x=df.loc[df["Label"] == "Sleep", "time"],
y=df.loc[df["Label"] == "Sleep", "percentage"],
name="Sleep",
marker_color=df.loc[df["Label"] == "Sleep", "color"].iloc[
0
],
),
go.Bar(
x=df.loc[df["Label"] == "Vigorous", "time"],
y=df.loc[df["Label"] == "Vigorous", "percentage"],
name="Vigorous",
marker_color=df.loc[
df["Label"] == "Vigorous", "color"
].iloc[0],
),
go.Bar(
x=df.loc[df["Label"] == "Moderate", "time"],
y=df.loc[df["Label"] == "Moderate", "percentage"],
name="Moderate",
marker_color=df.loc[
df["Label"] == "Moderate", "color"
].iloc[0],
),
go.Bar(
x=df.loc[df["Label"] == "Light", "time"],
y=df.loc[df["Label"] == "Light", "percentage"],
name="Light",
marker_color=df.loc[df["Label"] == "Light", "color"].iloc[
0
],
),
go.Bar(
x=df.loc[df["Label"] == "Sedentary", "time"],
y=df.loc[df["Label"] == "Sedentary", "percentage"],
name="Sedentary",
marker_color=df.loc[
df["Label"] == "Sedentary", "color"
].iloc[0],
),
],
layout=go.Layout(
barmode="stack",
title="Activity percentage during the day(average)",
xaxis=dict(title="Hour of the day"),
yaxis=dict(title="Duration %"),
),
),
style={"height": "600px"},
),
html.Hr(),
html.H3(
"Cluster Data and Projections using tsne and umap",
style={"margin-left": "20px"},
),
html.Div(
className="row",
children=[
html.Div(
children=[
html.Label(
["Algorithm:"],
style={"font-weight": "bold", "text-align": "center"},
),
dcc.Dropdown(
["Agglomerative", "K-Medoid", "Spectral"],
id="algorithm",
value="Agglomerative"
),
],
style={"width": "33.33%", "display": "inline-block"},
),
html.Div(
children=[
html.Label(
["Metric:"],
style={"font-weight": "bold", "text-align": "center"},
),
dcc.Dropdown(
["Edit", "Euclidean", "Wasserstein"],
id="metric",
value="Edit"
),
],
style={"width": "33.33%", "display": "inline-block"},
),
html.Div(
children=[
html.Label(
["No. of Clusters:"],
style={"font-weight": "bold", "text-align": "center"},
),
dcc.Dropdown(
[2, 3, 4, 5],
id="n_clusters",
value=3
),
],
style={"width": "33.33%", "display": "inline-block"},
),
],
style={"width": "90%", "margin": "auto"},
),
dcc.Graph(
id="tsne",
figure=go.Figure(
data=px.scatter(
x=df1.loc[
(df1["algorithm_name"] == "spectral_clustering_edit")
& (df1["n_clusters"] == 3)
& (df1["metric"] == "edit"),
"tsne_x",
],
y=df1.loc[
(df1["algorithm_name"] == "spectral_clustering_edit")
& (df1["n_clusters"] == 3)
& (df1["metric"] == "edit"),
"tsne_y",
],
color=[x for x in df1["hue"]],
)
),
style={"display": "inline-block"},
),
dcc.Graph(
id="umap",
figure=go.Figure(
data=px.scatter(
x=df1.loc[
(df1["algorithm_name"] == "spectral_clustering_edit")
& (df1["n_clusters"] == 3)
& (df1["metric"] == "edit"),
"umap_x",
],
y=df1.loc[
(df1["algorithm_name"] == "spectral_clustering_edit")
& (df1["n_clusters"] == 3)
& (df1["metric"] == "edit"),
"umap_y",
],
color=[x for x in df1["hue"]],
)
),
style={"display": "inline-block"},
),
]
)
init_callbacks(dash_app)
return dash_app.server
def init_callbacks(dash_app):
@dash_app.callback(
[Output("tsne", "figure1"),
Output("umap", "figure2")],
[Input("algorithm", "value"),
Input("metric", "value"),
Input("n_clusters", "value")],
)
def update_plots(algorithm, metric, n_clusters):
df1 = create_cluster_dataframe()
figure1 = go.Figure(
data=px.scatter(
x=df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric),
"tsne_x",
],
y=df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric),
"tsne_y",
],
color=[x for x in df1["hue"]],
)
)
figure2 = go.Figure(
data=px.scatter(
x=df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric),
"umap_x",
],
y=df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric),
"umap_y",
],
color=[x for x in df1["hue"]],
)
)
return figure1, figure2
它适用于初始图,但在回调中它给出错误 ValueError: Cannot accept list of column references or list of columns for both
xand
y.
我必须在回调中使用数据之前过滤数据。现在看起来像下面这样:
def init_callbacks(dash_app):
@dash_app.callback(
[Output("tsne", "figure1"), Output("umap", "figure2")],
[
Input("algorithm", "value"),
Input("metric", "value"),
Input("n_clusters", "value"),
],
)
def update_plots(algorithm, metric, n_clusters):
df1 = create_cluster_dataframe()
print(df1)
filtered_df = df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric)
]
figure1 = go.Figure(
data=px.scatter(
filtered_df,
x="tsne_x",
y="tsne_y",
color=[x for x in df1["hue"]],
)
)
figure2 = go.Figure(
data=px.scatter(
filtered_df,
x="umap_x",
y="umap_y",
color=[x for x in df1["hue"]],
)
)
return figure1, figure2
考虑以下在 flask 应用程序中使用的破折号应用程序:
"""Instantiate a Dash app."""
import dash
from dash import dcc, Input, Output, html
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from .layout import html_layout
from .data import create_cluster_dataframe, create_stacked_dataframe
def init_dashboard(server):
"""Create a Plotly Dash dashboard."""
dash_app = dash.Dash(
server=server,
routes_pathname_prefix="/dashapp/",
external_stylesheets=[
"/static/dist/css/styles.css",
"https://fonts.googleapis.com/css?family=Lato",
],
)
# Load DataFrame
df = create_stacked_dataframe()
df1 = create_cluster_dataframe()
# Custom HTML layout
dash_app.index_string = html_layout
# Create Layout
dash_app.title = "ActiSleep Monitoring App"
dash_app.layout = html.Div(
children=[
dcc.Graph(
id="histogram-graph",
figure=go.Figure(
data=[
go.Bar(
x=df.loc[df["Label"] == "Sleep", "time"],
y=df.loc[df["Label"] == "Sleep", "percentage"],
name="Sleep",
marker_color=df.loc[df["Label"] == "Sleep", "color"].iloc[
0
],
),
go.Bar(
x=df.loc[df["Label"] == "Vigorous", "time"],
y=df.loc[df["Label"] == "Vigorous", "percentage"],
name="Vigorous",
marker_color=df.loc[
df["Label"] == "Vigorous", "color"
].iloc[0],
),
go.Bar(
x=df.loc[df["Label"] == "Moderate", "time"],
y=df.loc[df["Label"] == "Moderate", "percentage"],
name="Moderate",
marker_color=df.loc[
df["Label"] == "Moderate", "color"
].iloc[0],
),
go.Bar(
x=df.loc[df["Label"] == "Light", "time"],
y=df.loc[df["Label"] == "Light", "percentage"],
name="Light",
marker_color=df.loc[df["Label"] == "Light", "color"].iloc[
0
],
),
go.Bar(
x=df.loc[df["Label"] == "Sedentary", "time"],
y=df.loc[df["Label"] == "Sedentary", "percentage"],
name="Sedentary",
marker_color=df.loc[
df["Label"] == "Sedentary", "color"
].iloc[0],
),
],
layout=go.Layout(
barmode="stack",
title="Activity percentage during the day(average)",
xaxis=dict(title="Hour of the day"),
yaxis=dict(title="Duration %"),
),
),
style={"height": "600px"},
),
html.Hr(),
html.H3(
"Cluster Data and Projections using tsne and umap",
style={"margin-left": "20px"},
),
html.Div(
className="row",
children=[
html.Div(
children=[
html.Label(
["Algorithm:"],
style={"font-weight": "bold", "text-align": "center"},
),
dcc.Dropdown(
["Agglomerative", "K-Medoid", "Spectral"],
id="algorithm",
value="Agglomerative"
),
],
style={"width": "33.33%", "display": "inline-block"},
),
html.Div(
children=[
html.Label(
["Metric:"],
style={"font-weight": "bold", "text-align": "center"},
),
dcc.Dropdown(
["Edit", "Euclidean", "Wasserstein"],
id="metric",
value="Edit"
),
],
style={"width": "33.33%", "display": "inline-block"},
),
html.Div(
children=[
html.Label(
["No. of Clusters:"],
style={"font-weight": "bold", "text-align": "center"},
),
dcc.Dropdown(
[2, 3, 4, 5],
id="n_clusters",
value=3
),
],
style={"width": "33.33%", "display": "inline-block"},
),
],
style={"width": "90%", "margin": "auto"},
),
dcc.Graph(
id="tsne",
figure=go.Figure(
data=px.scatter(
x=df1.loc[
(df1["algorithm_name"] == "spectral_clustering_edit")
& (df1["n_clusters"] == 3)
& (df1["metric"] == "edit"),
"tsne_x",
],
y=df1.loc[
(df1["algorithm_name"] == "spectral_clustering_edit")
& (df1["n_clusters"] == 3)
& (df1["metric"] == "edit"),
"tsne_y",
],
color=[x for x in df1["hue"]],
)
),
style={"display": "inline-block"},
),
dcc.Graph(
id="umap",
figure=go.Figure(
data=px.scatter(
x=df1.loc[
(df1["algorithm_name"] == "spectral_clustering_edit")
& (df1["n_clusters"] == 3)
& (df1["metric"] == "edit"),
"umap_x",
],
y=df1.loc[
(df1["algorithm_name"] == "spectral_clustering_edit")
& (df1["n_clusters"] == 3)
& (df1["metric"] == "edit"),
"umap_y",
],
color=[x for x in df1["hue"]],
)
),
style={"display": "inline-block"},
),
]
)
init_callbacks(dash_app)
return dash_app.server
def init_callbacks(dash_app):
@dash_app.callback(
[Output("tsne", "figure1"),
Output("umap", "figure2")],
[Input("algorithm", "value"),
Input("metric", "value"),
Input("n_clusters", "value")],
)
def update_plots(algorithm, metric, n_clusters):
df1 = create_cluster_dataframe()
figure1 = go.Figure(
data=px.scatter(
x=df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric),
"tsne_x",
],
y=df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric),
"tsne_y",
],
color=[x for x in df1["hue"]],
)
)
figure2 = go.Figure(
data=px.scatter(
x=df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric),
"umap_x",
],
y=df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric),
"umap_y",
],
color=[x for x in df1["hue"]],
)
)
return figure1, figure2
它适用于初始图,但在回调中它给出错误 ValueError: Cannot accept list of column references or list of columns for both
xand
y.
我必须在回调中使用数据之前过滤数据。现在看起来像下面这样:
def init_callbacks(dash_app):
@dash_app.callback(
[Output("tsne", "figure1"), Output("umap", "figure2")],
[
Input("algorithm", "value"),
Input("metric", "value"),
Input("n_clusters", "value"),
],
)
def update_plots(algorithm, metric, n_clusters):
df1 = create_cluster_dataframe()
print(df1)
filtered_df = df1.loc[
(df1["algorithm_name"] == algorithm)
& (df1["n_clusters"] == n_clusters)
& (df1["metric"] == metric)
]
figure1 = go.Figure(
data=px.scatter(
filtered_df,
x="tsne_x",
y="tsne_y",
color=[x for x in df1["hue"]],
)
)
figure2 = go.Figure(
data=px.scatter(
filtered_df,
x="umap_x",
y="umap_y",
color=[x for x in df1["hue"]],
)
)
return figure1, figure2