pythonplotlyplotly-dashplotly-pythonplotly.graph-objects

How to filter heatmap colorscale in Plotly Dash?


I am new to plotly dash. I am trying to create an interactive dashboard where I can filter the colorbar to see the upper values for example if the value is 3000 it was red, so if I type 3000 as input, it is still red but the graph will not show values less than 3000. I am able to add the filtering option but when I filter(I used zmin in go heatmap) the colorscale also changes. Can I keep the previous colorscale so that if I choose zmin, it refreshes the graph with the original colorscale but filters values greater than zmin? Here is the code I have written so far -

app.layout = html.Div(children=[
    html.H1(children='Title'),

    dcc.Graph(
        id='graph',
        figure=fig
    ),
    dcc.Input(
        id="input", type="number", value=0
    )
])
@app.callback(
    Output('graph', 'figure'),
    Input('input', 'value')
)
def update_figure(input):
    frames = []
    for d, i in enumerate(sorted(timestamp_list)):
        frames.append(
            go.Frame(
                name=time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(int(i) / 1000)),
                data=[
                    go.Heatmap(z=df_dict[i],
                               x=df_dict[i].columns,
                               y=df_dict[i].index,
                               zmin=input,
                               zmax=max(value_list))
                ]
            )
        )
    yaxis_name = kind.split("_")[0]
    xaxis_name = kind.split("_")[1]


    fig = go.Figure(
        data=frames[0].data,
        frames=frames,
        layout=go.Layout(
            autosize=True,
            height=800,
            yaxis={"title": yaxis_name, "dtick": 1},
            xaxis={"title": xaxis_name, "tickangle": 45, "side": 'top'},

        ),
    )

    # finally, create the slider
    fig.update_layout(
        updatemenus=[{
                'buttons': [
                    {
                        'args': [None, {'frame': {'duration': 500, 'redraw': True},
                                        'transition': {'duration': 500, 'easing': 'quadratic-in-out'}}],
                        'label': 'Play',
                        'method': 'animate'
                    },
                    {
                        'args': [[None], {'frame': {'duration': 0, 'redraw': False},
                                          'mode': 'immediate',
                                          'transition': {'duration': 0}}],
                        'label': 'Pause',
                        'method': 'animate'
                    }
                ],
                'direction': 'left',
                'pad': {'r': 10, 't': 100},
                'showactive': False,
                'type': 'buttons',
                'x': 0.1,
                'xanchor': 'right',
                'y': 0,
                'yanchor': 'top'
            }],
        sliders=[{"steps": [{"args": [[f.name], {"frame": {"duration": 0, "redraw": True},
                                                 "mode": "immediate", }, ],
                             "label": f.name, "method": "animate", }
                            for f in frames],
                  }]
    )
    return fig

Here is the sample output I get-[![enter image description here][1]][1] After filtering- [![enter image description here][2]][2]


Solution

  • I am not completely sure I understood what you mean, but isn't it enough to just filter your data? I also don't have an example of how you data look like, but why don't you try filtering your data frame before you plot?

    data_to_plot = frames[frames['your_column'] > zmin]