猿问

在仪表板中,根据复选框的值绘制数据集的子集

我正在使用Dash绘制一个数据集,其中包含 x、y 列和水果类型的另一列。我在清单中添加了苹果和葡萄。我现在想要做的是让图表显示葡萄的 x 和 y 值,如果葡萄复选框被勾选,苹果如果苹果复选框被勾选,如果两者都被勾选。


有人可以帮助我吗?这是我知道的一个非常简单的问题。


我知道我需要以某种方式更改“def ... return”部分。


import dash

from dash.dependencies import Input, Output

import dash_core_components as dcc

import dash_html_components as html

import pandas as pd

import plotly.graph_objs as go


app = dash.Dash()


fruits = {'Day':[1,2,3,4,5,6],

             'Visitors':[43,34,65,56,29,76],

             'Bounce_Rate':[65,67,78,65,45,52],

             'Nice_Fruits':['apple', 'apple', 'grape', 'apple', 'grape', 'grape']}

df_all_fruits = pd.DataFrame(fruits)


Nice_Fruits_list=df_all_fruits['Nice_Fruits'].unique()


app.layout = html.Div(children=[

html.H1('Payment Curve and predictor'),


 html.Label('fruits_1'),

    dcc.Checklist(

    id='fruits_checklist',

    options=[{'label': i, 'value': i} for i in Nice_Fruits_list],

        values=['apple', 'grape'],

    labelStyle={'display': 'inline-block'}

    ),


 dcc.Graph(

        id='fruits_graph',

        figure={

            'data': [

                go.Scatter(

                    x=df_all_fruits['Visitors'],

                    y=df_all_fruits['Bounce_Rate'],

                    mode='markers',

                    opacity=0.7,

                    marker={

                        'size': 15,

                        'line': {'width': 0.5, 'color': 'white'}

                    },

                )

            ],

            'layout': go.Layout(             

        xaxis={'type': 'linear', 'title': 'Visitors'},

                yaxis={'title': 'Bounce_Rate'},

                margin={'l': 40, 'b': 40, 't': 10, 'r': 10},

                hovermode='closest'

            )

        }

    ),


])

慕容3067478
浏览 163回答 1
1回答

慕丝7291255

所以你想要的是更新你的数据,这就是你要做的,但要这样做,你必须把两个图放在同一个散点上。你确实想修改回报。您需要遍历 Checkbox 中的值。然后告诉图形将两个散点放在同一个图形上:@app.callback(Output('fruits_graph', 'figure'),[Input('fruits_checklist', 'values')])def update_graph(values):      df = df_all_fruits      return {'data': [go.Scatter(        x= df[df['Nice_Fruits']==v]['Visitors'],        y= df[df['Nice_Fruits']==v]['Bounce_Rate'],    mode='markers',        marker={            'size': 15,            'opacity': 0.5,            'line': {'width': 0.5, 'color': 'white'}        }    ) for v in values],    'layout': {'margin': {'l': 40, 'r': 0, 't': 20, 'b': 30}}}告诉我它是否解决了您的问题。
随时随地看视频慕课网APP

相关分类

Python
我要回答