Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hover tool for plotly slider widget (python)

I am using python 3.6.5 and plotly 3.9.0 to create an interactive line graph that the user can change the range using a ranger slide.

I would like to add a hover tool to the range slider so that when the user moves the slider, a hover icon says the new date range before the user releases the mouse.

I think this is the default on bokeh, but I have given up on this and moved to plotly-dash.

Can this be done?

A minimum working example of what I am trying to do is below.

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import plotly.plotly as py

from datetime import datetime
import pandas as pd
import numpy as np

np.random.seed(10)
df = pd.DataFrame({
    'date':pd.date_range(start='1/1/2000', periods=200),
    'x': np.random.choice(range(100),200)
})

app = dash.Dash()
app.layout = html.Div([
    dcc.Graph(
        id='graph',
    ),

    dcc.RangeSlider(
        id='slider',
        min = df['date'].min().timestamp(),
        max = df['date'].max().timestamp(),
        value=[df.iloc[-101]['date'].timestamp(), df.iloc[-1]['date'].timestamp()]
    )

])


@app.callback(
    dash.dependencies.Output('graph','figure'),
    [dash.dependencies.Input('slider','value')])

def update_figure(value):
    lBound = pd.to_datetime(value[0], unit='s')
    uBound = pd.to_datetime(value[1], unit='s')
    filteredData = df.loc[(df['date']>=lBound) & (df['date']<=uBound)]
    fig = [
        go.Scatter(
            x=filteredData['date'],
            y=filteredData['x'],
            mode='lines',
            name='xxxx'
        )
    ]

    layout = go.Layout(
                xaxis={'title': ' '},
                yaxis={'title': 'per cent'},
                hovermode='closest')

    return {'data':fig,'layout':layout}


if __name__ == '__main__':
    app.run_server(debug=True)
like image 483
brb Avatar asked May 30 '19 14:05

brb


People also ask

What is hover data in Plotly?

Hover Labels One of the most deceptively-powerful features of interactive visualization using Plotly is the ability for the user to reveal more information about a data point by moving their mouse cursor over the point and having a hover label appear.

What is range slider in Plotly?

In plotly, the range slider is a custom range-type input control. It allows selecting a value or a range of values between a specified minimum and maximum range. And the range selector is a tool for selecting ranges to display within the chart. It provides buttons to select pre-configured ranges in the chart.

Is Plotly Express free?

Plotly Express is a terse, consistent, high-level API for creating figures. New to Plotly? Plotly is a free and open-source graphing library for Python.


1 Answers

Right now there is no feature for hovering on slider for plotly on dash.
You can check documentation for Slider and RangeSlider.

I know this is not what you want, but it will be easier to use marks for the slider to know the position.

dcc.RangeSlider(
        id='slider',
        min = df['date'].min().timestamp(),
        max = df['date'].max().timestamp(),
        value=[df.iloc[-101]['date'].timestamp(), df.iloc[-1]['date'].timestamp()],
        marks={
        955411200: {'label': '2000-04-11', 'style': {'color': '#77b0b1'}},
        959644800: {'label': '2000-05-30'},
        957484800: {'label': '2000-05-05'},
        961804800: {'label': '2000-06-24', 'style': {'color': '#f50'}}
    }

    )

Change your dcc.RangeSlider to this. It might be better than having no hover.

Else you can use Slider Widget with just plotly, limited to jupyter though.

like image 180
ASHu2 Avatar answered Sep 19 '22 14:09

ASHu2