gpt4 book ai didi

plotly - 如何将 Dash 回调装饰器适本地应用于包含选择的 Plotly figurewidget

转载 作者:行者123 更新时间:2023-12-04 09:43:20 26 4
gpt4 key购买 nike

我正在尝试使用具有多个图表元素的交互式小部件作为发现 here并将其应用到 Dash 中,以便我可以进行多图表刷:

我在破折号中适本地显示了小部件,我可以使用套索进行选择,重新着色点,但不会更新桑基。我搜索并发现这可能是一个“回调”问题,但似乎没有一个解决这个特定问题。有人可以解释一下我缺少什么吗?

阴谋:

def chart1():
cars_df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/imports-85.csv')

# Build parcats dimensions
categorical_dimensions = ['body-style', 'drive-wheels', 'fuel-type'];

dimensions = [dict(values=cars_df[label], label=label) for label in categorical_dimensions]

# Build colorscale
color = np.zeros(len(cars_df), dtype='uint8')
colorscale = [[0, '#167b7e'], [1, '#4b3268']]

# Build figure as FigureWidget
fig = go.FigureWidget(
data=[

go.Scatter(x=cars_df.horsepower, y=cars_df['highway-mpg'],
marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'firebrick'}},
unselected={'marker': {'opacity': 0.4}}),

go.Parcats(
domain={'y': [0, 0.4]}, dimensions=dimensions,
line={'colorscale': colorscale, 'cmin': 0,
'cmax': 1, 'color': color, 'shape': 'hspline'})
])

fig.update_layout(
height=800,
xaxis={'title': 'Horsepower'},
yaxis={'title': 'MPG', 'domain': [0.6, 1]},
dragmode='lasso',
hovermode='closest',
# plot_bgcolor='rgba(0, 0, 0, 0)',
paper_bgcolor='rgba(0, 0, 0, 0)',
autosize=False,
bargap=0.35,
font={"family": "Questrial", "size": 10})

# Update color callback

# @app.callback(Output("bigchart", "children"), [Input("points.point_inds", "value")]) <--- an attempt
def update_color(trace, points, state):
# Update scatter selection
fig.data[0].selectedpoints = points.point_inds

# Update parcats colors
new_color = np.zeros(len(cars_df), dtype='uint8')
new_color[points.point_inds] = 1
fig.data[1].line.color = new_color

# Register callback on scatter selection...
fig.data[0].on_selection(update_color)
# and parcats click
fig.data[1].on_click(update_color)

return fig

地块位置:
dcc.Graph(id="bigchart",figure=chart1())

Plot

抱歉,我会在线发布图片,但没有代表。谢谢你。

最佳答案

如果您想在 Dash 中实现交互性,您应该将所有回调转换为 Dash 语法。这是您在 Dash 中的示例的一种可能实现,

import dash
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import dash_core_components as dcc
import dash_html_components as html

from dash.dependencies import Input, Output, State

cars_df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/imports-85.csv')
# Build parcats dimensions
categorical_dimensions = ['body-style', 'drive-wheels', 'fuel-type']
dimensions = [dict(values=cars_df[label], label=label) for label in categorical_dimensions]
# Build colorscale.
color = np.zeros(len(cars_df), dtype='uint8')
colorscale = [[0, '#167b7e'], [1, '#4b3268']]


def build_figure():
fig = go.Figure(
data=[
go.Scatter(x=cars_df.horsepower, y=cars_df['highway-mpg'],
marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'firebrick'}},
unselected={'marker': {'opacity': 0.4}}),
go.Parcats(
domain={'y': [0, 0.4]}, dimensions=dimensions,
line={'colorscale': colorscale, 'cmin': 0,
'cmax': 1, 'color': color, 'shape': 'hspline'})
])
fig.update_layout(
height=800,
xaxis={'title': 'Horsepower'},
yaxis={'title': 'MPG', 'domain': [0.6, 1]},
dragmode='lasso',
hovermode='closest',
# plot_bgcolor='rgba(0, 0, 0, 0)',
paper_bgcolor='rgba(0, 0, 0, 0)',
autosize=False,
bargap=0.35,
font={"family": "Questrial", "size": 10})
return fig


app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([dcc.Graph(figure=build_figure(), id="graph")])


@app.callback(Output("graph", "figure"), [Input("graph", "selectedData"), Input("graph", "clickData")],
[State("graph", "figure")])
def update_color(selectedData, clickData, fig):
selection = None
# Update selection based on which event triggered the update.
trigger = dash.callback_context.triggered[0]["prop_id"]
if trigger == 'graph.clickData':
selection = [point["pointNumber"] for point in clickData["points"]]
if trigger == 'graph.selectedData':
selection = [point["pointIndex"] for point in selectedData["points"]]
# Update scatter selection
fig["data"][0]["selectedpoints"] = selection
# Update parcats colors
new_color = np.zeros(len(cars_df), dtype='uint8')
new_color[selection] = 1
fig["data"][1]["line"]["color"] = new_color
return fig


if __name__ == '__main__':
app.run_server()

关于plotly - 如何将 Dash 回调装饰器适本地应用于包含选择的 Plotly figurewidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62235672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com